Can Background Images and Opacity Be Set in One Property?
CSS allows you to set background transparency and background images separately, but how do you achieve a transparent background image?
Example Scenario:
Consider an image that would be visually overwhelming as a background if used at full opacity. You want to reduce its opacity to around 0.2 for a more subtle effect.
Traditional Approach:
Using the background-image property alone won't adjust the image's transparency.
#main { background-image: url(/wp-content/uploads/2010/11/tandem.jpg); }
Solution: Pseudo-Element Hack
To create a transparent background image, utilize a pseudo-element:
#main { position: relative; } #main:after { content : ""; display: block; position: absolute; top: 0; left: 0; background-image: url(/wp-content/uploads/2010/11/tandem.jpg); width: 100%; height: 100%; opacity : 0.2; z-index: -1; }
Explanation:
The above is the detailed content of Can CSS Combine Background Image and Opacity in a Single Property?. For more information, please follow other related articles on the PHP Chinese website!