Is Vendor Prefixing Dead?

Let‘s take a quick stroll down memory-lane to revisit how vendor prefixing CSS properties came to be. I hope I don’t trigger PTSD for anyone!

It‘s not clear who started prefixing, or when it began exactly. What is clear, is that by 2006, prefixed features were in Internet Explorer and Firefox. The raison d’être of prefixes was to specify browser-specific features. It was seen as a way to implement non-standard features and offer previews of new standard features.

By 2012, the W3C CSS Working Group was issuing guidance on the use of vendor prefixes:

In CSS we use vendor prefixes for properties, values, @-rules that are: – vendor specific extensions (per CSS 2.1), or – experimental implementations (per CSS Snapshot 2010) (e.g. in Working Drafts)

It became an industry norm. The number of prefixes grew, and with it, things grew confusing. It led to partial implementations of CSS features, introduced bugs, and ultimately created a fracture in the browser ecosystem, which disgruntled developers. Developers responded by making tools to automate the problem away.

Browser vendors slowly began to move away from prefixing, favoring feature flags inside the browser settings instead. It appeared that the problems created by vendor prefixes would fade away in time. The question is: has that time come yet?

I did some analysis on the caniuse dataset and Mozilla Developer Network Compat dataset to answer this question.

You can see the trend of the implementation of prefixed features across the major browsers in the chart above. I have excluded Chrome for Android because of insufficient data.

From 2007 until 2011, there was a steady increase in the numbers of prefixed features in all browsers. Internet Explorer only saw an uptick in 2011. Then, in 2012, Mozilla began to remove prefixed features — such as -moz-border-radius* and -moz-box-shadow — from Firefox. Thereafter, they consistently removed prefixed properties once the standard version of that property was fully implemented.

In 2013, Mozilla started to make features available behind feature flags (pref flags). That same year, Chrome switched its rendering engine from WebKit to Blink (part of the Chromium project). This was a big turning point in removing some prefixed features.

It was only in April 2016 that WebKit announced that it would no longer release experimental features with prefixes:

Over time this strategy has turned out not to work so well. Many websites came to depend on prefixed properties. They often used every prefixed variant of a feature, which makes CSS less maintainable and JavaScript programs trickier to write. Sites frequently used just the prefixed version of a feature, which made it hard for browsers to drop support for the prefixed variant when adding support for the unprefixed, standard version. Ultimately, browsers felt pressured by compatibility concerns to implement each other’s prefixes.

Because Safari and iOS browsers have always used the WebKit rendering engine, a consistent reduction in the number of prefixes came later to these browsers.

Microsoft was never “gung ho” on prefixing and consistently had the fewest prefixed features. In 2019, Edge switched from its own rendering engine to Blink. In a funny twist, the change actually increased the number of prefixed properties in Edge!

The table below contrasts the prefixed features in 2013 (the zenith of prefixing) with 2021.

2013 2021
Browsers
  • Chrome 31
  • Firefox 26
  • Internet Explorer 11
  • Safari 11
  • Safari iOS 7.0-7.1
  • Chrome 89
  • Edge 89
  • Firefox 88
  • Internet Explorer 11
  • Safari 14
  • Chrome Android 89
  • Safari iOS 14.0-14.5
Some prefixing required
Total features 53 35

It may be surprising to see the raw numbers. The number of features that require prefixing fell by ~33% between 2013 and 2021. That number sounds quite modest to me.

Of course, it could be misleading to focus just on numbers. The impact varies. It could be a family of properties that all require prefixing, such as animation; or it could be a feature that only has one property or value that requires a prefix, such as user-select: none. Let’s explore the actual features to understand the circumstances better, beginning by looking at what changed in that intervening period.

Twenty features were unprefixed (fully implemented across the major browsers) and three prefixed features were introduced (backdrop-filter, CSS text-orientation, and CSS initial-letter).

In my opinion, the most notable features that are unprefixed now, which were significant pain points are:

  1. CSS Flexible Box Layout Module
  2. CSS3 Box Sizing
  3. CSS Animation
  4. CSS3 2D Transforms
  5. CSS3 3D Transforms
  6. CSS Filter Effects

