

Better Box Shadows (CSS)
source link: https://uglyduck.ca/better-box-shadows/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Box shadow on HTML elements has been widely supported across most browsers for a while now, but I find the default options don’t allow for much visual manipulation of the shadows in general.
Let’s take a look at a default configuration of box-shadow
:
.box-container { box-shadow: 0 4px 8px rgba(0,0,0,0.3); }
In the example above the first property number is the origin of the x-axis , the second number is the origin of the y-axis and the third is the amount of blur .
We should also add some minimal styling to cleanup the .box-container
a little bit for our example:
<div class="box-container"></div>
.box-container { box-shadow: 0 4px 8px rgba(0,0,0,0.3); /* Styles to make it less ugly */ background: white; border-radius: 10px; border: 1px solid #eee; height: 200px; padding: 10px; position: relative; width: 250px; }
Which would render as this:
Not bad – but we can do a lot better than this.
Please sir, I want some more (depth)
We just need to add a simple child div
(or use a pseudo
element if you prefer) inside our main element we want to apply the shadow to:
<div class="box-container"> <div class="box-container-inner"></div> </div>
Now we make our inner child element absolute
and set it’s height
and width
dynamically to be slightly smaller than it’s parent (percentages work best for this). Remember to set this child element behind it’s parent by adding z-index: -1
.
.box-container { /* No box-shadow needed on this element anymore */ /* Styles to make it less ugly */ background: white; border-radius: 10px; border: 1px solid #eee; height: 200px; padding: 10px; position: relative; width: 250px; } .box-container-inner { bottom: 0; /* The box-shadow is added here now */ box-shadow: 0 4px 12px rgba(0,0,0,0.3); height: 94%; left: 3%; position: absolute; width: 94%; z-index: -1; }
Which will make the drop-shadow render with a little more realistic depth:
But wait – there’s more!
We could stop now and have a decent drop-shadow that is certainly easier on the eyes – but we can make this even better with one extra property – filter:blur();
.
So your final code would look like this:
.box-container { /* Styles to make it less ugly */ background: white; border-radius: 10px; border: 1px solid #eee; height: 200px; padding: 10px; position: relative; width: 250px; } .box-container-inner { bottom: 0; box-shadow: 0 4px 12px rgba(0,0,0,0.3); filter: blur(6px); height: 94%; left: 3%; position: absolute; width: 94%; z-index: -1; }
Which renders out into a much smoother blend of a drop-shadow, creating a more realistic illusion of depth:
Comments
No comments (yet)
Recommend
-
8
CSS Box Sizing Module Level 4↑ → CSS Box Sizing Module Level 4 Editor’s Draft, 15 December 2020
-
8
by zhangxinxu from https://www.zhangxinxu.com/wordpress/?p=8346 本文可全文转载,个人网站无需授权,只要保留原作者、出处以及文中链接即可...
-
8
IntroductionIn my humble opinion, the best websites and web applications have a tangible “real” quality to them. There are lots of factors involv...
-
10
Scroll Shadows With JavaScript Chris Coyier on Oct 5, 2021 (Updated on Oct 6, 2021) Take your JavaScript to the next level at ....
-
15
Design detail: crafting better shadows for interactionShadow doesn’t get as much love as other design elements, such as cool colour palettes or super sexy shapes, but it is vital in communicating interactio...
-
5
In Praise of Shadows DigitalOcean joining forces with CSS-Tricks! Special welcome offer:
-
8
Adding Box Shadows to WordPress Blocks and Elements DigitalOcean provides cloud products for every stage of your journey. Get started with
-
8
Different Ways to Get CSS Gradient Shadows T...
-
10
Ranked #11 for todayCSS Box Shadows GeneratorCreate, edit and generate multiple custom CSS box shadowsCSS Box Shadow Generator enables...
-
4
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK