Stacking of div elements is not possible when they have a position relative

To summarize, I created a container div (parent) with a relative position and then added 3 children divs with absolute positions. Now, I am attempting to add another div that should appear below all of this content, essentially representing the next section of a website. However, the next div is currently displaying under the first main "parent" div. Despite my efforts in researching on here and Google, I found that using a main div with a relative position was meant to maintain the flow, but it seems to have caused issues - hence why I'm seeking help.

My intention is to introduce another div outside of the parent so that it will be positioned below the initial div, creating a smooth-scrolling website experience. Please review my CSS code and assist me in understanding why the absolute elements within a relative element are disrupting the layout. (As someone new to CSS, any professional guidance is highly valued!)

Check out this image of the website for context

*{
    margin: 0;
    padding: 0;
    border: 0;
}

body {
    font-family: 'Noto Sans HK', sans-serif;
}

/* Arranging the parent and child elements so 
images can overlap */

.child {
    top: 0;
}
.child-1 {
    position: absolute;
    left: 0;
    z-index: 100;
}

.child-2 {
    position: absolute;
    right: 0;
    z-index: 1;
}

.child-3 {
    position: absolute;
    padding-top: 38%;
    left: 0;   
}
#parent {
    position: relative;
    height: auto;       
}

.hero-text {
    position: absolute;
    text-align: center;
    right: 10vw;
    top: 28vw;
    z-index: 9;
    font-size: 3.5vw;
    float: right;
    color: white;
}

/* Responsive viewport area, 
Logo resize based on the screen size */

#logo_png {
    max-width: 25vw;
}

#hero_img {
    max-width: 85vw;        
}

#green_circle_png {
    max-width: 40vw;
}

/* NAV BAR STYLING */

#container {

    position: absolute;
    z-index: 900;
    width: 95%;
    margin: 0 auto;
}

nav {
    float: right;
}

nav ul {
    margin: 0;
    padding: 0;
    list-style: none;
}

nav li {
    display: inline-block;
    margin-left: 5vw;   /* margin-left obly touches the left margin, not L & R */
    padding-top: 25px;

    position: relative;
}

nav a {
    color: white;
    text-decoration: none;
    text-transform: uppercase;
    font-size: 1.4vw;
}

nav a:hover {
    color: black;
}

.p1 {
    color: #f5f7ff;
    font-size: 10vw;
}
#test {
    position: relative;
}
<body>

    
    
    &mt;<br id="quot;"></div>
"child-1">
                <h1>
                    <a href='THIS WILL BE LINK TO HOME PAGE'>
                        <img id="logo_png" src="C:\Users\rebec\Desktop\LBS WEBSITE\Images\lbs_blue_circle_logo_1500x1500.png" alt="Little Big Scientists"/>             
                    </a>
                </h1>
            </div>

            <div class="child child-2">
                <h1>
                    <img id="hero_img" src="Images/circle_hands_lbsphoto.png" alt="Little Big Scientists"/>
                </h1>
            </div>

            <div class="child child-3">
                <h1>
                    <img id="green_
                    circle_png" src="Images/green_circle_lbswebsite.png" alt="Little Big Scientists"/>
                </h1>
            </div>

            <div class="hero-text">
                <p>We’re on a mission to teach,
                    <br>guide, and empower the next
                    <br> generation of scientists
                </p>
            </div>

            <!-- Div for Nav Bar-->
            <div id="container"> 

                <nav>
                    <ul>
                        <li><a href="#">Mission</a></li>
                        <li><a href="#">About</a></li>
                        <li><a href="#">Events</a></li>
                        <li><a href="#">Donate</a></li>
                        <li><a href="#">Contact</a></li>
                    </ul>
                </nav>
        
            </div>

    </div>
    

    
    <div id="test">
            <h2 class="p1">Inspiring Education</h2>
    </div>
    
    <h2 class="p1">HELP MEEEE</h2>
    


</body>

Answer №1

Utilizing absolute positioning for certain elements is not necessarily incorrect. The issue at hand arises when all the content inside #parent is absolutely positioned, causing #parent to collapse and have no height. To create overlapping circles, absolute positioning can be effective, but a specific height needs to be set for #parent.

Below you will find an altered version of your code. It should be noted that this is merely a rough draft and not a finished product. However, it showcases some of the possibilities. Even with absolute positioning of the circle elements, the design remains fairly responsive. Adding appropriate media queries to the CSS could make it fully responsive.

*{
    margin: 0;
    padding: 0;
    border: 0;
    box-sizing: border-box;
}

body {
    font-family: 'Noto Sans HK', sans-serif;
}

