How to make semitransparent layers backgrounds without affecting the text using CSS

How to make semitransparent layers backgrounds without affecting the text using CSS

if you just put the image behind your text, the text might be hard to read, so you want to also add a darker, semi-transparent overlay on top of the image, but behind the text.

Let’s say you have an image, like this crab, and you want to put it behind some text in your UI.

<div class="hero">
    <h1>This is a crab</h1>
    <p>I want an image of a crab behind this text.</p>
</div>

But, if you just put the image behind your text, the text might be hard to read, so you want to also add a darker, semi-transparent overlay on top of the image, but behind the text.

Today, let’s look at how to do that with just CSS.

Base styling #

Here’s some base styling for our element.

This adds a little bit of space on the top, bottom, and sides. It also aligns the text in the center of the element. Let’s also give it a black background with white text.

.hero {
    background-color: #000000;
    color: #ffffff;
    padding: 1em;
    text-align: center;
}

Here’s a demo.

Now, let’s go ahead and add a background image.

css

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Hire CSS Developer

Want to develop a website or re-design using CSS Development? We build a website and we implemented CSS successfully if you are planning to **[Hire CSS Developer](https://hourlydeveloper.io/hire-dedicated-css-developer/ "Hire CSS Developer")**...

Creating a CSS Visual Cheatsheet

The other day one of our students asked about possibility of having a CSS cheatsheet to help to decide on the best suited approach when doing this or that layout.

7 Best Vue CSS Component for Your App

Vue CSS frameworks are great for many reasons; code is more universally understood, web applications are easier to maintain, and prototyping becomes less of an extra step.

Learn to use variables in CSS (CSS Tricks)

whats the variable in CSS, how to declare use them. Whats the benefit of variables in CSS. Lets learn all this. #Variables #CSS #HTML #CssVariables #ITArticles

CSS Flex Box: A Flexible Way To Layout

Every element of HTML is a rectangular box. Every Box has a defined height and width. This way you can increase or decrease its size.