Radial CSS Gradient. Radial Gradient Gradient and Background Image Combination

When we talk about gradients, it "s worth beginning with the fact that gradients are an image replacement in CSS. That" sa fancy way of saying that creating a gradient in CSS provides the browser with instructions for painting an image on the screen rather than you providing the browser with the source URL of a file you created in an image editing application, like Photoshop or Sketch. It "s a native CSS way for doing the same thing in code and, as such, gradients are included in the CSS Image Values ​​and Replaced Content specification.

You can see how that gradient assumes the shape is ellipse. That "s because the element itself is not a perfect square. In that case, it would have assumed a circle instead. Pretty smart! Here" s what would happen if we had explicitly declared circle as the shape value:

Gradient (background-image: radial-gradient (circle, yellow, # f06d06);)

Notice the gradient is circular, but only fades all the way to the ending color along the farthest edge. Now we can explicitly declare the position value to ensure that the fade ends by the "closest-side" of the shape instead, like this:

Gradient (background-image: radial-gradient (circle closest-side, yellow, # f06d06);)

The possible values ​​there are: closest-corner, closest-side, farthest-corner, farthest-side. You can think of it like: "I want this radial gradient to fade from the center point to the __________, and everywhere else fills in to accommodate that."

A radial gradient doesn "t have to start at the default center either, you can specify a certain point by using" at ______ "as part of the first parameter, like:

Gradient (background-image: radial-gradient (circle at top right, yellow, # f06d06);)

I "ll make it more obvious here by making the example a square and adjusting a color-stop:

Browser Support

Browser support for radial-gradient () is largely the same as. The exception is a very old version of Opera. Right when they started supporting gradients they only accounted for linear and not radial.

But similar to linear-gradient (), if your browser support needs to go super deep, then you might consider using or a similar tool that handles vendor prefixing for you rather than having to manage those yourself.

This browser support data is from

Rice. 1. Radial and linear gradient

A radial gradient is created using the background or background-image property.

Syntax

background-image: radial-gradient ([circle ||<радиус>] [at<позиция>]? , | [ellipse || [<радиус> | <проценты>] (2)] [at<позиция>]? , | [[circle | ellipse] ||<размер>] [at<позиция>]? , | at<позиция> , <цвет> [ , <цвет> ]*)

Designations

DescriptionExample
<тип> Indicates the type of the value.<размер>
A && BThe values ​​must be displayed in the order shown.<размер> && <цвет>
A | BIndicates that only one of the suggested values ​​should be selected (A or B).normal | small-caps
A || BEach value can be used alone or in conjunction with others in any order.width || count
Groups values.[crop || cross]
* Repeat zero or more times.[,<время>]*
+ Repeat one or more times.<число>+
? The specified type, word or group is optional.inset?
(A, B)Repeat at least A, but no more than B times.<радиус>{1,4}
# Repeat one or more times, separated by commas.<время>#

The values

circle A radial gradient of a circular shape. ellipse Creates an elliptical gradient. This form is set by default.<радиус>The radius of the gradient in CSS units. One value indicates the radius of the circle, two values ​​indicate the radius of the ellipse along the x-axis and its radius along the y-axis. If no radius is explicitly specified, the gradient will fill the entire background of the element.<позиция>

Specifies the starting point where the gradient originates. Point position is written similarly to background-position values ​​using keywords or available units such as pixels or percentages; below are possible combinations.

  • top left = left top = 0% 0% (in the upper left corner);
  • top = top center = center top = 50% 0% (center top);
  • right top = top right = 100% 0% (in the upper right corner);
  • left = left center = center left = 0% 50% (left and center);
  • center = center center = 50% 50% (centered) is the default;
  • right = right center = center right = 100% 50% (right and centered);
  • bottom left = left bottom = 0% 100% (in the lower left corner);
  • bottom = bottom center = center bottom = 50% 100% (bottom center);
  • bottom right = right bottom = 100% 100% (bottom right corner).
<цвет>It is a color value (see color) followed by an optional color position relative to the gradient axis, it is specified as a percentage from 0% to 100% or in any other suitable CSS units.<размер>Sets the size of the gradient. Table 1 lists the possible size values ​​with their description and the result for white and black. Code and view given for circular and elliptical gradients. Tab. 1. Keywords for changing the size of the gradient
MeaningCodeDescriptionView
closest-side

background: radial-gradient (circle closest-side at 30px 20px, #fff, # 000);

background: radial-gradient (closest-side at 30px 20px, #fff, # 000);

The gradient coincides with the closest side of the block (for a circle) or simultaneously coincides with the nearest horizontal and vertical sides (for an ellipse).

background: radial-gradient (circle closest-corner at 30px 20px, #fff, # 000);

background: radial-gradient (closest-corner at 30px 20px, #fff, # 000);

The shape of the gradient is calculated based on the distance information to the closest corner of the block.

background: radial-gradient (circle farthest-side at 30px 20px, #fff, # 000);

background: radial-gradient (farthest-side at 30px 20px, #fff, # 000);

Similar in effect to closest-side, but the gradient extends to the far side of the box.
farthest-corner

background: radial-gradient (circle farthest-corner at 30px 20px, #fff, # 000);

background: radial-gradient (farthest-corner at 30px 20px, #fff, # 000);

The shape of the gradient is calculated based on the distance information to the far corner of the block,

Example

Gradient

Gradient

This element helps when you are aware of the fact that you absolutely do not understand who and how can help you. It is at this moment that we suggest that no one can help you.

The result of this example is shown below.

Note

Chrome prior to 26, Safari prior to 6.1 and Android prior to 4.4 support -webkit-radial-gradient ().

Opera prior to version 12.10 supports -o-radial-gradient ().

Firefox up to version 16 supports -moz-radial-gradient ().

All prefixed properties do not use the at keyword when specifying the position of the starting point of the gradient.

Specification

Each specification goes through several stages of approval.

  • Recommendation - This specification has been endorsed by the W3C and is recommended as a standard.
  • Candidate Recommendation ( Possible recommendation) - the group responsible for the standard is satisfied that it meets its objectives, but the help of the developer community is required to implement the standard.
  • Proposed Recommendation ( Suggested recommendation) - At this point, the document is submitted to the W3C Advisory Council for final approval.
  • Working Draft - A more mature version of the draft after discussion and revision for community review.
  • Editor "s draft ( Editorial draft) - a draft version of the standard after editing by the project editors.
  • Draft ( Draft specification) is the first draft of the standard.

CSS Gradient represents transitions from one color to another.

Gradients are created using the linear-gradient () and radial-gradient () functions.

A gradient background can be set in the background, background-image, border-image, and list-style-image properties.

How to make a gradient in CSS

Browser support

IE: 10.0
Firefox: 16, 3.6 -moz-
Chrome: 26.0, 10.0 -webkit-
Safari: 6.1, 5.1 -webkit-
Opera: 12.1, 11.1 -o-
iOS Safari: 7.1
Opera Mini:
Android Browser: 4.4, 4.1 -webkit-
Chrome for Android: 44

1. Linear-gradient ()


Rice. 1. Gradient line, start and end points and angle of the gradient

Linear Gradient is created using two or more colors for which a direction is specified, or gradient line.

If direction is not specified, the default is used - top down.

The default gradient colors are distributed evenly in a direction perpendicular to the gradient line.

Background: linear-gradient (angle / side or slope using keyword (keyword pairs), first color, second color, etc.);

Direction gradient can be specified in two ways:
using the tilt angle in degrees deg, the value of which determines the angle of the line within the element.

Div (height: 200px; background: linear-gradient (45deg, #EECFBA, # C5DDE8);)

using keywords to top, to right, to bottom, to left, which correspond to the angle of the gradient equal to 0deg, 90deg, 180deg, and 270deg, respectively.

Div (height: 200px; background: linear-gradient (to right, # F6EFD2, # CEAD78);)

If the direction is specified with a pair of keywords, for example, to top left, then the starting point of the gradient will be in the opposite direction, in this case bottom right.

Div (height: 200px; background: linear-gradient (to top left, powderblue, pink);)

For uneven distribution of colors, the starting position of each color is indicated through the gradient stop points, the so-called color stops. Breakpoints are set in%, where 0% is the start point, 100% is the end point, for example:

Div (height: 200px; background: linear-gradient (to top, # E4AF9D 20%, # E4E4D8 50%, # A19887 80%);)

For a clear distribution of color stripes, each subsequent color must start from the stopping point of the previous color:

Div (height: 200px; background: linear-gradient (to right, # FFDDD6 20%, # FFF9ED 20%, # FFF9ED 80%, #DBDBDB 80%);)

2. Radial-gradient ()

Radial gradient differs from linear in that the colors come out of one point (the center of the gradient) and are evenly distributed outward, drawing the shape of a circle or ellipse.

Background: radial-gradient (gradient shape / center size / position, first color, second color, etc.);

Gradient shape defined by the keywords circle or ellipse. If no shape is specified, the default radial gradient is elliptical.

Div (height: 200px; background: radial-gradient (white, # FFA9A1);)

Center position set using keywords used in the background-position property, appended with the at prefix. If no center position is specified, the default is at center.

Div (height: 200px; background: radial-gradient (at top, #FEFFFF, # A7CECC);)

A pair of values, specified in%, em, or px length units, controls the size of the elliptical gradient. The first value is the width of the ellipse, the second is the height.

Div (height: 200px; background: radial-gradient (40% 50%, # FAECD5, # CAE4D8);)

Gradient size specified using keywords. The default is farthest-corner (to the far corner).

div (height: 200px; background: radial-gradient (circle farthest-corner at 100px 50px, # FBF2EB, # 352A3B);)

With the help of a radial gradient, you can create realistic 3D shapes such as balls, buttons.

Ball

div (width: 200px; height: 200px; border-radius: 50%; margin: 0 auto; background: radial-gradient (circle at 65% 15%, aqua, darkblue);)

Button

.wrap (height: 200px; padding: 50px 0; background: #cccccc;) .button (width: 100px; height: 100px; border-radius: 50%; margin: 0 auto; background: radial-gradient (farthest-side ellipse at top left, white, #aaaaaa); box-shadow: 5px 10px 20px rgba (0,0,0,0.3), -5px -10px 20px rgba (255,255,255,0.5);)

Postage Stamp


Using a transparent color in gradients, you can create the following effects.

Jpg ">

.container (background: # B7D1D8; padding: 25px;) .wrap (background: radial-gradient (transparent, transparent 4px, white 4px, white); padding: 10px; width: 300px; height: 220px; background-size: 20px 20px; background-position: -10px -10px; / * crop the pattern to the edge * / margin: 0 auto;) img (width: 100%;)

3. Gradient repeat

In addition to linear and radial gradients, there is a gradient repeat, which is specified using the repeating-linear-gradient () and repeating-radial-gradient () functions, respectively. A repeating gradient background looks messy, so it is a good idea to start the next color at the point where the previous one stopped, thus creating striped patterns.

Div (height: 200px; background: repeating-linear-gradient (45deg, # 606dbc, # 606dbc 10px, # 465298 10px, # 465298 20px);) div (height: 200px; background: repeating-radial-gradient (circle, # B9ECFE, # B9ECFE 10px, # 82DDFF 10px, # 82DDFF 20px);)

4. Cross-browser gradient

To display gradients correctly in all browsers, you need to add a cross-browser entry.

Linear Gradient

Ms-filter: "progid: DXImageTransform.Microsoft.gradient (GradientType = 0, startColorstr = # 1471da, endColorstr = # 1C85FB)"; / * IE 8-9 * / background: -webkit-linear-gradient (left, red, # f06d06); / * Safari 5.1, iOS 5.0-6.1, Chrome 10-25, Android 4.0-4.3 * / background: -moz-linear-gradient (left, red, # f06d06); / * Firefox 3.6-15 * / background: -o-linear-gradient (left, red, # f06d06); / * Opera 11.1-12 * / background: linear-gradient (to right, red, # f06d06); / * Opera 15+, Chrome 25+, IE 10+, Firefox 16+, Safari 6.1+, iOS 7+, Android 4.4+ * /

Linear gradient repeat

Background: -webkit-repeating-linear-gradient (red, yellow 10%, green 20%); / * Safari 5.1 - 6.0 * / background: -o-repeating-linear-gradient (red, yellow 10%, green 20%); / * Opera 11.1-12.0 * / background: -moz-repeating-linear-gradient (red, yellow 10%, green 20%); / * Firefox 3.6-15 * / background: repeating-linear-gradient (red, yellow 10%, green 20%); / * Standard syntax * /

Radial gradient

Background: -webkit-radial-gradient (red, yellow, green); / * Safari 5.1-6.0 * / background: -o-radial-gradient (red, yellow, green); / * Opera 11.6-12.0 * / background: -moz-radial-gradient (red, yellow, green); / * Firefox 3.6-15 * / background: radial-gradient (red, yellow, green); / * Standard syntax * / background: -webkit-radial-gradient (60% 55%, farthest-side, red, yellow, black); / * Safari 5.1-6.0 * / background: -o-radial-gradient (60% 55%, farthest-side, red, yellow, black); / * Opera 11.6-12.0 * / background: -moz-radial-gradient (60% 55%, farthest-side, red, yellow, black); / * Firefox 3.6-15 * / background: radial-gradient (farthest-side at 60% 55%, red, yellow, black); / * Standard syntax * /

Radial gradient repeat

Background: -webkit-repeating-radial-gradient (red, yellow 10%, green 15%); / * Safari 5.1-6.0 * / background: -o-repeating-radial-gradient (red, yellow 10%, green 15%); / * Opera 11.6-12.0 * / background: -moz-repeating-radial-gradient (red, yellow 10%, green 15%); / * Firefox 3.6-15 * / background: repeating-radial-gradient (red, yellow 10%, green 15%); / * Standard syntax * /

5. Combination of gradient and background image

By combining the gradient and the image, you can create interesting effects. For the gradient, you need to use semi-transparent colors to keep the image visible.

div (height: 453px; background: linear-gradient (45deg, rgba (103, 0, 31, .8), rgba (34, 101, 163, ..jpg); background-size: cover;)

Radial gradient- a gradient for creating effects when transitioning from one color to another. Unlike a linear gradient, it uses either a circular or elliptical transition. The directions of the color change are in different directions from the center. Radial gradient is also called "circular gradient".

The radial gradient is set using the radial-gradient property in conjunction with background. It has quite a few parameters, which we will consider sequentially.

CSS radial-gradient syntax

background: radial-gradient ( color1, color2, ...);
  • position - initial coordinates X and Y (most often set in percent). There are constants:
    • top - centered on top (50% 0%)
    • left top - top left corner (0% 0%)
    • right top - upper right corner (100% 0%)
    • center (default) - center of the area (50% 50%)
    • left center - left center (0% 50%)
    • right center - right center (100% 50%)
    • bottom - bottom center (50% 100%)
    • left bottom - bottom left (0% 100%)
    • right bottom - bottom right (100% 100%)
    The at keyword is required.
  • shape - sets the shape of the gradient. There are two options:
    • circle - circle
    • ellipse - ellipse
  • size - how the gradient will stretch. Can take the following values
    • closest-side - The gradient tends to the closest border of the element.
    • closest-corner - The gradient tends to the closest corner.
    • farthest-side (default) - The gradient extends to the far edge of the element.
    • closest-corner - The gradient extends to the far corner of the element.
    Below is a table with examples
  • color1 - starting color
  • color2 - final color

The color can be set in RGB format, a specific color or in rgba format (see html color codes for the site).

Note 1

You can specify the transition of several colors separated by commas.

In its simplest form, only two colors can be specified. For example

background: radial-gradient (# ADFF2F, # 006400)
Note 2

For browsers, you need to set this property with vendor prefixes: -moz -, - webkit-, -ms-, -o-:

background: -moz-radial-gradient background: -webkit-radial-gradient background: -ms-radial-gradient background: -o-radial-gradient
Note 3

The radial-gradient property can also be made repeating: repeating-radial-gradient

background: repeating-radial-gradient ( circle, # 8FBC8F, # 8FBC8F .5em, transparent .5em, transparent 1.5em);

Examples with radial gradients

Example 1. Different start positions

The at keyword can be used to specify the position of the center of the radial gradient.

Example 2. Gradient size

Below is an example that shows how the border works.

The page is converted to the following

A radial gradient extends from the center point of the gradient in all directions in a circle or ellipse (the default shape), showing a smooth transition from one shade of color to another. A radial gradient is created using the radial-gradient () function. The function creates an image that is a radial gradient between the specified color shades. By default, the size of the gradient matches the size of the element to which it is applied.

The radial-gradient () function takes the following comma-separated arguments:

  • The first argument is keywords and / or CSS units that define the final shape, size, and starting position of the gradient. Optional argument.
  • A comma-separated list of two or more colors, each of which can be followed by a stop position.

The simplest radial gradient only requires two arguments to specify a start and end color:

Div (background-image: radial-gradient (cyan, indigo); width: 400px; height: 100px;) Try it "

As with linear gradients, a radial gradient can include a list of more than two colors, and a stop position can be specified for any color.

Div (width: 400px; height: 100px; margin: 10px;) #one (background-image: radial-gradient (cyan, yellow, indigo, white);) #two (background-image: radial-gradient (cyan, yellow 10%, indigo 30%, white 50%);) Try it "

The shape of the radial gradient can be defined using the keywords circle and ellipse, specifying one of them as the first argument:

Div (width: 400px; height: 100px; margin: 10px;) #one (background-image: radial-gradient (ellipse, cyan, indigo);) #two (background-image: radial-gradient (circle, cyan, indigo );) Try "

By default, the browser places the center point of the radial gradient at the center of the element. The center of the gradient can be positioned using the at keyword followed by keywords (top, left, right, bottom, center) or values ​​in specified CSS units:

Meaning Description
left top
left center
left bottom
right top
right center
right bottom
center top
center center
center bottom
If you only specify one keyword, the second will be "center".
x% y% The first value is the horizontal position, the second value is vertical. The top left corner is the 0% 0% position. The lower right corner is the 100% 100% position. If you only specify one value, the other value will be 50%.
x-pos y-pos The first value is the horizontal position, the second is the vertical one. The top left corner is position 0 0. The units can be pixels (0px 0px) or any other CSS unit of measure. If you only specify one value, the other value will be 50%. You can mix% and units.

Gradient positioning is specified before the hue values, but after the keyword defining the shape of the gradient (if specified):

Div (width: 400px; height: 100px; margin: 10px;) #one (background-image: radial-gradient (at right, cyan, indigo);) #two (background-image: radial-gradient (circle at 300px 50px , cyan, indigo, yellow);) #three (background-image: radial-gradient (circle at top left, cyan, indigo, yellow);) Try it "

The size of the gradient (how big the final shape of the gradient should be) can be determined using one of four key phrases or by specifying the radius using CSS units (you cannot use percentages to determine the radius).

Keywords Description
The final size of the gradient depends on the distance between the center of the gradient and the closest side of the element (for a circle) or the distance between the center and the two sides of the element closest to it (for an ellipse).
The final size of the gradient depends on the distance between the center of the gradient and the corner of the element closest to it.
The final size of the gradient depends on the distance between the center of the gradient and the farthest side of the element (for a circle) or the distance between the center of the gradient and the two sides farthest from it (for an ellipse).
The final size of the gradient depends on the distance between the center of the gradient and the corner of the element farthest from it. Used by default for a circle or ellipse shape.

The keywords defining the size of the gradient or the radius are specified before or after the keyword defining the shape, and always before defining the position.

Div (width: 400px; height: 100px; margin: 10px;) #one (background-image: radial-gradient (circle closest-corner at 100px, cyan 50%, indigo);) #two (background-image: radial- gradient (circle closest-side, cyan, red, indigo);) #three (background-image: radial-gradient (100px circle at 200px, cyan 50%, indigo);) #four (background-image: radial-gradient ( 170px 50px ellipse at 175px, cyan, # 90EE90, rgba (172,160,160,0));)

Liked? Like us on Facebook