What is the best way to place a div in a static position for my specific situation

I need help positioning container 2 statically below container 1. The navigation bar is fixed, so it will be outside the flow of the page while container 1 is relative and will appear on top as if the navigation doesn't exist in the flow. However, when I added container 2, it ended up being positioned at the top instead of under the entire container 1 div (below the image). Could someone please assist? Thank you

https://i.sstatic.net/VXdcj.jpg

body{
    height: 100%;
    width: 100%;
    margin:0;
}

/* I WOULD LIKE TO FIX THE NAVIGATION BAR SO THAT IT REMAINS IN PLACE AS I SCROLL DOWN */
.navigation{
    position: fixed;
    top: 0%;
    left: 0%;
    width: 100%;
    height: 50px;
    background-color: tomato;
    opacity: 0.6;
}

/* CONTAINER 1 WILL BE POSITIONED AT THE TOP OF VIEWPORT DUE TO FIXED NAVIGATION DIV */
.container1{
    position: relative;
    height: 100%;
    width: 100%;
}
/* CONTAINER 1 WILL INCLUDE AN IMAGE THAT COVERS THE ENTIRE SCREEN AS BACKGROUND */
.container1 img {
    position: absolute;
    width: 1349px;
    height: 750px;
    z-index: -1;

}

/* SOME CONTENT ON THE IMAGE ELEMENT */
.container1-desc{
    position: absolute;
    top: 200px;
    left: 120px;
}
.container1-desc h1{
    margin: 0;
    color: #FFF;
}
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="./index.css">
</head>

<body>
  <div class="main">
    <nav class="navigation"></nav>
    <div class="container1">
        <img src="./pizza-3.jpg"/>
        <div class="container1-desc"><h1>THE BEST PIZZA</h1></div>
    </div>
    <div class="container2">
        <h1>hello</h1>
    </div>
  </div>  
   
</body>

</html>

Answer №1

Eliminate the use of position in the .container1 img and .container1-desc elements.

body{
    height: 100%;
    width: 100%;
    margin:0;
}

/* I PLAN TO MAKE THE NAVIGATION BAR FIXED SO IT STAYS IN PLACE WHEN SCROLLING */
.navigation{
    position: fixed;
    top: 0%;
    left: 0%;
    width: 100%;
    height: 50px;
    background-color: tomato;
    opacity: 0.6;
}

/* CONTAINER 1 WILL BE AT THE TOP DUE TO THE FIXED POSITION OF navigation div (IT WILL BE REMOVED FROM FLOW) */
.container1{
    position: relative;
    height: 100%;
    width: 100%;
}
/* CONTAINER 1 WILL HAVE AN IMG THAT ACTS AS A BACKGROUND COVERING THE SCREEN */
.container1 img {
    width: 1349px;
    z-index: -1;

}

/* SOME CONTENT ON THE IMAGE ELEMENT */
.container1-desc{
    top: 200px;
    left: 120px;
}
.container1-desc h1{
    margin: 0;
}
<body>
  <div class="main">
    <nav class="navigation"></nav>
    <div class="container1">
        <img src="https://media.sproutsocial.com/uploads/2017/02/10x-featured-social-media-image-size.png"/>
        <div class="container1-desc"><h1>THE BEST PIZZA</h1></div>
    </div>
    <div class="container2">
        <h1>hello</h1>
    </div>
  </div>  
   

Answer №2

.container2 sits below .container1, even though .container1 doesn't have any content to occupy space (due to its image being set with position: absolute).
The image simply overflows from .container1, giving the illusion of a defined height, and creating the impression that .container2 is contained within .container1.

To address this issue, you could make the image fill as much background space in .container1 as possible and assign a specific height to the container for visibility. Additionally, consider using overflow: hidden to prevent the image from overflowing the div again.

body{margin:0}
nav {
  position: fixed;
  top: 0;
  width: 100%;
  height: 50px;
  background-color: tomato;
  opacity: 0.6;
}

main > section:nth-of-type(1) {
  position: relative;
  width: 100%;
  height: 400px;
}
main > section:nth-of-type(1) img {
  z-index: -1;
  position: absolute;
  width: 100%;
  height: 100%;
  object-fit: cover;
}
main > section:nth-of-type(1) > div {
  position: absolute;
  padding: 1rem;
  top: 300px;
  left: 60px;
  color: #FFF;
  background: rgba(0, 0, 0, 0.75);
  border-radius: 0.5rem;
}
main > section:nth-of-type(1) > div h1 {
  margin: 0;
  text-transform: uppercase;
}
<main>
  <nav></nav>
  <section>
    <img src="https://i.sstatic.net/VXdcj.jpg"/>
    <div>
      <h1>The Best Pizza</h1>
    </div>
  </section>
  <section>
    <h1>hello</h1>
  </section>
</main>

Alternatively, you could utilize the CSS property background-image to apply a background to your div.

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 new components within A-frame

