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, the third is the amount of blur, the fourth is the optional spread, and the fifth is color.

Sidenote: As pointed out by gkoberger, you could just use the spread parameter to set the box-shadow to mimic the final outcome of this demo. I use a separate element since I tend to find myself altering more properties such as border-radius, width, and transform. But in the end, use what will work best for you!

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:

Now we can look and compare all 3 variations:


Comments

  1. Dylan says:

    Very subtle. Very cool. Nice!

  2. paul says:

    Check out this alternate approach … 1 div & the contents are not covered up byu the shadow’s container

  3. Tim says:

    Doesn’t box-shadow have a built-in blur argument?

    That last box is cute but almost invisible.

  4. anonym says:

    Why not just use “box-shadow: 0px 6px 16px -6px rgba(0, 0, 0, 0.30);” on box-container and no box-container-inner.
    It’s many more lighter in css and html than your solution and give the same result (approximately, I haven’t made calculation to find the perfect numbers)?

    So the full css is:

    .box-container {
    box-shadow: 0px 6px 16px -6px rgba(0, 0, 0, 0.30);
    background: white;
    border-radius: 10px;
    border: 1px solid #eee;
    height: 200px;
    padding: 10px;
    position: relative;
    width: 250px;
    }

    And html is:

  5. Pablo says:

    I recommend put the three boxes together at the end to compare it best 🙂

  6. Jeff McMahan says:

    It’s much better to eliminate the extra element and the border, and instead stack *multiple* shadows – CSS supports indefinitely many shadows on the same element, just separate with a comma.

    .box {
    box-shadow:
    0 0 1px 0 rgba(0,0,0,0.08),
    0 6px 12px 0 rgba(0,0,0,0.07),
    0 1px 1px 0 rgba(0,0,0,0.07);
    }

    *That’s* how you get lovely shadows.

  7. pinionlabs says:

    You can achieve this without a second element. Use the ::before or ::after pseudo-element for cleaner semantic html, since after all this is just added styling.

  8. Jeremiah Lewis says:

    A pseudo element would work just as well, without the added markup.

    .box-container::before {
    content: ”;
    position: absolute;
    display: block;
    bottom: 0;
    box-shadow: 0 4px 12px rgba(0,0,0,0.3);
    filter: blur(6px);
    height: 100%;
    top: 0;
    left: 1%;
    width: 96%;
    z-index: -1;
    }

Leave a Reply

Your email address will not be published. Required fields are marked *