The other 14 features are less prominent:

  1. :any-link
  2. ::placeholder
  3. ::selection
  4. :focus-visible
  5. :is()
  6. :read-only
  7. :read-write
  8. font-feature-settings
  9. text-align-last
  10. writing-mode
  11. CSS grab and grabbing cursors
  12. CSS Logical Properties (will be used a lot more in the future, now that support is better)
  13. CSS3 zoom-in and zoom-out cursors
  14. CSS3 Multiple Column Layout

If you choose not to support Internet Explorer 11 in 2021, then an additional seven features no longer require prefixing. That reduces the number of features that require prefixing in 2021 to 28, which is a 46% reduction since 2013.

Prefixing in 2021

Let‘s look at the properties that require prefixing. It’s a motley group!

# Name Properties/Values Description Prefix required Unprefixed support Prefixed support Improvement with prefixes
1 appearance appearance Defines how elements (particularly form controls) appear by default. Setting the value to none causes in the element’s default appearance being entirely redefined using other CSS properties.
  • Safari
    (-webkit)
  • Opera
    (-webkit)
  • Android Browser 2.1-4.4.4
    (-webkit)
69.80% 97.03% 27.23%
2 background-clip-text background-clip: text Non-standard method of clipping a background image to the foreground text.
  • Chrome
    (-webkit)
  • Edge
    (-webkit)
  • Safari
    (-webkit)
  • Opera
    (-webkit)
  • Safari iOS
    (-webkit)
  • Android Browser
    (-webkit)
  • Chrome for Android
    (-webkit)
3.89% 96.65% 92.76%
3 backdrop-filter backdrop-filter Method of applying filter effects (like blur, grayscale or hue) to content or elements below the target element.
  • Safari
    (-webkit)
  • Safari iOS
    (-webkit)
70.20% 88.20% 18.00%
4 background-image: crossfade() background-image: crossfade() Image function to create a “crossfade” between images. This allows one image to transition (fade) into another based on a percentage value.
  • Chrome
    (-webkit)
  • Edge
    (-webkit)
  • Opera
    (-webkit)
  • Android Browser
    (-webkit)
  • Chrome for Android
    (-webkit)
17.77% 92.62% 74.85%
5 background-image: image-set() background-image: image-set() Method of letting the browser pick the most appropriate CSS background image from a given set, primarily for high PPI screens.
  • Chrome
    (-webkit)
  • Edge
    (-webkit)
  • Opera
    (-webkit)
  • Android Browser
    (-webkit)
  • Opera Mobile
    (-webkit)
17.77% 92.48% 74.71%
6 box-decoration-break box-decoration-break Controls whether the box’s margins, borders, padding, and other decorations wrap the broken edges of the box fragments, when the box is split by a break, like a page, column, region, or line.
  • Chrome
    (-webkit)
  • Edge
    (-webkit)
  • Safari
    (-webkit)
  • Opera
    (-webkit)
  • Safari iOS
    (-webkit)
  • Android BrowserOpera Mobile
    (-webkit)
6.39% 97.17% 90.78%
7 clip-path clip-path Method of defining the visible region of an HTML element using SVG or a shape definition.
  • Safari
    (-webkit)
72.00% 96.33% 24.33%
8 color-adjust color-adjust A non-standard CSS extension that can be used to force background colors and images to print.
  • Chrome
    (-webkit-print-color-adjust)
  • Edge
    (-webkit-print-color-adjust)
  • Safari
    (-webkit-print-color-adjust)
  • Opera
    (-webkit-print-color-adjust)
  • Android Mobile
    (-webkit-print-color-adjust)
3.69% 39.77% 36.08%
9 element() background: element() This function renders a live image generated from an arbitrary HTML element
  • Firefox
    (-moz)
0.00% 4.04% 4.04%
10 font-kerning font-kerning Controls the usage of the spacing between lettersstored in the font. Note that this only affects OpenType fonts with kerning information, it has no effect on other fonts.
  • Safari iOS
    (-webkit)
81.73% 96.03% 14.30%
11 font-smooth font-smooth Controls how anti-aliasing is applied when fonts are rendered. Though present in early 2002 drafts of the CSS3 Fonts specification, this property has since been removed and is currently not on the standard track.
  • Chrome
    (-webkit-font-smoothing)
  • Edge
    (-webkit-font-smoothing)
  • Firefox
    (-moz-osx-font-smoothing)
  • Safari
    (-webkit-font-smoothing)
  • Opera
    (-webkit-font-smoothing)
