Placing a div on top of an image with a responsive height that adjusts to the image size using Bootstrap 4.3

I have created two layers on top of an image: one is a filter that darkens the image, and the other contains random text. I want to align them to the image with the same width and height but I'm not sure how to achieve that. I attempted to use background-image: url('...') in CSS but it did not work as expected.

When adjusting the browser window size, the alignment does not stay consistent. The layers are fixed at 632px width and height, and I am unsure how to make their height relative to the image height.

html {
  scroll-behavior: smooth;
}

body {
  background: #fff;
  font-family: 'Lato', sans-serif;
}

header {
  background: #eee;
  width: 100%;
  height: 100px;
}

ul {
  background: #eee !important;
}

header a {
  text-decoration: none;
  color: white;
}

header ul {
  list-style-type: none;
}

ul {
  background: #fff;
  margin-top: 10px;
  display: flex;
  font-size: 85%;
  width: 100%;
  padding-left: 0;
}

ul>li {
  display: flex;
  justify-content: center;
  align-items: center;
}

a {
  outline: none;
}

p {
  font-size: 115%;
}


/*header a*/

.nav-item a {
  display: inline-block;
  color: black;
  width: 200px;
  padding: 25px 0px;
  text-align: center;
}

.nav-item a:hover {
  text-decoration: none;
  color: #000000 !important;
  background: #d1de4c;
}

.header-container {
  padding-top: 6px;
}

.container-fluid {
  padding-left: 0;
}

.presentation {
  width: 100%;
  height: 632px;
}

.img-content {
  z-index: 2;
  width: 100%;
  height: 100%;
  position: absolute;
  top: 0;
  left: 0;
}

.img-filtr {
  z-index: 1;
  background: black;
  opacity: 0.45;
  width: 100%;
  height: 100%;
  position: absolute;
  top: 0;
  left: 0;
}

.nav-item.active>a {
  color: #d1de4c;
}

.col-12 {
  padding-right: 0;
}
<!DOCTYPE html>
<html>

<head>
  <link href="https://fonts.googleapis.com/css?family=Lato:400,400i,700&amp;subset=latin-ext" rel="stylesheet">
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  <!-- Bootstrap CSS -->
  <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
  <link rel="stylesheet" href="style.css">
</head>

<body id="home" data-target=".navigation" data-offset="0" data-spy="scroll">

  <div class="container-fluid">
    <div class="row">
      <div class="col-12">
        <header class="header-container sticky-top">
          <nav class="navbar navbar-expand-lg">
            <a class="navbar-brand" href="#home"><img src="https://i.sstatic.net/Fzk7J.png" class="img-fluid"></a>
            <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
                                            <span class="navbar-toggler-icon"></span>
                                        </button>

            <div class="collapse navbar-collapse" id="navbarSupportedContent">
              <ul class="navbar-nav mr-auto">
                <li class="nav-item active">
                  <a class="nav-link" href="#home">STRONA GŁÓWNA <span class="sr-only">(current)</span></a>
                </li>
                <li class="nav-item">
                  <a class="nav-link" href="#firma">O FIRMIE</a>
                </li>
                <li class="nav-item">
                  <a class="nav-link" href="#oferta">OFERTA</a>
                </li>
                <li class="nav-item">
                  <a class="nav-link" href="#etapy">ETAPY SPRZĄTANIA</a>
                </li>
                <li class="nav-item">
                  <a class="nav-link" href="#opinie">OPINIE</a>
                </li>
                <li class="nav-item">
                  <a class="nav-link" href="#kontakt">KONTAKT</a>
                </li>
              </ul>
            </div>
          </nav>
        </header>
      </div>
    </div>

    <div class="row">
      <div class="col-12">
        <div class="presentation">
          <div class="img-content">Some text, styling not finished</div>
          <div class="img-filtr"></div>
          <img style="z-index:0;" src="https://i.sstatic.net/PNDW6.jpg" class="img-fluid img-in-placeholder">
        </div>
      </div>

    </div>
  </div>

  <!-- Optional JavaScript -->
  <!-- jQuery first, then Popper.js, then Bootstrap JS -->
  <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
  <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
</body>

</html>

On full screen, the text and filter layers may not align perfectly: https://i.sstatic.net/jlaiE.jpg

Adjusting the window aspect ratio can further distort the alignment: https://i.sstatic.net/UtOyL.jpg

Answer №1

Set the height of presentation to 100% and apply position: relative to it - this will allow the absolutely positioned child to be positioned relative to this element - check out the demo below:

html {
  scroll-behavior: smooth;
}

body {
  background: #fff;
  font-family: 'Lato', sans-serif;
}
/* More CSS styles here */

.presentation {
  width: 100%;
  height: 100%; /* ADDED */
  position: relative; /* ADDED */
}

.img-content {
  z-index: 2;
  width: 100%;
  height: 100%;
  position: absolute;
  top: 0;
  left: 0;
}

.img-filtr {
  z-index: 1;
  background: black;
  opacity: 0.45;
  width: 100%;
  height: 100%;
  position: absolute;
  top: 0;
  left: 0;
}
/* More CSS styles here */
<!DOCTYPE html>
<html lang="en">
<head>
  <link href="https://fonts.googleapis.com/css?family=Lato:400,400i,700&amp;subset=latin-ext" rel="stylesheet">
  <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
