Section with relative positioning has taken an unexpected position

I'm facing a rather basic issue with positioning a relative div on my webpage.

My layout consists of an absolute header that is 100vh tall, containing a headline and caption. Below the header, I have two section elements, both set to a relative position. The first section has a "top" value of 100vh to place it right below the header. However, the second section is causing trouble as it seems to get attached to the header's caption instead of following the first section.

To illustrate the problem, I've created a snippet showcasing the code:

/* -------------------- UNIVERSAL TYPES -------------------- */

section {
position: relative;
width: 100%;
padding: 40px 0;
} 

// Rest of the CSS styles...

The HTML structure of the page:

<head>
  // External stylesheet link
</head>
<body>
  <header>
    <div class="content">
      <div class="headline">Header.</div>
      <div class="caption">Caption on dark background. This header section is 100vh tall and positioned absolutely.</div>
    </div>
  </header>
  <section id="About">
    // About section content
  </section>
  <section id="Portfolio">
    // Portfolio section content
  </section>
</body>

I understand this might have a straightforward solution (I'm still learning modern CSS), but I've been struggling with it for some time now.

Answer №1

This phenomenon occurs because the use of the position: absolute property results in the content being taken out of the normal flow of the page rendering and placed where specifically positioned. As a result, the following element does not acknowledge the previous one and starts from its original position.

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  color: #fff;
  font-size: 20px;
}

header {
  height: 200px;
  background: red;
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
}

section {
  position: relative;
  top: 200px;
  height: 300px;
  background: blue;
}
<header>This is absolute</header>
<section>This is Relative but aligned by giving TOP: 200px<br>Just to tell where RED DIV ends</section>

Answer №2

Give this a shot. The .content is set to absolute, which removes it from the normal document flow, allowing other elements to either cover or go behind it.

html {
    margin: 0;
}
#Info {
    background-color: blue;
    color: #ffffff;
}

Answer №3

Elements that are relative remain at their original point of origin. In the hierarchy, the header and sections are siblings. Due to its absolute positioning, the header does not affect the layout of other elements. Normally, the first section would occupy the space where the header is positioned but it has been shifted downwards by 100vh. The second section, lacking any specific direction to move, stays in its original position which is at the end of the first section before the vertical shift. To achieve the desired behavior, you can set the top value of the second section equal to the height of the first section.

If you have sibling elements, it is recommended to position them consistently and enclose them within a positioned parent element. In the provided code snippet, I have applied a common practice of using a relative parent with absolute children. Absolute positioning enables an element to be referenced to the closest positioned ancestor. If the closest positioned ancestor happens to be its parent, then the element will align itself with the edges of the parent element.

  • Enclose all elements within a container and assign position:relative [<main>]

  • Apply position: absolute to immediate child elements [<section> and <header>]

  • Set a specific height for each element [section, header {height:100vh}]

  • Define top:0 for the header (already done)

  • For the first section, use top:{HEIGHT OF HEADER} (already done) [top:100vh]

  • The second section should have

    top:{HEIGHT OF HEADER + HEIGHT OF 1ST SECTION}
    , or if the child is relative, then top:{HEIGHT OF 1st SECTION}. This step was missed in the existing implementation [top:200vh]

SNIPPET(outlines and transparent backgrounds for demo)

html,
body {
  height: 100%;
  width: 100%;
  font: 400 16px/1.5 Verdana;
  overflow: scroll;
}

* {
  box-sizing: border-box
}


/* -------------------- UNIVERSAL TYPES -------------------- */

section {
  position: absolute;
  width: 100%;
  height: 100vh;
  padding: 40px 0;
  margin: 0 auto;
  outline: 8px solid blue;
}

main {
  position: relative;
  height: auto;
  min-height: 100vh;
  outline: 10px solid gold;
}


/* -------------------- HEADER  -------------------- */

