Apply a tint to the specified section

I'm looking to add a semi-transparent color overlay to my HTML section, but I'm facing an issue where it doesn't cover the entire section. Here's what I have so far:

#main {
    padding-top: 50px;
    padding-bottom: 50px;
    background-image: url('https://cdn.pixabay.com/photo/2020/12/23/08/00/bow-lake-5854210_960_720.jpg');
    background-repeat: no-repeat;
    background-size: cover;
    background-position: center top;
    position: relative;
    z-index: 1;
}

#main::before {
    background-color: rgba(0, 0, 0, 0.80);
    content: '';
    height: 100%;
    position: absolute; 
    width: 100%; 
    z-index: -1;
}
<link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="27454848535453554657671309120914">[email protected]</a>/dist/css/bootstrap.min.css" rel="stylesheet"/>

<section id="main">

    <div class="container">
        <div class="row">
            <div class="col-lg-6">
                <h1>This is a text block</h1>
                <p>This will only contain text which the user can edit from the admin via a text editor. Paddings, bg color/image are controlled via css file.</p>
                <p>Lorem ipsum dolor, sit amet consectetur adipisicing elit. Hic ratione saepe quidem quo corrupti rem accusantium ex dolorem explicabo odio quae quos illo, voluptates maiores.</p>
            </div>

            <div class="col-lg-6">
                <h1>This is a text block</h1>
                <p>This will only contain text which the user can edit from the admin via a text editor. Paddings, bg color/image are controlled via css file.</p>
                <p>Lorem ipsum dolor, sit amet consectetur adipisicing elit. Hic ratione saepe quidem quo corrupti rem accusantium ex dolorem explicabo odio quae quos illo, voluptates maiores.</p>
            </div>
        </div>
    </div>

</section>

The issue here is that the color overlay isn't consistently displayed across all screen sizes due to the padding in #main. Adding margin-top to the before element helps on large screens, but not on smaller ones. How can I ensure the color overlay covers the entire section regardless of screen size and the presence of padding in #main? Thanks

Answer №1

Insert the following ruleset into #main::before:

top: 0;
left: 0;
right: 0;
bottom: 0;

Ensure to eliminate height: 100% and width: 100% as well.

Answer №2

To apply the style, simply add top: 0 to your pseudo element.

#main {
    padding-top: 50px;
    padding-bottom: 50px;
    background-image: url('https://cdn.pixabay.com/photo/2020/12/23/08/00/bow-lake-5854210_960_720.jpg');
    background-repeat: no-repeat;
    background-size: cover;
    background-position: center top;
    position: relative;
    z-index: 1;
}

#main::before {
    background-color: rgba(0, 0, 0, 0.80);
    content: '';
    height: 100%;
    position: absolute; 
    width: 100%; 
    z-index: -1;
    top: 0;
}
<link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="15777a7a61666167746555213b203b26">[email protected]</a>/dist/css/bootstrap.min.css" rel="stylesheet"/>

<section id="main">

    <div class="container">
        <div class="row">
            <div class="col-lg-6">
                <h1>This is a text block</h1>
                <p>This will only contain text which the user can edit from the admin via a text editor. Paddings, bg color/image are controlled via css file.</p>
                <p>Lorem ipsum dolor, sit amet consectetur adipisicing elit. Hic ratione saepe quidem quo corrupti rem accusantium ex dolorem explicabo odio quae quos illo, voluptates maiores.</p>
            </div>

            <div class="col-lg-6">
                <h1>This is a text block</h1>
                <p>This will only contain text which the user can edit from the admin via a text editor. Paddings, bg color/image are controlled via css file.</p>
                <p>Lorem ipsum dolor, sit amet consectetur adipisicing elit. Hic ratione saepe quidem quo corrupti rem accusantium ex dolorem explicabo odio quae quos illo, voluptates maiores.</p>
            </div>
        </div>
    </div>

</section>

Answer №3

Instead of specifying width and height, you can set the top, bottom, left, and right positions for your overlay like this:

#main {
    padding-top: 50px;
    padding-bottom: 50px;
    background-image: url('https://cdn.pixabay.com/photo/2020/12/23/08/00/bow-lake-5854210_960_720.jpg');
    background-repeat: no-repeat;
    background-size: cover;
    background-position: center top;
    position: relative;
    z-index: 1;
}

#main::before {
    background-color: rgba(0, 0, 0, 0.80);
    content: '';
    top: 0;
    bottom: 0;
    left: 0;
    right: 0;
    position: absolute;
    z-index: -1;
}
<link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="abc9c4c4dfd8dfd9cadbeb9f859e8598">[email protected]</a>/dist/css/bootstrap.min.css" rel="stylesheet"/>

<section id="main">

    <div class="container">
        <div class="row">
            <div class="col-lg-6">
                <h1>This is a text block</h1>
                <p>This will only contain text which the user can edit from the admin via a text editor. Paddings, bg color/image are controlled via css file.</p>
                <p>Lorem ipsum dolor, sit amet consectetur adipisicing elit. Hic ratione saepe quidem quo corrupti rem accusantium ex dolorem explicabo odio quae quos illo, voluptates maiores.</p>
            </div>

            <div class="col-lg-6">
                <h1>This is a text block</h1>
                <p>This will only contain text which the user can edit from the admin via a text editor. Paddings, bg color/image are controlled via css file.</p>
                <p>Lorem ipsum dolor, sit amet consectetur adipisicing elit. Hic ratione saepe quidem quo corrupti rem accusantium ex dolorem explicabo odio quae quos illo, voluptates maiores.</p>
            </div>
        </div>
    </div>