0.00% 39.64% 39.64%
12 hyphens hyphens Method of controlling when words at the end of lines should be hyphenated.
  • Internet Explorer 10+
    (-ms)
  • Safari
    (-webkit)
  • Safari iOS
    (-webkit)
76.49% 96.51% 20.02%
13 initial-letter initial-letter Method of creating an enlarged cap, including a drop or raised cap, in a robust way.
  • Safari
    (-webkit)
  • Safari iOS
    (-webkit)
0.00% 18.00% 18.00%
14 line-clamp line-clamp Contains text to a given amount of lines when used in combination with display: -webkit-box. Any text that overflows the space produces anellipsis when text-overflow: ellipsis is included.
  • Chrome
    (-webkit)
  • Edge
    (-webkit)
  • Firefox
    (-webkit)
  • Safari
    (-webkit)
  • Opera
    (-webkit)
  • Safari iOS
    (-webkit)
  • Android Browser
    (-webkit)
  • Chrome for Android
    (-webkit)
  • Firefox for Android
    (-webkit)
  • Opera Mobile
    (-webkit)
0.19% 96.28% 96.09%
15 position: sticky position: sticky Keeps elements positioned as”fixed” or”relative” depending on how it appears in the viewport. As a result the element is “stuck” in place while scrolling.
  • Safari 7.1-12.1
    (-webkit)
93.50% 95.36% 1.86%
16 tab-size tab-size Method of customizing the width of the Tab character. Only effective alongside white-space: pre or white-space: pre-wrap.
  • Firefox 4+
    (-moz)
  • Firefox for Android
    (-moz)
  • Opera 11.5-12-1
    (-o)
92.33% 97.38% 5.05%
17 text-decoration styling text-decoration
text-decoration-* properties.
Method of defining the type, style and color of lines in the text-decoration property. These can be defined as a shorthand (e.g. text-decoration: line-through dashed blue) or as single properties (e.g. text-decoration-color: blue).
  • Firefox 6-35
    (-moz)
  • Safari 7.1+
    (-webkit)
  • Safari for iOS 8+
    (-webkit)
80.25% 94.86% 14.61%
18 text-emphasis styling text-emphasis
text-emphasis-* properties
Method of using small symbols next to each glyph to emphasize a run of text, commonly used in East Asian languages. The text-emphasis shorthand property, and its text-emphasis-style and text-emphasis-color constituent properties can be used to apply marks to the text. The text-emphasis-position property, which inherits separately, allows setting theposition ofemphasis marks with respect to the text.
  • Chrome 25+
    (-webkit)
  • Edge 79+
    (-webkit)
  • Opera 15+
    (-webkit)
  • Android Browser
    (-webkit)
  • Chrome for Android
    (-webkit)
  • Opera Mobile
    (-webkit)
21.96% 95.99% 74.03%
19 text-orientation text-orientation Specifies the orientation of text within a line. Current values only have an effect in vertical typographic modes (defined with the writing-mode property). Safari
(-webkit)
90.88% 94.84% 3.96%
20 text-size-adjust text-size-adjust Control if and how the text-inflating algorithm is applied to the textual content of the element it is applied to.
  • Edge 12-18
    (-ms)
  • Safari iOS
    (-webkit)
  • Firefox for Android
    (-moz)
72.75% 87.48% 14.73%
21 user-select: none user-select Method of preventing text or element selection.
  • Internet Explorer 10-11
    (-ms)
  • Safari
    (-webkit)
  • Safari iOS
    (-webkit)
  • Android Browser 2.1-4.4.4
    (-webkit)
74.81% 96.49% 21.68%
22 CSS Canvas Drawings background: -webkit-canvas(mycanvas) Method of using HTML5 Canvas as a background image. Not currently part of any specification.
  • Safari
    (-webkit)
0.00% 19.40% 19.40%
23 CSS Masks mask mask-* properties Method of displaying part of an element, using a selected image as a mask.
  • Chrome
    (-webkit)
  • Edge
    (-webkit)
  • Safari
    (-webkit)
  • Safari iOSAndroid Browser
    (-webkit)
  • Chrome for Android
    (-webkit)
  • Opera Mobile
    (-webkit)