header {
  position: absolute;
  width: 100%;
  background: rgba(20, 20, 20, .1);
  top: 0;
  outline: 5px dashed gold;
  height: 100vh;
}

header .content {
  width: auto;
  max-width: 500px;
  padding: 10px;
  margin: 0 auto;
  /*margin-top: 40vh;*/
  text-align: center;
  color: tomato;
  outline: 3px dotted red;
}

header .content .headline {
  font-size: 100px;
  margin-bottom: 0px height:100px;
  /*margin-bottom: 20px;*/
  outline: 3px dotted blue;
}

header .content .caption {
  position: relative;
  font-size: 20px;
  outline: 3px dotted green;
}


/* -------------------- UNIVERSAL CONTENT -------------------- */

.pagecontent {
  position: absolute;
  width: 1000px;
  margin: 0 auto;
  padding: 30px;
  outline: 5px dashed brown;
}

.pagecontent h2 {
  font-size: 80px;
  outline: 5px dotted cyan;
}

.pagecontent p {
  width: 100%;
  font-size: 20px;
  outline: 3px solid lime
}


/* -------------------- INDIVIDUAL SECTIONS -------------------- */

#About {
  top: 100vh;
  background-color: rgba(111, 111, 111, .1);
  color: #ff0000;
  outline: 5px solid red;
}

#Portfolio {
  top: 200vh;
  background-color: rgba(51, 51, 51, .1);
  color: purple;
  outline: 5px double purple;
}
<head>
  <!--<link rel="stylesheet" href="https://unpkg.com/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="7606030413150505364658405844">[email protected]</a>/build/pure-min.css" integrity="sha384-UQiGfs9ICog+LwheBSRCt1o5cbyKIHbwjWscjemyBMT9YCUMZffs6UqUTd0hObXD" crossorigin="anonymous">-->
</head>

<body>
  <main>
    <header>
      <div class="content">
        <div class="headline">Header.</div>
        <div class="caption">Caption on dark background. This header section is 100vh tall and positioned absolutely.</div>
      </div>
    </header>

    <section id="About">
      <div class="pagecontent">
        <h2> About section header </h2>
        <p>All subsequent sections, starting with this one, are relative. My problem is that this section is obscured by the "Portfolio" section, which refuses to add on to the end of the page, and instead inserts itself immediately following the header caption. Duis aliquam finibus sagittis.</p>
      </div>
    </section>

    <section id="Portfolio">
      <div class="pagecontent">
        <h2>Portfolio section header.</h2>
        <p>This section refuses to add on to the end of the page, and instead inserts itself immediately following the header caption.</p>
      </div>
    </section>
  </main>
</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

Pictures change positions in online email interfaces like gmail

Hey there! I'm encountering an issue with the image on my website. The image is contained within a container and seems to extend a bit to the right. Everything looks perfect in all email clients except for web-based ones like Hotmail and Gmail. Here a ...

The issue arises with owl carousel elements not functioning properly when added dynamically through ajax calls and jquery

