Explanations:

  1. , - CSS language properties used to create shadows.
  2. , , — shadow property parameters.
  3. , , , — CSS properties, responsible for changing the size and location of elements.
  4. — tag, responsible for creating links.
  5. Selectors are a kind of links with the help of which CSS determines which elements should apply certain styles.
  6. Hover is a state selector.
  7. — property of animated object change.

Shadows are often used to achieve a three-dimensional effect. They allow you to give texts and blocks a unique style, and in combination with special fonts, you can get a great effect.

CSS offers us so much room for imagination and experimentation that it will take you years to try them all.

In this article, we'll go into detail about creating shadows in CSS. In addition to regular outer shadows, we'll show you how to create inner and state shadows.

CSS - box shadow. box-shadow property


html( background: #fff; ) body( width: 80%; margin: 0 auto; background-color: rgba(150,60,60,0.54) box-shadow: -1px 5px 5px 5px rgba(77,63,63 ,0.88) )

html (

background : #fff ;

body(

width: 80%;

margin: 0 auto;

box-shadow : -1px 5px 5px 5px rgba (77,63,63,0.88 )

The tool allows us to create one or more shadows around a block, with any color.

Essentially, what is a shadow? This is an exact copy of the element's area, which has a specific color and location. These parameters can be set using the box-shadow property.

The shadow properties are used in this order:

  1. Move horizontally.
  2. Move vertically.
  3. Level of vagueness.
  4. Stretching.
  5. Shadow color.

This is the order in which the shadow parameters should be specified. The main thing is not to confuse the value, since the first 4 are set the same way, using numeric values ​​- , , and so on.

The first parameter, which is called — shifts the shadow horizontally relative to the block. Accepts any values, including negative ones.

Negative values ​​shift the shadow to the left, and positive values ​​shift the shadow to the right. The parameter value can be equal to 0 when blurring shadows along the vertical axis.

Next in order is the property . Responsible for moving the shadow vertically. It also accepts any values ​​- positive and negative. Negative values ​​move the shadow up, and positive values ​​move the shadow down.

This parameter can be set to 0. Typically used for horizontal shadow blur.

Attention! We recommend specifying the value in pixels. You can use other values ​​- , , % and others, but this is not recommended. All these values ​​are quite large for creating shadows. By specifying them, you can go overboard with the sizes.

3 value - or blur. It is not necessary to enter this value; without specifying it, the shadows will still work.

The specified value determines the radius along which the shadow will be blurred. Negative values ​​are not indicated.

Larger values ​​can also be used here. A strong blur will help create a colorful effect, but only in some situations.

The penultimate value, which is responsible for stretching the shadow. This is also optional, but can add some color to your shadow.

Expansion occurs due to stretching of the central solid part of the shadow, which is located between the blurred edges.

This parameter accepts any values, both positive and negative. Positive values ​​stretch the shadow, while negative values ​​shrink it.

And the last parameter - . It is also not necessary to specify it, since by default the box shadow has its own color - black.

To change the color, any color system is suitable - as well as standard colors.

Attention! If you want the property to work in Safari, then this parameter must be specified, even if you use the standard black color.

Inner Shadow - CSS


html( background: #fff; ) body( width: 80%; margin: 0 auto; background-color: rgba(150,60,60,0.54) box-shadow: -1px 5px 5px 5px rgba(77,63,63 ,0.88) inset; )

html (

background : #fff ;

body(

width: 80%;

margin: 0 auto;

background-color : rgba (150,60,60,0.54 )

box-shadow : -1px 5px 5px 5px rgba (77,63,63,0.88 ) inset ;

So let's look at the first example of using shadows. Here we will look at creating an inner shadow for the body block.

To get started, open our HTML document and fill it out. You don’t have to specify any tags, don’t fill out the content, the main thing is to create a tag .

Open our CSS document and write the tag , it will be our main selector.

Next, open the curly braces and write the property . Set the location, blur, stretch. You already know how to set these values. After that, we set the color of the shadow, we made it white, but you can set it to your liking.

Now the most important thing. In order to create an inner shadow of a block in CSS, you need to specify the inset value. You can specify it immediately after defining the color.

As a result, we ended up with this block:

HTML/CSS - Text Shadow


html( background: #fff; ) body( width: 80%; margin: 0 auto; background-color: rgba(150,60,60,0.54) box-shadow: -1px 5px 5px 5px rgba(77,63,63 ,0.88) inset; ) div( margin: 150px; box-shadow: -1px 5px 5px 5px rgba(77,63,63,0.88); font-size: 25px; )

html (

background : #fff ;

body(

width: 80%;

margin: 0 auto;

background-color : rgba (150,60,60,0.54 )

5px rgba(77,63,63,0.88);

font-size: 25px;


Created in a similar way, but instead of box-shadow using the property . The property parameters are in the following order.

  1. Horizontal shift.
  2. Vertical shift.
  3. Blur radius. Not necessary.
  4. Color.

As you can see, there is no stretch option in this property. The size of the shadow will always be identical to the size of the text; this cannot be corrected.

It is very convenient to apply shadows on top of each other. To do this, you need to list several groups of parameters

The first shadow will be located closer to the text, and the second above it. Reminiscent of working with the outline and border properties, but here you can create 2, 3 or more shadows.

Hover Shadows - CSS

Now let's look at creating shadows for a specific state. In our example we will use a button.

In order to create a shadow for a button, you first need to place it. Open the a tag, write down the button class for it and any text value for better display.

First, let's set the placement using the margin property and increase the button to a certain size. To increase it, you can use the width and height properties, or use the padding property.

Now we set the background color, text and frame. Our button is ready, let's start creating the shadow.

First, let's create a regular shadow, small in size, using box-shadow. It will be visible by default, without hovering over the button.


Let's make the simplest effect - increase the shadow. You can make it blurry or change the stretch to suit your taste.

For the best effect, let's set the transition property for a regular link and set the time to 0.5s.

Now we have a great button with a beautiful shadow that will smoothly expand when hovered.


Conclusion

In this article, we learned about all the basic ways to create shadows. We have discussed all the properties and parameters of shadows.

You can find all the elements that were presented in the screenshots in the source folder. If you want, you can explore them yourself and experiment with the values. Maybe you will do much better than we did.

The box-shadow property adds one or more shadows to an element. The shadow is a copy of the element offset by the specified distance. Shadows can be external or internal, blurry or flat, and can follow the outlines of blocks with rounded corners. Using the inset keyword, shadows are created inside the element, making the element visually voluminous or depressed.

How to make a box shadow using the box-shadow property

Browser support

IE: 9.0
Edge: 12.0
Firefox: 4.0, 3.5 -moz-
Chrome: 10.0, 4.0 -webkit-
Safari: 5.1, 3.1 -webkit-
Opera: 11.5
iOS Safari: 5.1, 3.1 -webkit-
Android Browser: 4.0, 2.1 -webkit-

1. Box-shadow property syntax

The box-shadow property attaches one or more shadows to a box. The property accepts either the value none , which indicates no shadows, or a comma-separated list of shadows ordered from beginning to end.

Each shadow is a separate shadow, represented by 2 to 4 lengths, an optional color, and an optional inset keyword. Allowed lengths 0 ; omitted colors default to the value of the color property.

The property is not inherited.

Rice. 1. Box-shadow property syntax
Values:
x-offset Sets the horizontal offset of the shadow. A positive value draws a shadow offset to the right of the text, a negative length draws a shadow to the left.
y-offset Sets the vertical offset of the shadow. A positive value moves the shadow down, a negative value moves the shadow up.
blur Sets the blur radius. Negative values ​​are not allowed. If the blur value is zero, then the edge of the shadow is sharp. Otherwise, the higher the value, the more blurred the edge of the shadow is.
stretching Sets the distance by which the shadow increases. Positive values ​​cause the shadow to expand in all directions by the specified radius. Negative values ​​cause the shadow to shrink. For interior shadows, expanding the shadow means compressing the shape of the shadow's perimeter.
color Sets shadows. If there is no color, the color used is taken from the color property. For Safari, the shadow color must be specified.
inset Changes the block's cast shadow from an outer shadow to an inner shadow.
none The default value means no shadow.
initial Sets the property value to the default value.
inherit Inherits the property value from the parent element.

Shadow effects are applied as follows: the first shadow is on top and the rest are behind. Shadows do not affect the layout and may overlap other elements or their shadows. In terms of stacking contexts and drawing order, an element's outer shadows are drawn directly below that element's background, and an element's inner shadows are drawn directly above that element's background (below the border and border-image , if any).

If an element has multiple boxes, they all receive shadows, but shadows are only drawn where borders will also be drawn; see box-decoration-break.

Shadows do not cause scrolling or increase the size of the scrollable area.

2. Block shadow examples

2.1. Inner shadow

.example-shadow-1 ( background: #e6e3df; text-align: center; ) .example-shadow-1 span ( margin: 50px; height: 100px; width: 200px; display: inline-block; box-shadow: inset 2px 2px 5px rgba(154, 147, 140, 0.5), 1px 1px 5px rgba(255, 255, 255, 1); )

2.2. Flat shadow on one side

flat

flat

.example-shadow-2 ( background: beige; text-align: center; ) .example-shadow-2 a ( display: inline-block; border-radius: 5px; padding: 15px 35px; font-size: 22px; text -decoration: none; margin: 20px; color: white; background: #55acee; ​​box-shadow: 0 5px 0 #3C93D5; transition: .3s; ) .example-shadow-2 a:hover ( background: #6FC6FF; )

2.3. Material design shadow

.example-shadow-3 ( background: #e8e8e8; text-align: center; ) .example-shadow-3 span ( background: white; display: inline-block; width: 200px; height: 100px; margin: 50px; box -shadow: 0 14px 28px rgba(0,0,0,0.25), 0 10px 10px rgba(0,0,0,0.22); )

Adds a shadow to an element. It is possible to use several shadows, indicating their parameters separated by commas; when applying shadows, the first shadow in the list will be higher, the last one lower. If a rounding radius is specified for an element via the border-radius property, then the shadow will also have rounded corners. Adding a shadow increases the width of the element, so it may appear horizontal stripe scrolling in the browser.

brief information

Syntax

Box-shadow: none |<тень> [,<тень>]*

Where<тень>:

inset<сдвиг по x> <сдвиг по y> <размытие> <растяжение> <цвет>

Designations

DescriptionExample
<тип> Indicates the type of the value.<размер>
A && BThe values ​​must be output in the order specified.<размер> && <цвет>
A | BIndicates that you need to select only one value from the proposed ones (A or B).normal | small-caps
A || BEach value can be used independently or together 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.<время>#
×

Values

none Cancels adding a shadow. inset The shadow is drawn inside the element.<сдвиг по x>The horizontal offset of the shadow relative to the element. A positive value of this parameter sets the shadow shift to the right, a negative value - to the left.<сдвиг по y>The vertical offset of the shadow relative to the element. A positive value shifts the shadow down, a negative value shifts it up.<размытие>Sets the shadow blur radius. The higher this value, the more the shadow is smoothed out, becoming wider and lighter. If this parameter is not specified, it defaults to 0 and the shadow will be sharp rather than blurry.<растяжение>A positive value stretches the shadow, a negative value, on the contrary, compresses it. If this parameter is not specified, the default is 0, which will cause the shadow to be the same size as the element.<цвет>Shadow color in any available CSS format, default shadow is black.

It is possible to specify several shadows, separating their parameters with a comma. The following order is taken into account: the first shadow in the list is placed at the very top, the last in the list is at the very bottom.

Example

box-shadow

Would citrus live in the thickets of the south? Yes, but a fake copy!


The result of the example is shown in Fig. 1.

Rice. 1. Type of shadow

Object model

An object.style.boxShadow

Note

Safari before version 5.1, Chrome before version 10.0, Android before version 4.0 support the -webkit-box-shadow property.

Firefox before version 4.0 supports the -moz-box-shadow property.

Internet Explorer prior to version 9.0 does not support the box-shadow property; instead, you can use the filter property:

Filter: progid:DXImageTransform.Microsoft.dropshadow(offX=5, offY=5, color=#000000);

Here: offX - horizontal shadow offset; offY — vertical shadow offset; color — shadow color.

Using the dropshadow filter gives a clear, sharp shadow, so you can use the shadow filter for a blur effect.

Filter: progid:DXImageTransform.Microsoft.shadow(direction=120, color=#000000, strength=10);

Here: direction — shadow direction angle from 0 to 360°; color — shadow color; strength — shadow offset in pixels.

Specification

Each specification goes through several stages of approval.

  • Recommendation - The specification has been approved 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 goals, but requires help from the development community to implement the standard.
  • Proposed Recommendation Suggested Recommendation) - at this stage the document is submitted to the W3C Advisory Council for final approval.
  • Working Draft - A more mature version of a draft that has been discussed and amended for community review.
  • Editor's draft ( Editorial draft) - a draft version of the standard after changes were made by the project editors.
  • Draft ( Draft specification) - the first draft version of the standard.
×

With the advent of the standard CSS 3 introduced the box-shadow property, which has become widely used among web developers, as it makes it easy to add one or more shadows to an element's box to give the desired visual effect.

In the previous article, we looked at the border-radius property; using it, you can get a shadow with rounded corners. By analogy with a text shadow (text-shadow), you can create multiple shadows, they are applied according to z-axis front to back (with the first given shadow on top).

Let's take a closer look at the syntax of this modern property:


Let us consider in order the possible values ​​of this property:

MeaningDescription
noneThe shadow is not displayed. This is the default value.
insetOptional value. If this value is not specified (the default), the shadow will be on the outside of the element and will create the effect of the element being bulged. If the keyword (value) inset is present, the shadow will fall inside the element and create a dented effect. In other words, it is a change from an outer shadow to an inner one.
h-shadowRequired value. Sets the location of the horizontal shadow. Negative values ​​are allowed.
v-shadowRequired value. Sets the location of the vertical shadow. Negative values ​​are allowed.
blur-radiusOptional value. Sets the blur radius. The higher the value, the greater the blur, making the shadow larger and lighter. If the value is not specified, the value will be 0 (sharp - distinct shadows). Negative values ​​are not allowed.
spread-radiusOptional value. Shadow size (shadow stretch radius). With positive values ​​the shadow will expand, and with negative values ​​it will shrink. If no value is specified, the value will be 0 (the shadow corresponds to the size of the element).
colorOptional value. Defines the color of the shadow (HEX, RGB, RGBA, HSL, HSLA, "Predefined colors"). The default value is black.

The main points you need to understand to create shadows for elements are:

I would like to draw your attention to the fact that the box-shadow property is currently supported by all modern browsers:

Property
Opera

IExplorer

Edge
box-shadow10.0
4.0
-webkit-
4.0
3.5
-moz-
10.5 5.1
3.1
-webkit-
9.0 12.0

If you want to expand support for some browsers, including mobile iOS 3.2 - 4.3 And Android 2.1 - 3, then it is recommended to use manufacturer prefixes and use the following syntax (in the examples in the article, the syntax will only be used for modern browsers):

-webkit-box-shadow : value ; /* Safari 3.1 - 4, IOS 3.2 - 4.3 and Android 2.1 - 3 */-moz-box-shadow : value ; /* Firefox 3.5 - 3.6 */ box-shadow : value ; /* table above */

Let's move on to practice and start with simple example, in which we add one shadow to the elements:

Example of using the box-shadow property in CSS
box-shadow:10px 10px 0px red;
class = "test2" > box-shadow:10px 10px 10px #777;


The result of our example:

Using Multiple Shadows

In the next example, I propose to consider interesting effect mixing colors, which can be achieved by using multiple shadows:

Example of using multiple shadows in CSS (box-shadow property)


  • Two blocks of fixed width and height (15em and 10em), added padding on all sides (2em) and set the blocks to be block-line to be able to line them up.
  • For first block with class .test we indicated four shadows with different horizontal and vertical shadow values ​​so that the element has different shadows on all sides. The blur radius for all shadows was set to the same. The color of each shadow is different and is specified using predefined colors.
  • For second block with class .test2 we indicated four shadows with different horizontal and vertical shadow values. The blur radius for all shadows was set to the same, while the shadow stretch was set to negative, which led to a decrease in the shadow itself. The color of each shadow is different and is specified using the RGBA system.

The result of our example:

Using shadows for images

The final example of this article will focus on using shadows for images. I would like to immediately draw your attention to the fact that directly indicate the shadow for HTML element It won’t work, but we have the opportunity to specify an image as the background for the element we are interested in, and then assign the shadow we need to this element.

We will look at working with background images in detail in the textbook article "", and now, to put an end to the study of using shadows, we will touch on it superficially and use the image as a background to an element in the following example:

Example of using image shadows in CSS


  • Two blocks of fixed width and height (237px and 232px), added margins for all sides (2em) and set the blocks to be block-line to be able to line them up. We set the block size 237px by 232px to the image size so that at this stage training did not have to scale the image to fit the element and affect those CSS properties that are planned to be studied at a later stage (in the textbook article " ").
  • For first block with class .test We specified a zero value for the horizontal and vertical shadow, but at the same time we specified a blur radius of 50px and expanded it by setting a stretch radius of 10px. The shadow color was indicated with a predefined color (violet). In addition, we indicated in the advertisement keyword inset , which causes the shadow to fall inside the element. In other words, we have created an inner shadow of the element.
  • For second block with class .test2 We specified a zero value for the horizontal and vertical shadow, but at the same time we specified a blur radius of 50px and expanded it by setting a stretch radius of 10px. The shadow color is specified in RGBA mode.

The result of our example:

Rice. 98 Example of using shadows for images in CSS (box-shadow property)

Questions and tasks on the topic

Before moving on to the next topic, complete the practice assignment:


If you have difficulty completing the practice task, you can always open the example in a separate window and inspect the page to understand what CSS code was used.


2016-2019 Denis Bolshakov, you can send comments and suggestions on the site to [email protected]

Regular shadows are easy to implement using box-shadow or text-shadow. But what if you need to make internal shadows? This article describes how to make these shadows with just a few lines of code.

Syntax

First, let's look at the two main ways to implement shadows in CSS.

box-shadow

Design box-shadow contains several different meanings:

Horizontal offset And vertical offset- horizontal and vertical displacement, respectively. These values ​​indicate which direction the object will cast its shadow:

Blur radius And spread radius a little more complicated. What's the difference? Let's look at an example with two elements, where the values blur radius differ:

The edge of the shadow is simply blurred. With different values spread radius we see the following:

In this case, we see that the shadow is scattered over a large area. If you do not specify a value blur radius And spread radius, then they will be equal to 0.

text-shadow

The syntax is very similar to box-shadow:

The meanings are similar, but not spread-shadow. Usage example:

Inset in box-shadow

To “flip” the shadow inside the object, you need to add inset in CSS:

Once you understand the basic box-shadow syntax, it's very easy to understand how to implement inner shadows. The values ​​are still the same, you can add color (RGB in hex):

Color in RGB format, the alpha value is responsible for the transparency of the shadow:

Images with shadows

Adding an inner shadow to an image is a little more difficult than adding a regular shadow div. To begin with, here is the usual image code:

It is logical to assume that you can add a shadow like this:

Img ( box-shadow: inset 0px 0px 10px rgba(0,0,0,0.5); )

But the shadow is not visible:

There are several ways to solve this problem, each of which has its pros and cons. Let's look at two of them. The first is to wrap the image in a regular div:

Div ( height: 200px; width: 400px; box-shadow: inset 0px 0px 10px rgba(0,0,0,0.9); ) img ( height: 200px; width: 400px; position: relative; z-index: -2 ; )

Everything works, but we have to add a little extra HTML and CSS markup. The second way is to set the image as background the desired block:

Div ( height: 200px; width: 400px; background: url(http://lorempixum.com/400/200/transport/2); box-shadow: inset 0px 0px 10px rgba(0,0,0,0.9); )

This is what can happen when using inner shadows:

Inset to text-shadow

To implement an inner text shadow, simply add to the code inset does not work:

To solve, first apply to the header h1 install dark background and a light shadow:

H1 ( background-color: #565656; color: transparent; text-shadow: 0px 2px 3px rgba(255,255,255,0.5); )

Here's what happens:

Adding a secret ingredient background-clip which cuts off anything that extends beyond the text (to a dark background):

H1 ( background-color: #565656; color: transparent; text-shadow: 0px 2px 3px rgba(255,255,255,0.5); -webkit-background-clip: text; -moz-background-clip: text; background-clip: text ; )

It turned out almost exactly what we needed. Now we just darken the text a little (alpha), and the result is: