HTML element represents with shadow gives realistic impact. CSS provides properties to styles html element in different looks.
This post will describes how to give shadow effect to html element, different kinds of shadow styles, etc..
The shadow effect can be applied by two CSS properties.
2) text-shadow property
1) box-shadow property
The box-shadow property adds shadow to an html element.
Syntax of box-shadow
box-shadow: [horizontal offset] [vertical offset] [blur] [spread] [color];
This is the basic syntax. By default value of box-shadow property is none means no shadow will display.
Let’s understand the components of box-shadow.
Syntax explanation of box-shadow property
This component is required. As name suggest this offset puts shadow at left side or right side of the box. Two values can be specified. Positive and Negative.
Positive value generate shadow at right of box.
Negative value generate shadow at left of box.
This component is also required. This offset puts shadow at top or bottom. This offset have also positive and negative value.
Positive value puts shadow at bottom of box.
Negative value puts shadow at top of box.
Gives blur effect to shadow.
Higher blur value gives more blur effect and lower blur value gives less blur effect.
Negative value don’t work. Always specify positive value.
spread value specify the size of shadow.
The default value is zero.
Positive value increase the area acquired by shadow.
Negative value decrease the area acquired by shadow.
The color value is not compulsory but if you want shadow in different color specify color value.
When color value is not present the shadow will display in color of text. (If text color is black shadow will also black color. )
Color value can be specified by many color values like rgba, hsla, color code, name.
Examples
1) Following example defines shadow at horizontal offset. The shadow at right side. (With positive value)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | <html> <head> <title>box-shadow</title> <style> .hshadow{ margin:100px; height:300px; padding:15px; font-size:18pt; width:300px; background-color:#f0f0f0; box-shadow:6px 0px #ccc;} </style> </head> <body> <div class="hshadow"> shadow at horizontal offset.</div> </body> </body> </html> |
Output:-
Shadow at horizontal offset (positive value) |
Shadow at horizontal offset (negative value) |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | <html> <head> <title>box-shadow</title> <style> .vshadow{ margin:100px; height:300px; padding:15px; font-size:18pt; width:300px; background-color:#f0f0f0; box-shadow:0px 6px #ccc;} </style> </head> <body> <div class="vshadow"> shadow at vertical offset.</div> </body> </body> </html> |
Output:-
Shadow at vertical offset (bottom) |
Shadow at vertical offset (negative value) |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | <html> <head> <title>box-shadow</title> <style> .blureffect{ margin:100px; height:100px; width:100px; padding:15px; background-color:#0f0; box-shadow:6px 6px 3px #c0c0c0;} </style> </head> <body> <p class="blureffect">blur effect</p> </body> </body> </html> |
Output:-
Shadow with blue effect |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | <html> <head> <title>box-shadow</title> <style> .sprd{ height:300px; width:300px; margin:100px; background-color:#a2f; box-shadow:5px 5px 4px 9px #ccc;} </style> </head> <body> <div class="sprd">shadow spreads to 6px .</div> </body> </body> </html> |
Output:-
Shadow with spread effect |
Shadow with blue effect negative value |
Shadow with text color value |
Multiple shadows effect with box-shadow
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | <html> <head> <title>box-shadow</title> <style> .multishad{ margin:100px; height:300px; width:300px; background-color:#eaf; box-shadow:10px 10px 5px #f00,20px 20px 5px #0f0,30px 30px 5px #00f;} </style> </head> <body> <div class="multishad"></div> </body> </body> </html> |
Output:-
Multiple shadow with box-shadow |
Multiple shadows (negative value) |
Multiple shadows with box-shadow |
How to generate inner shadow?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | <html> <head> <title>box-shadow</title> <style> .inner{ margin:100px; height:300px; width:300px; background-color:#eee; box-shadow:6px 6px 6px inset black;} </style> </head> <body> <div class="inner"></div> </body> </body> </html> |
Output:-
Inner shadow with box-shadow |
Multiple inner shadows
Multiple inner shadows with box-shadow |
box-shadow:initial value
box-shadow:initial |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | <html> <head> <title>box-shadow</title> <style> .one{ position:relative; height:700px; width:700px; background-color:#000;} .two{ position:absolute; left:200px; top:100px; height:300px; width:300px; border-radius:250px; background-color:#eee; box-shadow:inset 20px 20px 50px #000; } </style> </head> <body> <div class="one"> <div class="two"></div> </div> </body> </body> </html> |
Output:-
box-shadow example (fully round element) |
2) text-shadow property
Syntax of text-shadow
Syntax explanation of text-shadow property
This property is required.
Positive and negative both values are applicable.
With positive value shadow generates at right side of text.
Negative value generates shadow at left.
The initial value of text-shadow
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | <html> <head> <title>Text-shadow</title> <style> .hoffset{ text-shadow:3px 0px #f00; font-size:18pt;} </style> </head> <body> <p class="hoffset">This is paragraph.</p> </div> </body> </body> </html> |
Output:-
text-shadow horizontal offset |
Text shadow with negative value |
Multiple shadows with positive and negative value |
0 Comments