Why does having a floating div and a div positioned below an absolutely positioned div result in additional margin?

When trying to create a two-column layout with a left floating navigation div and an absolute positioned header, I encountered an issue where the navigation div had an extra margin of 4em. This seemed unnecessary to me as it disrupted the expected layout flow. I have included my code below for reference:


 * {
    box-sizing: border-box;
  }

  body { /* disable default browser settings */
    margin: 0px;
    padding: 0px; 
  }
  header {
    position: absolute;
    top: 1px;
    left: 0px;
    height: 3em;
    width: 100%;
    padding: 10px;
    border: 1px solid blue;
    border-radius: 0.5em;
    background-color: silver;
  }
  nav {   
      float: left; 
      width: 17em; 
      border: 1px dashed red; 
      margin-top: 4em;
      padding: 5px;
    }
  
  article {
      margin: 4em 1em 0em 18em; 
      padding: 0px 10px;
      border: 1px dashed red;
      min-width: 15em;
  }

After further investigation, I realized that by removing the absolute positioning of the header, the layout appeared as expected: header - margin - navigation and article on one line. My question here is: why does the absolute positioning of the header cause this extra margin in the navigation div?

Answer №1

By positioning the header absolutely, the layout behaves as if the header doesn't exist.

Considering only the body, nav, and article elements, we see that when the header is no longer relevant to the layout, the article becomes the first child of the body in flow. This causes the top margin of the article to collapse with the body's margin, making it appear unchanged (refer to Why does this CSS margin-top style not work?). The nav, being floated, is taken out of the flow and its margin edge aligns with the body.

When the header is no longer positioned, it remains the first child of the body in flow. Now, the article's top margin collapses with the bottom margin of the header instead of the body's top margin. Since the header has no margins and both the nav and article have equal top margins, they become aligned with each other.

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

How to manipulate wrapping behavior in flexbox

My flex container holds three divs, arranged in two rows: First row: One div with a fixed width Another div that should stretch to fill the remaining space Second row: Just one item However, the first div stretches to 100% width and pushes the seco ...

Tips for styling links in Nuxt/Vue based on specific conditions

Struggling with conditional styling in my Nuxt app. I want to change the background color of the active link based on the current page. Using .nuxt-link-exact-active works for styling the active link, but how can I customize it for each page? The links ar ...

Error occurred during the file watch process in PhpStorm after saving the CSSO file

Following the advice in the official PhpStorm documentation, I have implemented a CSS minifier. However, upon attempting to use it, I encountered the following error: cmd.exe /D /C call C:\Users\douglas\AppData\Roaming\npm\css ...

SliderToggle and Div implementation in Internet Explorer (IE) using jQuery

I'm encountering an issue with jQuery's slideToggle function and a div specifically in IE. Here is the code snippet causing the problem: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.d ...

Best Practices for Integrating PHP Code into an HTML File for Contact Form Placement

My current setup involves an html file named index.html, which contains the code for my contact form fields such as name and email. Is there a way to include PHP code in this file without converting it to index.php? ...

Can anyone guide me on saving an array to local storage, retrieving its contents, and populating them into a select element?

I have written code to retrieve an array and add its elements to a select object. Check it out: var select = document.getElementById('select'); var theArray = localStorage.getItem("array") JSON.parse(theArray) if (theArray != null){ f ...

What could be causing the lack of access to the prerender.io service in this particular setup?

I am the owner of a Angular application running on an Apache server. To enable prerendering, I added <meta name="fragment" content="!" /> in the <head> section and set $locationProvider.html5Mode(true); In my Apache server's .htaccess fi ...

Bootstrap: Organize Columns Simultaneously with Push and Pull for Customized Column Ordering

I am experiencing an issue with the concurrent use of "Push" and "Pull" column ordering classes. I would like my columns to be ordered like this: However, the result on medium and larger screen sizes is <section style="direction:rtl;"> <div cla ...

Angular routes cause a glitch in the Bootstrap navbar, causing it to shift to the left on certain active

Apologies for the simple question, I am new to web design and couldn't find a solution even after extensive googling and searching. The issue I am facing is that when clicking on "EX5" or "EX6" in my navbar, it shifts to the left side of the screen i ...

How to assign attributes to all child elements in Angular?

I have a unique component in Angular that I utilize throughout my app. It's a button component which I use by calling <app-delete-btn></app-delete-btn> wherever needed. I tried to set the tabindex="1" attribute for my component ...

Should all images be set to 100% width with a standard Bootstrap configuration?

For my website project, I decided to incorporate the Bootstrap carousel into the header using version 3.0.3 of Bootstrap. Everything seemed to be functioning correctly until I noticed a strange issue with the image sizes on the page. Initially, all the ima ...

Persistent footer overlaps lower body content

Issue with Sticky Footer in Safari I recently added a sticky footer to my website, but I'm facing problems with it covering the body content on Safari. It works fine on Chrome and Firefox after adding a bottom margin to the body to adjust for the hei ...

Processing an HTML form submission with the help of PHP and AJAX

Having trouble saving basic email data from a website to a MySQL database. I've written the code, but it's not functioning correctly and I can't seem to figure out why. <form class="subfield"> <input type="text" id="em ...

What is the best way to showcase my divs in a masonry layout using flexbox?

I am attempting to design display divs in a masonry style using flex/flexbox. The layout must consist of a maximum of 2 columns, with each div having the same width (thus dividing into 2 columns equally) while varying in height based on their content. Some ...

What could be the reason for the absence of this Javascript function in my attribute?

I have been working on an app using electron, and I have a function that successfully adds tabs to the app. The issue arises when I try to add tabs via JavaScript with the onclick attribute - they show up as expected but do not execute the code to hide and ...

What is the best method for integrating custom CSS into Extjs 6?

Transitioning from Extjs 4.2 to Extjs 6.2 has presented a challenge for me. In the past, I would include a custom css file in the head using a simple link placed after the Extjs inclusion. <link rel="stylesheet" href="/custom.css" type="text/css" chars ...

Issue with Three.js GLTF loader peculiar behavior while attempting to incorporate three-dimensional text

I had an idea to import a prebuilt gltf scene created in Blender, then add some 3D text using the ttf loader and manipulate it. Each loader worked perfectly when used separately, but strange things started happening when I combined them into a single scrip ...

What CSS attribute determines the distinction in visual presentation between two elements that share the same CSS configurations?

In the HTML code below, there is a button and a div with the same class and content: <div class="root"><!-- --><button class="outer"><div class="middle"><div class="inner">label</div></div></button><!-- - ...

JavaScript is unable to modify the value of an input in HTML

function modifyComment(id) { var pageID = "<?= $ID ?>"; var commentID = "p" + id; console.log(id); console.log(commentID); console.log(pageID); var commentContent = document.getElementById(commentID).innerHTML; < ...

Showing off the latest products at the top of the list

Typically, when utilizing ngFor, the most recent item is displayed underneath the initial element. For instance, a list containing: [Apple, Orange, Banana] If we use ngFor to display this list: Apple Orange Banana I am interested in learning a method t ...