Partial support in WebKit/Blink browsers refers to supporting the mask-image and mask-box-image properties, but lacking support for other parts of the spec.

4.18% 96.93% 92.75%
24 CSS Reflections -webkit-box-reflect Method of displaying a reflection of an element.
  • Chrome
    (-webkit)
  • Edge
    (-webkit)
  • Safari
    (-webkit)
  • Opera
    (-webkit)
  • Safari iOS
    (-webkit)
  • Android Browser
    (-webkit)
  • Chrome for Android
    (-webkit)
  • Opera Mobile
    (-webkit)
0.00% 91.20% 91.20%
25 CSS Scrollbar Styling scrollbar-color
scollbar-width
Methods of styling scrollbar color and width.
  • Chrome
    (-webkit)
  • Edge
    (-webkit)
  • Safari
    (-webkit)
  • Opera
    (-webkit)
  • Safari iOS
    (-webkit)
  • Android Browser
    (-webkit)
  • Chrome for Android
    (-webkit)
4.28% 96.87% 92.59%
26 CSS Text Fill & Stroking text-stroke
text-stroke-* properties
Method of declaring the outline (stroke) width and color for text.
  • Chrome
    (-webkit)
  • Edge
    (-webkit)
  • Firefox
    (-webkit)
  • Safari
    (-webkit)
  • Opera
    (-webkit)
  • Safari iOS
    (-webkit)
  • Android Browser
    (-webkit)
  • Chrome for Android
    (-webkit)
  • Firefox for Android
    (-webkit)
  • Opera Mobile
    (-webkit)
0.00% 96.65% 96.65%
27 Intrinsic & Extrinsi Sizing max-content
min-content
fit-content
stretch (formerly fill).
Allows for the height and width of an element to be specified in intrinsic values rather than fixed numeric values.
  • Chrome 22-45
    (-webkit)
  • Edge 3-65
    (-webkit)
  • Firefox (-moz-available)
  • Opera 15-32
    (-webkit)
  • Safari 6.1-10-1
    (-webkit)
  • Safari iOS 7-13.7
    (-webkit)
  • Androind Browser 4.4-4.4.4
    (-webkit)
91.99% 96.36% 4.37%
28 Media Queries: Resolution Feature @media (min-resolution: 300dpi) { … }, @media (max-resolution: 300dpi) { … } Allows a media query to be set based on the device pixels used per CSS unit. While the standard uses min-resolution and max-resolution, some browsers support the older non-standard device-pixel-ratio media query.
  • Chrome 4-28
    (-webkit)
  • Safari 4+
    (-webkit)
  • Opera 10-11.5
    (-webkit)
  • Safari iOS
    (-webkit)
  • Android Browser (2.3-4.3) Opera Mobile 12
    (-webkit)
  • Firefox 3.5-15
    (min--moz-device-pixel-ratio)

Browsers that support -webkit support the non-standard -webkit-min-device-pixel-ratio and webkit-min-device-pixel-ratio.

80.40% 99.16% 18.76%

After putting this list together, my initial impression was that these aren’t things that I would bump into very often. Some properties have not been — and probably will not be — fully implemented. I’d say the element() function and CSS Canvas Drawings fall into that category. Safari recently dropped prefixing for position: sticky, so that will likely drop off the list very soon.

You can winnow the list down and steer away from certain situations if you want to. You can dismiss it and say it’s not important, so why bother? The reality is that the list is still long enough that manually managing prefixes in your code is not something you want to take on. A pertinent question to answer is: do you want to improve cross-browser support to a high level? If the answer is yes, then you should consider this as part of your development effort.

It is also important to remember that it is not just about these properties and current browsers. There are still people out there using older devices with older browsers, which do not support the unprefixed versions of some features. Take the animation property, for example. Chrome was the last browser to unprefix it in 2015. Yet, today, 1.3% of the users worldwide are still using a browser that does not support the unprefixed version.

I have bumped into a couple of situations recently that required prefixed properties. For example, I was making a reading progress bar for a blog and needed to use -webkit-appearance: none; and -moz-appearance: none; to reset the default styling for the progress element. It also needed sticky positioning, so I had to prefix position: sticky to support sticky positioning in Safari.

