How do I reduce the opacity of an element's background using CSS?
To target just the background for transparency while keeping the content opaque, you simply utilize the background-color
property with rgba
color values like so:
Making sense of rgba() and optimizing fallbacks
Decoding rgba()
rgba()
stands for "red, green, blue, alpha", with alpha specifically denoting opacity. By tweaking the alpha value, we adjust the background's level of transparency.
Fallbacks for older bros (browsers)
For the older generation of browsers that don't speak rgba
, use a solid color fallback:
Additional methods for achieving transparent backgrounds
Fashion with fashion (PNG or SVG)
rgba()
may be the go-to for colour transparency, but who said PNG or SVG images can't strut the runway too? They work perfectly in giving your website a unique texture.
Spotlights using pseudo-elements
Applying the clock-and-dagger technique of pseudo-elements is perfect for creating an overlay whilst keeping your content totally opaque:
Cascade away, my CSS warriors!
Keeping content on the clear side
To keep your content crystal clear against your semi-transparent background, you want to handle .pane
padding and .cont
margins right:
Integrity of layout: When inline meets block
To keep a smooth layout flow going with inline elements, use display:inline-block
.
And speaking of blocks, don't forget to "block" absolute background elements to full dimensions with:
IE: The browser that lived in the past
In Internet Explorer, alpha transparency is rendered uniquely as an additional pair of digits before the hex color code: #99
stands for 60% opacity.
And a pro-tip:
Was this article helpful?