opacity
is a CSS property that allows you to change the opaqueness of an element. By default, all elements have a value of 1
. By changing this value closer to 0
, the element will appear more and more transparent.
A common use case is using an image as part of the background. Adjusting the opacity can improve the legibility of text or achieve the desired appearance. However, there is no way to target the background-image
of an element with opacity
without affecting the child elements.
This limitation can be particularly challenging when designing web pages that require a specific visual hierarchy or readability. For instance, you might want a background image to be less prominent so that text or other content on top of it stands out more clearly. Simply applying the opacity
property to the entire element will not suffice, as it will also make the text and other child elements transparent.
In this article, you will learn two methods to work around this limitation for background images with opacity. These methods will help you achieve the desired transparency effect on background images without compromising the visibility of the content within the element.
Deploy your frontend applications from GitHub using DigitalOcean App Platform. Let DigitalOcean focus on scaling your app.
If you would like to follow along with this article, you will need:
opacity
.position: relative
and position: absolute
.z-index
:before
and :after
pseudo-elements.The first approach will rely upon two elements. One is a “wrap” that provides a point of reference with position: relative
. The second is an img
element that appears behind the content with position: absolute
and stacking context.
Here is an example of the markup for this approach:
And here are the accompanying styles:
This markup and styles will produce a result with text on top of an image:
The parent demo-wrap
<div>
establishes an absolute positioning containing block. The demo-bg
<img>
is set to position: absolute
and assigned a slight opacity
. The demo-content
<div>
is set to position: relative
and due to how the markup is arranged it has a higher stacking context than demo-bg
. It is also possible to use z-index
for finer control over the stacking context.
There are some limitations to this approach. It assumes that your image is large enough to accomodate the size of any element. You may need to enforce size limitations to prevent an image from appearing cut off or not covering the entire height of an element. It will also require additional adjustments if you want to control the “background position” and no clean “background repeat” alternative.
The second approach will rely upon pseudo-elements. The :before
and :after
pseudo-elements are available to most elements. Typically, you would provide a content
value and use it to append extra text at the beginning or end. However, it is also possible to provide an empty string and then you can utilize the pseudo-elements for designs.
Here is an example of the markup for this approach:
And here are the accompanying styles:
This markup and styles will produce a result with text on top of an image:
The parent demo-wrap
<div>
establishes an absolute positioning containing block. The pseudo-element :before
is set to position: absolute
, assigned a slight opacity
, and uses background-size: cover
to occupy all the available space.
This approach has the advantage of support for other background
properties like background-position
, background-repeat
, and background-size
. This approach has the disadvantage of using one of the pseudo-elements which may conflict with another design effect - like a clearfix solution.
When designing web pages, it’s essential to ensure that text remains readable over background images. Overlays can be used to improve readability by adding a semi-transparent layer between the background image and the text. This technique is particularly useful for hero sections, banners, and other areas where text needs to stand out against a visually appealing background.
Hover effects can significantly enhance the user experience by providing visual cues and feedback. One common hover effect is changing the opacity of UI elements, such as buttons or icons, to indicate interactivity. By dynamically adjusting opacity on hover, designers can create a sense of depth and visual interest, making the user interface more engaging and responsive.
Hero sections and banners are critical components of modern web design, often serving as the first point of contact between the user and the website. Applying transparent backgrounds to these elements can create a sense of continuity and visual flow, allowing the background image to seamlessly integrate with the rest of the layout. This design approach can be particularly effective in creating a modern, sleek look that draws the user’s attention to the content.
To make a background image transparent in CSS, you can use the background-blend-mode
property in combination with a semi-transparent color. Here’s an example:
This will make the background image slightly transparent, allowing the background color to show through.
Yes, you can change the background image opacity without affecting the text by using a pseudo-element or a separate element for the background image. Here’s an example using a pseudo-element:
This way, the opacity of the background image is adjusted without affecting the text or other content within the element.
opacity
affects the entire element, including its content, whereas background-blend-mode
only affects the background image or color. opacity
sets the level of transparency for the entire element, making it and its content semi-transparent. On the other hand, background-blend-mode
blends the background image or color with the background color, allowing for more control over the background’s transparency and appearance.
To make only the background opaque in CSS, you can use the background-color
property with an rgba
value, setting the alpha channel to 1
(fully opaque). Here’s an example:
This sets the background color to be fully opaque, ensuring that the background is not transparent.
To make a background image transparent, you can use the background-blend-mode
property in combination with a semi-transparent color, as shown in the first FAQ. Alternatively, you can use a pseudo-element or a separate element for the background image and adjust its opacity
property, as shown in the second FAQ.
To dull the background image in CSS, you can use the filter
property to apply a blur effect. Here’s an example:
This adds a blur effect to the background image, making it appear duller. You can adjust the value of blur
to control the level of dullness.
In this article, you learned about two methods to work around this limitation for background images with opacity.
For more in-depth information on CSS, you can use the following tutorials:
Additionally, if you’d like to deep-dive into CSS, check out our learn more about CSS series for exercises and programming projects.
Continue building with DigitalOcean Gen AI Platform.
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
Hi, this was really helpful but I have one question. What is the use of the “content:’ '” line in the pseudoelement?
In some case I need to use JS to dynamically set the background image (such as from CMS) using inline style. So method 2 may not work well?
(unless if I somehow manipulate the CSS objects?)
Method 1 won’t allow you to do background repeat, positioning, multiple background easily. So how about use a div and set its background CSS (as child 1), and then the content as child 2, and set the child 1’s width and height to 100% to be the same as parent container?