</head>
<body>

  <div id="home" data-target=".navigation" data-offset="0" data-spy="scroll">
    <div class="container-fluid">
      <div class="row">
        <div class="col-12">
          <header class="header-container sticky-top">
            <nav class="navbar navbar-expand-lg">
              <a class="navbar-brand" href="#home"><img src="https://i.sstatic.net/Fzk7J.png" class="img-fluid"></a>
              <!-- Navbar content -->
            </nav>
          </header>
        </div>
      </div>
      <div class="row">
        <div class="col-12">
          <div class="presentation">
            <div class="img-content">Some text, styling not finished</div>
            <div class="img-filtr"></div>
            <img style="z-index:0;" src="https://i.sstatic.net/PNDW6.jpg" class="img-fluid img-in-placeholder">
          </div>
        </div>
      </div>
    </div>
  </div>

  <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
  <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>

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

Creating a binary tree in vanilla JavaScript and styling it with HTML and CSS

I'm facing a challenge with my homework. I am required to convert my JavaScript binary tree into HTML and CSS, strictly using vanilla JavaScript without any HTML code. I have the tree structure and a recursive function that adds all the tree elements ...

Conceal all components on a webpage with the exception of a designated div

I am looking to selectively hide elements on the page, revealing only the contents of div.k1. The page contains numerous elements. How can I achieve this using just CSS? <div>1-this will be hidden</div> <div class="k1"> 2-this div wi ...

Align pictures in the middle of two divisions within a segment

This is the current code: HTML: <section class="sponsorSection"> <div class="sponsorImageRow"> <div class="sponsorImageColumn"> <img src="img/kvadrat_logo.png" class="sponsorpicture1"/> </div& ...

Is your CSS not reflecting changes in Node.js?

I am having trouble getting the CSS to update in the browser using Browser Sync via Gulp on Node.js. What could be the issue? Below is my Gulp.js file: var themename = 'playtest haha 1'; var gulp = require('gulp'), // Prepare ...

Navigate to a specific element using jQuery

I am faced with a challenge involving a lengthy list of nested div elements. The ID of a specific paragraph element is passed in the query string to open its corresponding div and parent upon loading the page. However, due to the extensive nature of the li ...

What is the best way to display a link in an iframe across the entire browser tab?

My goal is to enhance the consistency of my website's subpages by placing the header in an iframe-element and styling it with CSS. However, I encountered an issue where clicking on a header link only loads the new page within the iframe. Is there a so ...

Dynamic header feature that maintains the aspect ratio of the logo image

Currently, I'm faced with the challenge of fixing the main grid on a website that I am currently working on. My objective is to minimize the use of media queries as much as possible. I am looking to ensure that the Logo Image maintains the height of ...

Having trouble displaying the output on my console using Node.js

Hey there, I'm new to this community and also new to the world of nodejs technology. I have encountered a problem that may seem minor to you but is quite big for me. Here's what's going on: In my code snippet, I want a user to input 3 value ...

Adding the children of JSON objects to every individual div element

How can I add each day's players [day-1, day-2, day-3, day-3] wrapped in a <span> into the .card-body section as required? The goal is to load only four card-body. var vehicles = { "day-1": { "player-1-1": "Ford", "player-1-2": "BMW ...

How can you activate or deactivate Bootstrap Checkboxes using buttons in Angular 9?

I've been working on developing a page in Angular, although I'm still getting the hang of it. Despite spending several hours and going through numerous threads trying to find a solution, I haven't been able to address my specific issue. Jus ...

Apply the :after pseudo-class to the specified element

I am struggling with implementing an overlay effect on two images on my website. My goal is to add an overlay when the images are clicked, and I have tried creating this effect using the after pseudo class. Unfortunately, the overlay does not seem to wor ...

JavaScript and AJAX: Dynamically Create URLs

I don't have any experience with web development, so could you please explain in detail? If my questions are unclear, feel free to ask for more information. Imagine I have a webpage with the following URL: www.mycompany.com/category1 This page has ...

I am facing an issue with my JavaScript variable that keeps changing temporarily. How can I ensure that its value remains permanent? Console log

I recently made some edits to the p tags, but only one of them appeared to have changed. Upon trying to display the changes again, I couldn't see any difference. Here's the code snippet that I'm working with: $(document).ready(function() ...

A step-by-step guide to triggering a click event using CSS

This code snippet is working as expected: .rightone ul { list-style: none; padding: 0px; margin: 0px; } .rightone ul li { display: block; position: relative; float:right; } .rightone li ul { display: none; } .rightone ul li a ...

Unable to See Success Notification on First Attempt

I am facing an issue with displaying a message when adding a new record. The first time I add a record, the message shows up correctly. However, if I try to add another record, the message does not appear even though the record is added successfully. Here ...

PhoneGap nested executeSql not functioning properly on iPad Simulation

My application seems to be working perfectly in Win-Safari, but encounters issues when wrapped by PhoneGap. Below is the code snippet: // working db connection db.transaction(function(tx) { tx.executeSql('SELECT * FROM tbl', [], function(tx, r ...

Issue with maplace.js preventing the display of the Google map

I have followed the setup instructions found at . After loading the maplace.js file on my server, I incorporated the provided code: <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>minimal maplace</title> < ...

Internet Explorer 11 fails to interpret the attribute attr.xlink:href

I am a beginner in Angular 2 and I'm working on creating an icon component. The code I have below works perfectly in Chrome and Firefox, but unfortunately, it's not functioning in Internet Explorer 11. Here is what my component looks like: @Com ...

What is the reason my function is only operating with a single ID?

I'm attempting to create color-changing "squares" that change color when clicked. The first square changes color correctly, but when I click on the others, the color change only happens on the first square. var image_tracker = 'red'; fu ...

Inspect every div and perform an action if the criteria are met

I need assistance with adding text inside a specific div based on certain conditions. Currently, all the div elements are being populated with the added text. Please review my code for more clarity on what I am trying to achieve. Can you suggest the most e ...