Q:

How to change the opacity of an element\'s background without affecting the child elements or text content

0

How to change the opacity of an element\'s background without affecting the child elements or text content

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Use the CSS RGBA colors

There is no CSS property like "background-opacity" that you can use only for changing the opacity or transparency of an element's background without affecting its child elements. On the other hand if you will try to do this using the CSS opacity property, it will not only change the opacity of the background but also changes the opacity of all the child elements as well.

In such situations you can use the RGBA color introduced in CSS3 that includes alpha transparency as part of the color value. Using RGBA color you can set the color of the background as well as its transparency, as demonstrated in the following example:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>CSS Opacity Using RGBA Colors</title>
<style>
    body {
        background-image: url("/examples/images/pattern.jpg");
    }
    p{
        padding: 20px;
        background: rgba(0, 0, 0, 0.6);
        color: #fff;
        font: 18px Arial, sans-serif;
    }
</style>
</head>
<body>
    <p>Setting background transparency without affecting the text content.</p>
</body>
</html>

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

HTML / CSS Frequently Asked Questions

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
How to define or create style sheet only for Inter... >>
<< How to align a DIV horizontally center using CSS...