The owl carousel items seem to be facing an issue when added dynamically using AJAX and jQuery. Here is the AJAX code snippet, any assistance would be highly appreciated. Thank you in advance! $.ajax({ type: "POST", url: "urllinks", data: &a ...

Transform the infoBox into a jQuery dialog modal window

In this section, I have integrated a map with markers and infoBoxes. My goal now is to replace the infoBoxes with jquery dialog() and modal window functionalities. How can I achieve this effectively? Below is the code snippet that includes the implementat ...

Having trouble centering my menu bar on CSS, can anyone help?

I'm having trouble getting the buttons inside the menu bar to align properly. Does anyone have any suggestions on how to fix this? I'm not very experienced with coding, so any assistance would be greatly appreciated! .main-navigation { clear ...

Fixed-bottom footer in Bootstrap 5 that overlays content

Having trouble creating a fixed footer using the fixed-bottom class in react & bootstrap. The issue is that it's covering some content and I've tried various solutions without success. Any suggestions on how to fix this? (Code and Demo provided b ...

What could be causing the CSS height percentage to malfunction?

I'm having trouble with some basic CSS that uses percentages. I've labeled everything and checked my code, but it still isn't working properly. Here's the CSS code snippet: .BoxHeight { height: 100%; } #Box { margin-top: 0%; mar ...

Invoke data-id when the ajax call is successful

Initially, there was a smoothly working "like button" with the following appearance: <a href="javascript:void();" class="like" id="<?php echo $row['id']; ?>">Like <span><?php echo likes($row['id']); ?></span ...

Differences between .php and .html: What causes variations in IE rendering?

After duplicating one of the HTML pages in my project, I simply changed the file extension from .html to .php. Surprisingly, all browsers display the page identically except for Internet Explorer (IE), which seems to handle the pages differently based on t ...

create a miniature display featuring text and visuals using HTML

Currently, I am in the process of developing a website and require assistance. My goal is to have a small screen appear with text and images whenever a link is clicked, similar to the functionality found in Chrome's "chrome://settings/startup". Howeve ...

Clicking on an element will remove a class and unselect an input using JQuery

Trying to achieve a functionality where clicking on a list item with a radio button input selects it, but clicking again deselects it. The issue is despite various attempts, nothing seems to work properly. function setupToptions() { if ($('ul.top ...

Left-align elements within a div container that is centered

I want to left-align my elements within a div container that is centered. I used the text-align property to center the overall div, but now I'm having trouble aligning another content container's text to the left within the center-aligned div. H ...

What is the best way to vertically center an item at the end of a card in Bootstrap 4?

In the process of creating a long bootstrap card containing two rows of information including a title and description, I encountered an alignment issue with the button. The button is intended to be aligned to the right of the card while remaining centered. ...

Create Stunning Half-Brick Image Layouts Using HTML and CSS

Can anyone offer advice on creating complex image layouts such as the half-brick pattern using HTML, CSS, and jQuery? I am looking to be able to rotate and scale the image as well. The background-image CSS property does not seem to support scaling. I have ...

Issue with alert not being triggered in browser when using HTML and JavaScript function

Just starting out with HTML and Javascript! I'm working on a simple program where users can input a card number, and the browser should indicate whether it is valid (between 13-16 digits) or not. The website looks great, but I'm not getting an ...

Creating dynamic PDFs from HTML templates and generating table of contents using Java

Our latest project involves using an editor to create templates for catalogs specifically for design purposes. The process begins with designing a template in the editor and adding data in that specific format. Once the template is completed, it is convert ...

Issues encountered with the functionality of face-api and tensorflow.js within the browser

I've been trying to execute this example in the browser Check out the example here Specifically looking at this code snippet <!DOCTYPE html> <html> ... (Contents of the code snippet) ... </body> </html> Unfortunately, I&apos ...

Incorporating a flexible header size and scrollable content: Tips for avoiding the need to scroll through the entire page to find

I am currently working on creating a unique page template that has certain requirements: The height of the header may vary The content and aside containers should be independently scrollable Anchor links should not disrupt the page layout To see what I h ...

What is the best way to apply maximum height to an element using CSS?

Looking for help with my website: I made some changes in the code using Firebug and identified these elements that I want to modify (but changes are not live yet) The following code represents the elements on my site: This is the HTML code: <di ...

Steps for Converting Unicode Characters to HTML Encoding in C++

Can you assist me with converting unicode characters like the ones below in C++? Thére Àre sôme spëcial charâcters ïn thìs têxt عربى I need to convert them to HTML encoding as shown below: Th&eacute;re &Agrave;re s&ocirc;me sp&am ...

Maintaining leading zeros in Firefox and SafariMaintaining leading zeroes in Firefox and

Having trouble with the number field in Firefox and Safari browsers (and possibly Opera, although I haven't tested it there). When entering an initial value in the number field that contains leading zeros, Firefox and Safari automatically remove them. ...