In CSS, setting a background image and adjusting its opacity are separate tasks. However, it is possible to combine these two to create a transparent background image.
To set a background image, use the background-image property with a valid image URL. For example:
#main { background-image: url(/wp-content/uploads/2010/11/tandem.jpg); }
To specify the opacity of the background image, use the opacity property. The value ranges from 0 (fully transparent) to 1 (fully opaque).
However, directly applying opacity to the background-image property only affects the image, not the background itself. To achieve a transparent background image, we need an additional step.
To create a transparent layer over the background image, use the following CSS:
#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; }
In this CSS:
By combining these CSS techniques, you can create transparent background images with precise opacity control.
The above is the detailed content of How Can I Create Transparent Background Images with Opacity Control in CSS?. For more information, please follow other related articles on the PHP Chinese website!