Another example? I wanted to use a PNG image as a mask image for a Christmas-themed design and found that -webkit-mask-image is the only property that works properly. Masking is generally a bit erratic because most browsers only partially support the spec.

Here’s yet another: Flavio Copes, in “How to apply padding to multiple lines in CSS,” wrote about how he wanted to have the same padding on each line of a multi-line heading. The solution was to use box-decoration-break: clone. Most browsers require the -webkit prefixed version of this property, so you need to use this.

Tools

Some of the tools that were created to solve issues with prefixing and browser support have fallen by the wayside. I would recommend checking first to see if a tool is up-to-date before using it.

Certainly, Autoprefixer (a PostCSS plugin) is maintained and it uses data straight from caniuse to stay current.

Emmet also has great prefixing capabilities. Specifically, it has a css.autoInsertVendorPrefixes preference to automatically insert prefixes for you. I haven’t verified if it is current or not, but it is worth considering as part of your development environment.

Since most code editors support Emmet, it makes editing prefixed properties a lot easier. Emmet has a CSS reflect value command that updates the value of all prefixed versions of the same property in a rule. You can read the Emmet docs for more info about the prefixing capabilities.

Conclusion

Vendor prefixing is not dead, unfortunately. We are still living with the legacy. At the same time, we can be grateful that prefixed features are on a steady decline. Some good work has been done by browser vendors to implement unprefixed features in lieu of prefixed features. This has removed the brunt of the burden from developers.

However, you may bump into scenarios that require prefixes still from time to time. And if you want to support as many browsers as possible, you should continue with an auto-prefixing strategy.


The post Is Vendor Prefixing Dead? appeared first on CSS-Tricks.

You can support CSS-Tricks by being an MVP Supporter.

Getting Deep into Shadows

Let’s talk shadows in web design. Shadows add texture, perspective, and emphasize the dimensions of objects. In web design, using light and shadow can add physical realism and can be used to make rich, tactile interfaces.

Take the landing page below. It is for cycling tours in Iceland. Notice the embellished drop shadow of the cyclist and how it creates the perception that they are flying above not only the content on the page, but the page itself, as though they are “popping” over the screen. It feels dynamic and immediate, which is perfect for the theme of adventure.

A view of a BMX biker from above leaping over a vast area of light brown land with the words free ride along the bottom edge in large, bold, and white block lettering.
Credit: Kate Hahu

Compare that with this next example. It’s a “flat” design, sans shadows. In this case, the bike itself is the focal point. The absence of depth and realism allows the bike to stand out on its own.

Screenshot of a webpage with a light pink background with a white box that contains the site content with a headline that reads "Ride as Much or as Little" in red, an email subscription form, and a large image of a red and white bicycle to the right.
Credit: saravana

You can appreciate the differences between these approaches. Using shadows and depth is a design choice; they should support the theme and the message you want the content to convey.

Light and shadows

As we just saw, depth can enhance content. And what exactly makes a shadow? Light!

It’s impossible to talk about shadow without getting into light. It controls the direction of a shadow as well as how deep or shallow the shadow appears. You can’t have one without the other.

Google’s Material Design design system is a good example of employing light and shadows effectively. You’ve certainly encountered Material Design’s aesthetics because Google employs it on nearly all of its products.

A masonry grid of photos on a mobile screen. The purple header that contains the page title and hamburger menu has a shadow along its bottom edge that separates it from the white background of the photos.
White modal with a list of checkboxes. The modal has an underlying black shadow against the background.
A two-by-three grid of cards on a mobile screen. Each card has a light shallow shadow against a white background.

The design system takes cues from the physical world and expresses interfaces in three-dimensional space using light, surfaces, and cast shadows. Their guidelines on using light and shadows covers this in great detail.

In the Material Design environment, virtual lights illuminate the UI. Key lights create sharper, directional shadows, called key shadows. Ambient light appears from all angles to create diffused, soft shadows, called ambient shadows.

Shadows are a core component of Material Design. Compare that with Apple’s Human Interface Guidelines for macOS, where translucency and blurring is more of a driving factor for evoking depth.