</section>

Setting the positions as mentioned will make the overlay cover its entire parent element. Alternatively, you can place the overlay in a specific corner by setting the top and left positions along with a width and height of 100%.

For more detailed documentation, check here

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Is there a way to make the delete button remove just one item from the local storage?

Is there a way to make the delete button on each item in a list remove only that specific item without deleting the others and also remove it from local storage? I have been trying to figure this out but haven't had any success so far. <div class ...

unable to display picture on puggy

Check out the code snippet below: <!DOCTYPE html> <html lang="en> <head> <meta charset="UTF-8> <title>Home Page</title> </head> <body> <img src="resources/mainlogo.png" style="width:304px;height:2 ...

Shift two tabs to the right in Qt, and keep the others on the left side

I am currently working on a layout design for my QtTabWidget and need to move two specific tabs to the right while keeping the rest of them aligned to the left. It's important to note that these tabs are static and not dynamically added during runtim ...

Alignment in rows with a flexible number of rows

My goal is to create a series of equally sized elements with consistent spacing. I want these elements to be evenly distributed both horizontally and vertically on the page, adjusting accordingly to changes in screen size or number of elements. The challen ...

Broken styles when using Material UI with babel and the 'with styles' feature

We've implemented babel and lerna to maintain specific elements of our react web app separately. While the setup has been effective thus far, we're encountering styling issues when generating the static build. The main project is not processed t ...

The appearance of a list item (<li>) changes when navigating to a different page within a master page

I encountered an issue where after logging in with a username and password, upon being redirected to the homepage, the login button reappears on all pages instead of remaining hidden until I logout. My goal is for the login button within the <ul> ele ...

Material UI - Clear all designs from the slate

Is it possible to completely override all default material-ui styles and implement your own custom style guide for components? This would involve removing all material-ui styles and starting fresh with customized html skeletons. Creating a new theme with m ...

Full height Footer using Flash and HTML

Seeking advice: I successfully implemented a Flash object with 100% height and width on an HTML page. However, I am struggling to add an HTML footer that stays fixed at the bottom of the page. Attempts made: I have tried various solutions, searched on Go ...

Creating a unique post type in Wordpress

Currently, I am in the process of converting a Bootstrap one-page template into a WordPress template. My goal is to incorporate a custom post type that will display items in the services portfolio while maintaining the same CSS styling. Here is the code sn ...

What are the indicators of JSON in a response?

When using ReactJS to make a fetch request to an API, the JSON body response includes the following: { "place": <a href=\"http:\/\/place.com\/ search?q=%23MILKYDOG\" target=\"_blank\">#milkydog<&b ...

Creating a customized icon scheduling feature with CSS

Creating an icon using CSS that looks like the one below seems to be quite challenging. I attempted to achieve it with this approach, but the result was not satisfactory. https://i.stack.imgur.com/5W7Hj.png .schedule { height: 10px; width ...

Is there a way to adjust the spacing between cards in Bootstrap?

I am attempting to replicate the layout shown in the attached picture. How can I adjust this property? Expected output: I would like the elements to be closer together and centered on the page, but I am struggling to achieve this. I have tried adjusting ...

Is it possible to use -webkit-transform with dual background layers?

I've implemented a unique animation for a div that features two semi-transparent backgrounds: /* Custom keyframes for the animated background DIV. */ @-webkit-keyframes wind { 0% { background-position: 0px 0px, 0px 0px; } 100 ...

Display the second dropdown after the user has made a selection in the first dropdown

Recently, I've been delving into the world of html/javascript and navigating through the process of implementing the code found in this specific link. While I can see the solution outlined in the link provided below, I'm struggling with how to i ...

Unable to extract text input from form

When designing a form for users to change their password, I encountered an issue where passing 3 empty Strings instead of a User object resulted in the Strings being returned as empty when submitting the form. Is there a way to retrieve String values from ...

What is causing the issue with this pseudo class selector not functioning properly?

I've been experimenting with the :after selector to create an inside-border effect in every div I hover over. However, I'm facing an issue where the ":after" pseudo-class doesn't seem to work. Any insights on why this selector isn't fun ...

Looking for Recommendations in javascript

I have created a responsive container for users with a simple filter that displays a list of users based on their Id. Current let data = [ {Id:'123',Name:'John Doe',Gender:'Male'}, {Id:'213',Name:'W ...

Tips for centering multiple images vertically within a div

Currently, I am using jQuery.carouFredSel for displaying banners. Everything is working fine except for one issue - the banners have variable heights which causes them to align at the top in the slider. I need a solution to align shorter banners in the mid ...

Utilizing HTML5's geolocation API to construct a geofence

Is there a way to utilize the HTML5 geolocation API to determine if a user is located in a specific area? I'm considering setting a central latitude and longitude and creating a radius around it, so the site will function as long as the user is within ...

Looking to modify the HTML layout in order to be compatible with a specific script

Utilizing a form validation script with required:true data-validation will stop the form from submitting and highlight invalid fields in red by adding "has-error" to the parent container when the user submits the form. In the code snippet below, "has-erro ...