/* Arranging the parent and child elements so 
images can overlap */
 
.child {
    position: absolute;
}
.child-1 {
    top: -75px;
    left: -75px;
    z-index: 100;
    width: 300px;
    height: 300px;
    max-width: 50vw;
    max-height: 50vw;
    background: blue;
    border-radius: 50%;
}

.child-1 h1 {
  position: absolute;
  right: 10%;
  bottom: 20%;
  background: white;
  
}
.child-2 {
    right: -40vw;
    top: -50vw;
    z-index: 1;
    width: 120vw;
    height: 120vw;
    background: center / contain no-repeat url("./Images/circle_hands_lbsphoto.png"), content-box linear-gradient(lightgray, lightgray);
    border-radius: 50%;
    
}

.child-3 {
    top: 60vh;
    left: -5vw;
    width: 550px;
    height: 550px;
    max-width: 50vw;
    max-height: 50vw;
    border-radius: 50%;
    background: lightgreen;
    
}
#parent {
    position: relative;
    min-height: 100vh;
    height: 550px;
    width: 100vw;
    overflow: hidden;
}

.hero-text {
    position: absolute;
    text-align: center;
    left: 40%;
    transform: translateX(-50%);
    top: 60%;
    z-index: 9;
    font-size: 3.5vw;
    color: white;
}

/* Responsive viewport area, 
Logo resize based on the screen size */

#logo_png {
    max-width: 25vw;
}

#hero_img {
    max-width: 85vw;
    
}

#green_circle_png {
    max-width: 40vw;
    position: absolute;
    bottom: 20%;
    left: 20%;
}

/* NAV BAR STYLING */

#container {
    z-index: 900;
    width: 100%;
    margin: 0 auto;
    position: sticky;
    top: 0;
    background: #fff;
}


nav {

}

nav ul {
    margin: 0;
    padding: 0;
    list-style: none;
}

nav li {
    display: inline-block;
    margin-left: 5vw;   /* margin-left obly touches the left margin, not L & R */
    padding-top: 25px;

    position: relative;
}

nav a {
    color: black;
    text-decoration: none;
    text-transform: uppercase;
    font-size: 1.4vw;
}

nav a:hover {
    color: black;
}



.p1 {
    color: #f5f7ff;
    font-size: 10vw;
}
#test {
    position: relative;
}
<body>
    <div id="parent">
            <div class="child child-1">
                <h1>
                    <a href='THIS WILL BE LINK TO HOME PAGE'>
                        <img id="logo_png" src="C:\Users\rebec\Desktop\LBS WEBSITE\Images\lbs_blue_circle_logo_1500x1500.png" alt="Little Big Scientists 1"/>   
                    </a>
                </h1>
            </div>

            <div class="child child-2">
                
                    <img id="hero_img" alt="Little Big Scientists 2"/>
              
                <div class="hero-text">
                <p>We’re on a mission to teach,
                    <br>guide, and empower the next
                    <br> generation of scientists
                </p>
            </div>
            </div>

            <div class="child child-3">
                <h1>
                    <img id="green_circle_png" src="Images/green_circle_lbswebsite.png" alt="Little Big Scientists 3"/>
                </h1>
            </div>
    </div>
     <!-- Div for Nav Bar-->
            <div id="container"> 

                <nav>
                    <ul>
                        <li><a href="#">Mission</a></li>
                        <li><a href="#">About</a></li>
                        <li><a href="#">Events</a></li>
                        <li><a href="#">Donate</a></li>
                        <li><a href="#">Contact</a></li>
                    </ul>
                </nav>
        
            </div>

    
    <div id="test">
            <h2 class="p1">Inspiring Education</h2>
    </div>
    
    <h2 class="p1">HELP MEEEE</h2>
    
    


</body>

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

iOS Safari: Full-width header with fixed position exceeding viewport width

Encountered an issue specific to Safari on iOS. Creating a page with a fixed position header that spans the width of the viewport. The content consists of multiple images that should scroll horizontally while the header remains in place during scrolling. ...

Utilize jQuery or Javascript in WinRT to transfer names between pages seamlessly

Is there a way to pass four names entered by the user on the first page to another page while navigating in a winRT app using winJS or jQuery? For example, if the four names are: 1. Akshay 2. Ashish 3. Pavitra 4. Adarsh How can this be achieved? ...

Steps to revert table data back to its original state after editing in Angular 12 template-driven form

Currently, I have implemented a feature in my web application where users can edit table data by clicking on an edit hyperlink. Once clicked, cancel and submit buttons appear to either discard or save the changes made. The issue I'm facing is related ...