Screenshot of Apple's Reminders app on a desktop. The left column that contains search and navigation is opaque and blends lightly into the desktop background while the solid white right column contains a checkbox list of reminders.
Screenshot of Apple's Maps app. The left column contains the map addresses and different route options with an opaque background that lightly blends in with the desktop background. The right column contains the map and does not blend in with the background.

In this case, light is still an influential factor, as it allows elements to either blend into the desktop, or even into other panels in the UI. Again, it’s is a design choice to employ this in your interface. Either way, you can see how light influences the visual perception of depth.

Light sources and color

Now that we understand the relationship between light and shadows, we ought to dig in a little deeper to see how light affects shadows. We’ve already seen how the strength of light produces shadows at different depths. But there’s a lot to say about the way light affects the direction and color of shadows.

There are two kinds of shadows that occur when a light shines on an object, a drop shadow and a form shadow.

Photo of an orange with light shining on it from the top right. That area is brighter than the left side which is covered in shadow. The ground contains a light reflection of the orange.

Drop shadows

A drop shadow is cast when an object blocks a light source. A drop shadow can vary in tone and value. Color terminology can be dense and confusing, so let’s talk about tone and value for a moment.

Tone is a hue blended with grey. Value describes the overall lightness or darkness of a color. Value is a big deal in painting as it is how the artist translates light and object relationships to color.

Illustration showing the effects of Hue, Tint, Tone, and Shade on red rectangles. Each rectangle is a slightly different shade of red where tint adds white, tone adds gray and shade adds black.

In the web design world, these facets of color are intrinsic to the color picker UI.

Form shadows

A form shadow, on the other hand, is the side of an object facing away from the light source. A form shadow has softer, less defined edges than a drop shadow. Form shadows illustrate the volume and depth of an object.

The appearance of a shadow depends on the direction of light, the intensity of light, and the distance between the object and the surface where the shadow is cast. The stronger the light, the darker and sharper the shadow is. The softer the light, the fainter and softer the shadow is. In some cases, we get two distinct shadows for directional light. The umbra is where light is obstructed and penumbra is where light is cast off.

Two vertically stacked illustrations.The top is a green circle with a yellow light source coming at it from the left and both umbra and penumbra shadows are cast to the right. The bottom illustration is the same green circle and light source, but with a solid black shadow cast to the right.

If a surface is close to an object, the shadow will be sharper. If a surface is further away, the shadow will be fainter. This is not some abstract scientific stuff. This is stuff we encounter every day, whether you realize it or not.

This stuff comes up in just about everything we do, even when writing with a pencil.

Light may also be reflected from sides of an object or another surface. Bright surfaces reflect light, dark surfaces absorb light.

These are the most valuable facets of light to understand for web design. The physics behind light is a complex topic, I have just lightly touched on some of it here. If you’d like to see explicit examples of what shadows are cast based on different light sources, this guide to drawing shadows for comics is instructive.

Positioning light sources

Remember, shadows go hand-in-hand with light, so defining a light source — even though there technically isn’t one — is the way to create impressive shadow effects. The trick is to consistently add shadows relative to the light source. A light source positioned above an element will cast a shadow below the element. Placing a light source to the left of an element will cast a shadow to the right. Placing multiple light sources to the top, bottom, left and right of an element actually casts no shadow at all!

Showing two browser mockups side by side. The left has light shining on it from all four directions showing uniform light and no shadows. The right has a single light source from the top casting a shadow along the bottom edge.

A light source can be projected in any direction you choose. Just make sure it’s used consistently in your design, so the shadow on one element matches other shadows on the page.

Elevation

Shadows can also convey elevation. Once again, Material Design is a good example because it demonstrates how shadows are used to create perceived separation between elements.

Showing a mobile screen flat on a light blue background with header, box, and navigational elements elevated over the screen showing depth.
Credit: Nate Wilson

Inner shadows

Speaking of elevation, the box-shadow property is the only property that can create inner shadows for a sunken effect. So, instead of elevating up, the element appears to be pressed in. That’s thanks to the inset keyword.

That good for something like an effect where clicking a button appears to physically press it.

It’s also possible to “fake” an inner text shadow with a little trickery that’s mostly supported across browsers:

Layering shadows

We’re not limited to a single shadow per element! For example, we can provide a comma-separated list of shadows on the box-shadow property. Why would we want to do that? Smoother shadows, for one.

