CSS

How to Change Background Opacity without Affecting Text

In this tutorial, we are going to see how to Change Background Opacity without Affecting Text. The background-opacity property can only be used to change the opacity or transparency of an element’s background without affecting sub-elements. However, if you try to use the opacity property, it will not only modify the opacity of the background but also modify the opacity of all the subitems.

In this case, you can use the RGBA color introduced in CSS3, which includes alpha transparency in the color value. Using RGBA color, you can set the background color as well as its transparency.
 

CSS Code:
body {
    background-image: url("https://stackhowto.com/background.jpg");
}

p {
    padding: 30px;
    color: #fff;
    font: 18px Georgia;
    background: rgba(0, 0, 0, 0.7);
}
 

HTML Code:
<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>Change Background Opacity without Affecting Text</title>
		<style type="text/css">
			/* Put the CSS Style Here */
		</style>
	</head>
	<body>
		<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
	</body>
</html>
Result

Lorem ipsum dolor sit amet, consectetur adipiscing elit.

mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

Your email address will not be published. Required fields are marked *