I've been attempting to implement the bubble function into my scene to generate a sphere, but unfortunately nothing is showing up. Even when I try creating a sphere without using the bubble function, it still doesn't appear in the scene. func ...

How can you use a jQuery Selector to target all images and sources within dynamic containers?

I am currently attempting to identify all images and their paths (srcs) within the dynamic containers. The dynamic containers refer to container patterns stored in an array, which is filled dynamically. For example: var containers = new Array( ...

What steps are involved in converting .html files to .ejs format via terminal?

I'm currently working on a Node project and I have a bunch of HTML files that I need to convert to .ejs format. Does anyone know of a quick way to change all the .html files in my directory to .ejs using terminal commands? ...

I require assistance in implementing a button for executing this specific HTML code

Can someone assist me in embedding my HTML code into a button so that when the button is clicked, my code executes? function loadProgressBar() { var bar = document.getElementById('progressBar'); var status = document.getElementById(&ap ...

Tips for creating a static header div with a fixed size and a scrollable body

Check out the code snippet below as a reference: <div class="main"> <div class="header"> Header<br> Header<br> Header<br> Header<br> Header<br> Header<br> Header<br> Header<br> Header<br> He ...

Guide on combining vendor CSS files in a React application using Webpack

Incorporating third-party libraries is an essential part of my project. For example, I have Mapbox GL installed via npm, which comes with CSS files needed for its functionality. The Mapbox GL CSS file can be found at mapbox-gl/dist/mapbox-gl.css in the no ...

placing a vertical bootstrap button along the left border

My goal is to place a vertical bootstrap button on the left side of the screen. Currently, I have implemented the following CSS: #button1 { position: absolute !important; z-index: 10 !important; left: 0px !important; bottom: 20% !important; tr ...

Issue with Left Alignment of Tabs in Material-UI

As of now, material-ui's latest version does not provide support for aligning tabs to the left in the component. However, I came across a workaround on this GitHub page I have implemented the same workaround, and you can view it here: Unfortunately, ...

I must remove the thumb from the input range control

I am looking for a way to remove the thumb from the progress bar in my music player. I have tried various methods without success, and I simply want the progress bar to move forward with color change as it advances based on the Progress variable. For refe ...

The placement of DropLeft is incorrect

I have encountered an issue while designing a navbar with Bootstrap 4. I have placed my links on the right side of the navbar. However, when I add a dropdown to the left side of the navbar, the dropdown appears in the wrong position. Can anyone help me ide ...

Is there a way to have a content's columns adjust to the size of an image?

I'm currently struggling with a layout issue. I have an image on the left and want the content columns on the right (title, lorem...) to adjust their height to match the image. How can I achieve this? I'm using Bootstrap v5.1.1 Current image: ...

Tips for modifying HTML attributes in AngularJS

I am looking to modify an attribute of a div using AngularJS. While I am familiar with how to do this in jQuery, I am not sure how to achieve it in Angular. Here is the HTML code: <div ng-app="test"> <div ng-controller="cont"> < ...

Trouble with HTML CSS rendering compatibility across different browsers

So, I've built this page with a gradient background and three white divs acting as columns. Each column contains some text, and it displays perfectly in Google Chrome. However, the gradient appears taller in Firefox and Internet Explorer, pushing the ...

Is it possible to nest a parsys within another parsys in this context?

On my webpage, I have a paragraph system (parsys) with a component that contains two inner paragraph systems. When I try to add a rich text component to each of these inner paragraph systems, the components overlap and become uneditable. Is it possible t ...

How can we implement a function on every table using jQuery?

I have created a custom jQuery function for implementing pagination. However, I encountered an issue where when I load the function on a page with multiple tables, the pagination system gets applied to all tables. This means that the number of pages refle ...

Setting a customized background color for a designated section of a component

I am trying to create a hover effect at the position of a cursor, similar to what is shown in this example: For reference, I have also created a code snippet: https://jsfiddle.net/onnmwyhd/ Below is the code I am using: HTML <div id="container&q ...

What is the best way to create a website where images align perfectly with stacked cards?

Having trouble aligning the image on the right side of my website with two cards on the left side. Here is an example of what I'm aiming for: (https://i.sstatic.net/fBvTQ.jpg)](https://i.sstatic.net/fBvTQ.jpg) This is the current code: <!doctyp ...

Bootstrap 4 responsive card group with uniform height card footers

I've set up a responsive card layout using Bootstrap 4's card-deck class utility. Everything is functioning as expected, but I'm looking to align the top of the footers instead of having them default to bottom aligning and potentially overfl ...

Guide on uploading a 3D model in JSON format to Three JS

I am trying to import a JSON file that contains information about a 3D model for Three JS. I have the basic code structure for Three JS and also have a loader function: const loader = new THREE.ObjectLoader(); loader.load( // resource URL "mo ...

What is the best way to show a list when hovering the mouse over a

<nav> <a href="login.html" class="dropdown-content" style="text-align: center"><span id="login">您好</span><i class="login"></i></a> <a href="9.html"><i class="like"></i></a> <a href="7 ...