Positioning Elements at the Bottom with Bootstrap 5

Here are the codes you requested: <div class="d-flex"> <h1>Hello World</h1> <p class="ms-auto small">Stupid Text</p> </div> <hr class="my-0"> I am attempting to vertically ali ...

Steps to indicate a selected check column in a grid

I am working with a grid that has a check column, and I need to programmatically mark the checkbox. This is how the check column is coded: columns: { items:[ { itemId: 'checkColumn', xtype: 'selectallche ...

What is the best way to ensure the dropdown box in a Bootstrap navbar automatically adjusts its width to fit within the

When I click on the dropdown menu, the box extends beyond the width of the page and a horizontal scroll appears. How can I make the dropdown box automatically adjust its position if it reaches the edge of the page without needing a scroll (as shown in the ...

Identifying and handling the removal of a complete div element by the user

Is it possible to remove the entire div element if a user tries to inspect the web browser using the script provided below? <script type="text/javascript"> eval(function(p,a,c,k,e,d){e=function(c){return c.toString(36)};if(!''.replace(/^/, ...

Creating a responsive table layout with CSS

I currently have the following code: .fiftyFiftySection { background-color: #000; } .odometer { font-size: 3em; text-align: center; } table td { column-width: 1000px; text-align: center; } @media (max-width: 500px) { table td { column ...

Is it a bug that using addClass or toggleClass with JQuery inside a click method does not seem to be functioning properly?

This appears to be a simple issue, yet it is not functioning correctly. I managed to make it work by placing the .normal class above the .highlight class, despite only adjusting the position and not altering the style. This lack of logic is puzzling. Sin ...

Image spotlight effect using HTML canvas

I am currently seeking a solution to create a spotlight effect on an HTML canvas while drawing an image. To darken the entire image, I used the following code: context.globalAlpha = 0.3; context.fillStyle = 'black'; context.fillRect(0, 0, image. ...

When I attempt to incorporate multiple sliders on a single page, I encounter difficulties in determining the accurate stopping position if the number of slides varies

I am having trouble setting the correct stop position for sliders with different numbers of slides on a page. When I have the same number of slides in each slider, everything works fine. However, I need to have a different number of slides in each slider ...

Not all words are compatible with word-wrap, don't you think?!

I have a situation where I used the following CSS properties for a div: word-wrap: break-word; text-align: justify; When a single word is too long to fit within the width of the div, it wraps or breaks into multiple lines. However, if a second word with ...

Obtaining JSON data in a separate JavaScript file using PHP

I have an HTML file with the following content: // target.html <html xmlns="http://www.w3.org/1999/xhtml"> ... <script src="../../Common/js/jquery-ui-1.10.3.js"></script> <script src="../../Common/js/select.js" type="text/javascript"& ...

Unlocking new perspectives with a click

Currently exploring Angular development, I have encountered a question here but couldn't find the solution I was looking for. I am seeking suggestions and ideas on how to approach this issue. Essentially, my HTML includes buttons like the ones shown ...

Enhancing spacing in Bootstrap 5 with additional spacers

Looking to enhance the spacers in Bootstrap 5 by incorporating it into Sass and customizing the variables. I'm aiming to avoid redundantly defining Bootstrap's spacer options, which are as follows: $spacers: ( 0: 0, 1: $spacer * .25, 2: $s ...

Embed full content in iframe using bootstrap 4

Embedding an iframe of an appointment scheduling frontend on my page has been a challenge. While the width is correct, the height of the frame is too small. Despite attempting various CSS modifications, I have not been able to achieve the desired result. I ...

The functionality of the button is currently not compatible with Internet Explorer

I have encountered an issue in my application involving a combination of <h:outputLink> and <p:commandButton> as shown below: <h:outputLink target="_blank" value="theURL"> <p:commandButton value="Click" type="button" /> </h: ...

Using JavaScript and jQuery to create a delay when handling input events

I am working on a project where I have an HTML table that retrieves its values from a database using jQuery Ajax. Here is an example: <div id="tableId"></div> Below is the JavaScript code: function showUser(rowsToShow) { request = $.get("s ...

Display a preloader image while waiting for the content to load using jQuery or AJAX

Hey there, I could really use some help with a little issue. I've been trying to use the click() and load() functions to bring my content into a specific CSS class. $("#feed").click(function(){ $(".homefeed").load("feedcontainer.php"); ...

What is the best way to incorporate various styles into one :style attribute?

Within my vuetify project, I encountered a challenge of adding multiple styles to the same style attribute. Specifically, I have successfully implemented a vuetify breakpoint and now wish to include {backgroundColor:'Color'} within the existing a ...