CSS background image opacity without affecting child elements
CSS Tutorial » CSS background image opacity without affecting child elements
Study in this chapter:
1. - explanations - How can you change the opacity of a background image in CSS?
2. - Transparent Hover Effect
3. - Transparency using RGBA
To improve the design of a web page, designers of web pages use the opacity property to specify the opacity / transparency of an itemwhen they need it.
We can add opacity property to an backgroundimage or color as:Internal Style - by using a <style> element in the <head> section
External Style - by using an external CSS file as style.css
Inline - by using the style attribute in HTML elements
Syntax:
Inline CSS.
Example:background image opacity without affecting text, color gradient
Browser Support
Element | ![]() Chrome |
![]() IE |
![]() Firefox |
![]() Opera |
![]() Safari |
opacity property | Yes | Yes | Yes | Yes | Yes |
Change background-image opacity in CSS without affecting text
How can you change the opacity of a background image in CSS? - simple, we use opacity: value;
The background-opacity CSS property you want to use only to change the opacity or transparency of a CSS element's background without affecting the child item, there is no.
<!DOCTYPE html>
<html>
<head>
<style>
.main-box{
width:99%;
height:200px;
background:#E9E9E9;
}
.box1{
background-image: url('https://www.agernic.com/uploads/image-opacity.jpg');
opacity: 0.2;
margin: 10px;
padding: 2px;
font-size:30px;
height: 160px;
width:27%;
float: left;
}
.box2{
background-image: url('https://www.agernic.com/uploads/image-opacity.jpg');
opacity: 0.5;
margin: 10px;
padding: 2px;
font-size: 30px;
height:160px;
width:27%;
float: left;}
.box3{
background-image: url('https://www.agernic.com/uploads/image-opacity.jpg');
margin: 10px;
padding: 2px;
font-size: 30px;
height: 160px;
width:27%;
float: left;
}
</style>
</head><body>
<div class="main-box">
<div class="box1">opacity 0.2</div>
<div class="box2">opacity 0.5</div>
<div class="box3">opacity 1 (default)</div>
</div>
</body>
</html>
Related subjects:
Background image size to fit screen
Images size
Rounded corners image
Transparent Hover Effect
Image Transparency
The opacity property is often used in conjunction with the :hover selector to change the opacity on mouseover.
<!DOCTYPE html>
<html>
<head>
<style>
img {
opacity: 0.5;
}img:hover {
opacity: 1.0;
}
</style>
</head>
<body><h1>Image Transparency</h1>
<p>The opacity property is often used in conjunction with the :hover selector to change the opacity on mouseover.</p>
<img src="https://www.agernic.com/uploads/image-opacity.jpg" alt="mountin" width="30%">
<img src="https://www.agernic.com/uploads/landscape_beach.jpg" alt="landscape beach" width="30%">
<img src="https://www.agernic.com/uploads/espana-imagenes.jpg" alt="Italy" width="30%"></body>
</html>
Tags: opacity without affecting text, only, gradient, black, color, darker, rgba, How to make background image lighter? Background image opacity without affecting child elements, text or content.
Transparency using RGBA
If you don't want to apply opacity to the items as in the example shown above, you can use RGBA color values. The next example sets the opacity/transparency for the background color.
<!DOCTYPE html>
<html>
<head>
<style>
div {
background: rgb(0, 0, 255);
padding: 10px;
}div.fi {
background: rgba(0, 0, 255, 0.2);
}div.se {
background: rgba(0, 0, 255, 0.4);
}div.th {
background: rgba(0, 0, 255, 0.6);
}
</style>
</head>
<body><p>Transparent Box With RGBA color values:</p>
<div class="fi"><p>20% opacity</p></div>
<div class="se"><p>40% opacity</p></div>
<div class="th">
<p>60% opacity</p></div>
<div><p>default</p></div>
</body>
</html>
The value of a color in the RGBA code is specified with: rgba (red, green, blue, beta). The beta parameter is a number between 0.0 (it is completely transparent) and 1.0 (it is completely opaque).
Summary of description
only, gradient, black, color, darker, rgba, How to make background image lighter?
CSS background image opacity without affecting child elements, text, or content - css tutorial
This tool makes it easy to create, adjust, and experiment with custom colors for the web.

Magnews2 is a modern and creative free magazine and news website template that will help you kick off your online project in style.

Find here examples of creative and unique website layouts.

Find here examples of creative and unique website CSS HTML menu.