Interesting effects is another.

Layering shadows can even enhance typography using the text-shadow property.

Just know that layering shadows is a little different for filter: drop-shadow() It’s syntax also takes a list, but it’s space-separated instead of comma-separated.

.box {
  box-shadow: 
    0 2px 2px #555, /* commas */
    0 6px 5px #777,
    0 12px 10px #999
  ;
}

.box {
  filter:
    drop-shadow(0 2px 2px #555) /* spaces */
    drop-shadow(0 6px 5px #777)
    drop-shadow(0 12px 10px #999);
}

Another thing? Shadows stack on top of one another, in the order they are declared where the top shadow is the first one in the list.

Two vertically stacked examples showing a white circle with a yellow and a grey circle behind it and the CSS code snippets that create the effect. On the top, the gray shadow is above the yellow shadow. On the bottom, the yellow shadow is above the gray shadow.

You may have guessed that drop-shadow() works a little differently here. Shadows are added exponentially, i.e. 2^number of shadows - 1.

Here’s how that works:

  • 1 shadow = (2^1 – 1). One shadow is rendered.
  • 2 shadows = (2^2 – 1). Three shadows are rendered.
  • 3 shadows = (2^3 – 1). Seven shadows are rendered.

Or, in code:

.one-shadow {
  filter: drop-shadow(20px 20px 0 grey);
}

.three-shadows {
  filter: 
    drop-shadow(20px 20px 0 grey)
    drop-shadow(40px 0 0 yellow);
}

.seven-shadows {
  filter: 
    drop-shadow(20px 20px 0 grey)
    drop-shadow(40px 0 0 yellow);
    drop-shadow(80px 0 0 red);
}

The <feDropShadow> element works the exact same way for SVGs.

Shadows and accessibility

Here’s something for you to chew on: shadows can help improve accessibility.

Google conducted a study with low-vision participants to better understand how shadows and outlines impact an individual’s ability to identify and interact with a component. They found that using shadows and outlines:

  • increases the ease and speed of finding a component when scanning pages, and
  • improves one’s ability to determine whether or not a component is interactive.

That wasn’t a wide-ranging scientific study or anything, so let’s turn around and see what the W3C says in it’s guidelines for WCAG 2.0 standards:

[…] the designer might darken the background behind the letter, or add a thin black outline (at least one pixel wide) around the letter in order to keep the contrast ratio between the letter and the background above 4.5:1.

That’s talking about light text on a light background. WCAG recommends a contrast ratio that’s at least 4.5:1 between text and images. You can use text shadows to add a stronger contrast between them.

Photo credit: Woody Van der Straeten

Shadows and performance

Before diving into shadows and adding them on all the things, it’s worth calling out that they do affect performance.

For example, filter: drop-shadow is hardware-accelerated by some browsers. A new compositor layer may be created for that element, and offloaded to the GPU. You don’t want to have too many layers, as it takes up limited GPU memory, and will eventually degrade performance. You can assess this in your browser’s DevTools.

Blurring is an expensive operation, so use it sparingly. When you blur something, it mixes the colors from pixels all around the output pixel to generate a blurred result. For example, if your <blur-radius> parameter is 2px, then the filter needs to look at two pixels in every direction around each output pixel to generate the mixed color. This happens for each output pixel, so that means a lot of calculations that grow exponentially. So, shadows with a large blur radius are generally slower to render than other shadows.

Did you know?

Did you know that shadows don’t influence the document layout?

A shadow is the same size as the element it targets. You can modify the size of a box-shadow (through the spread radius parameter), but other properties cannot modify the shadow size.

And did you know that a shadow implicitly has a lower z-index than elements? That’s why shadows sit below other elements.

And what about clipping and masking? If an element with a box-shadow is clipped (with clip-path) or uses a mask (with mask), the shadow isn’t shown. Conversely, if an element with text-shadow or filter: drop-shadow() is clipped, a shadow is shown, as long as it is within the clip region.

Here’s another: We can’t create oblique shadows (with diagonal lines) with shadow properties. That requires creating a shadow element and use a transform:skew() on it.

Oh, and one more: box-shadow follows border-radius. If an element has rounded corners, the shadow is rounded as well. In other words, the shadow mirrors the shape of the box. On the other hand, filter: drop-shadow() can create an irregular shape because it respects transparency and follows the shape of the content.

Showing two of the same card component side-by-side. They are brightly colored with a background gradient that goes from red to gold. The Nike logo is at the top, a title is below it, then a paragraph of white text beneath that. A red show with an exaggerated shadow is on both cards. The cards illustrated the difference between box shadow, which follows the boundaries of the card's edges, and drop shadow, which includes the shape of the shoe outside the card boundary.

Best use cases for different types of shadows

Practically anything on the web can have a shadow and there are multiple CSS properties and functions that create shadows. But choosing the right type of shadow is what makes a shadow effective.

Let’s evaluate the options:

  • box-shadow: This CSS property creates shadows that conform to the elements bounding box. It’s versatile and can be used on anything from cards to buttons to just about anything where the shadow simply needs to follow the element’s box.
  • text-shadow: This is a CSS property that creates shadows specifically for text elements.
  • filter: drop-shadow(): The CSS property here is filter, but what create the shadow is the drop-shadow function it accepts. What makes this type of shadow different from, say box-shadow, is that it follows the rendered shape of any element (including pseudos).
  • <feDropShadow>: This is actually an SVG element, whereas the rest are CSS properties. So, you would use this to create drop shadows directly in SVG markup.

Once you get the hang of the different types of shadows and each one’s unique shadow-creating powers, the possibilities for shadow effects feels endless. From simple drop shadows to floating elements, and even inner shadows, we can create interesting visuals that add extra meaning or value to UI.

The same goes for text shadows.

Shadows in the wild

Shadows are ubiquitous. We’re seeing them used in new and interesting ways all the time.

Have you heard the buzzword “neumorphism” floating around lately? That’s all about shadows. Here’s an implementation by Maria Muñoz:

Yuan Chuan, who makes amazing generative art, calls shadows a secret weapon in UI design:

CSS relies on existing DOM structure in the browser. It’s not possible to generate new elements other than ::before and ::after. Sometimes I really wish CSS had the ability to do so straightforwardly.

Yet, we can partially make up for this by creating various shadows and gradients entirely in CSS.

That’s why having drop-shadow is so exciting. Together with text-shadow and box-shadow we can do a lot more.

Just check out how he uses drop shadows to create intricate patterns.

Yes, that’s pretty crazy. And speaking of crazy, it’s worth mentioning that going too crazy can result in poor performance, so tread carefully.

What about pseudo-elements?

Oh yes, shadow properties are supported by the ::before and ::after pseudo-elements.

Other pseudos that respect shadows? The ::first-letter pseudo-element accepts box-shadow and text-shadow. The ::first-line pseudo-element accepts text-shadow.

Look at how Jhey Tompkins got all creative using box-shadow on pseudo elements to create animated loaders.

Animating shadows

Yes, we can make them move! The properties and function we’ve covered here are totally compatible with CSS animations and transitions. That means we can move shadows, blur shadows, expand/shrink shadows (with box-shadow), and alter the color.

Animating a shadow can provide a user with a cue that an element is interactive, or that an action has taken place. We saw earlier with our button example that an inset shadow showed that the button had been pressed. Another common animation pattern is elevating a card on hover.

If you want to optimize the animation performance, avoid animating box-shadow! It is more performant to animate drop-shadow(). But if you want the smoothest animation, a hack is the best option! Add an ::after pseudo-element with a bigger box-shadow, and animate its opacity instead.

Of course, there is a lot more you can animate. I will leave that exploration up to you!

Wrapping up

Phew, who knew there was so much to something as seemingly “simple” as CSS shadows! There’s the light source and how shadows are cast. The different types of shadows and their color. There’s using shadows for evoking depth, elevating elements and insetting them. There’s the fact that we can layer shadows on top of other shadows. And that we have a selection of CSS properties that we can use for different use cases. Then, there are the accessibility and performance implications that come with them. And, hey, animation is thing! That’s a heckuva lot!

Anyway, hopefully this broad overview gave you something new to chew on, or at the very least, helped you brush up on some concepts.


The post Getting Deep into Shadows appeared first on CSS-Tricks.

You can support CSS-Tricks by being an MVP Supporter.