What defines a top margin with adequate space?

Based on the WC3 specification:

  • If an in-flow block box with a 'height' of 'auto' has no bottom padding and no bottom border, its bottom margin collapses with the bottom margin of its last in-flow block-level child, provided that:

    • The child's bottom margin does not collapse with a top margin that has clearance,

    • The child's bottom margin does not collapse with the box's top margin if the box's min-height is non-zero.

Can someone offer an explanation along with an example to clarify the implications of the last paragraph?

Answer №1

Ready for a bit of an adventure? In my humble opinion, coming across this scenario in real-world practice is highly unlikely. Personally, I have yet to find a practical use for it.

The concept we're diving into is clearance, which occurs when a box is moved down to clear a float within its block formatting context.

This is achieved using clear:left or clear:right or clear:both. If the box would otherwise be next to a float on the side that it's cleared, it will be shifted down until its border-box is completely below the margin box of the float.

Let's explore an example:

body {
  border: 1px solid red;
}
main {
  padding-top:1px;
  margin-bottom:100px;
}
aside {
  background-color: tan;
  height: 100px;
  width: 200px;
  float: left;
}
header {
  margin-top:90px;
}
footer {
  margin-bottom:100px;
}
<body>
  <main>
    <aside>The aside</aside>
    <header></header>
    <footer></footer>
  </main> 
</body>

In this setup, the aside element acts as the float. The top padding on the main element ensures no collapsing can happen with the top margins of the main or body elements.

Due to the empty content of the header and footer elements, along with the margins set, they collapse together, resulting in a total vertical margin of 100px. The body element contains them within its content box, making its height 101px (1px padding + 100px margin).

Now, let's introduce clear:left to the header element.

body {
  border: 1px solid red;
}
main {
  padding-top:1px;
  margin-bottom:100px;
}
aside {
  background-color: tan;
  height: 100px;
  width: 200px;
  float: left;
}
header {
  margin-top:90px;
  clear:left;
}
footer {
  margin-bottom:100px;
}
<body>
  <main>
    <aside>The aside</aside>
    <header></header>
    <footer></footer>
  </main> 
</body>

After this change, the body element's height increases to 211px!.

By assigning clearance to the header element, its content shifts down by 10px to clear the float. This adjustment triggers further movement due to margin interactions, altering the overall layout.

This leads us to a key observation - the impact of having an element with clearance on margin collapse behavior.

Exploring different scenarios helps solidify our understanding of how clearances and margins influence layout outcomes.

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

Content duplication occurs when sharing information

After clicking the share button on a user's link, the post is successfully shared but appears twice. Upon inspecting through Firebug, it reveals two POST requests being triggered when the button is clicked once, resulting in duplicate posts being inse ...

Angular styling and form error issue

Hey there! I'm new to Angular and facing a major issue along with a minor styling problem. Let's start with the big one: <mat-form-field appearance="fill"> <mat-label>Password</mat-label> <input matInput ...

Clicking on a JQuery dropdown menu will always result in it staying open

After creating a dropdown menu click, I noticed some strange behavior. When I click the dropdown button, the menu appears as expected. However, if I move my cursor away without clicking the button again, the dropdown menu disappears and behaves like a hove ...

What are the solutions for resolving problems with the display and functionality of a multi-page form in Internet Explorer?

I am currently working on a form and you can view it at this link: http://jsfiddle.net/pPWr3/14/ While the form displays correctly in Chrome and Firefox, I am experiencing display and functionality issues when using Internet Explorer 9. The problems inclu ...

Align an image with text using CSS and HTML

I'm having trouble with my CSS and HTML code. I want to align the text to the right of an image in a grid format, but it keeps appearing below the image instead. Here is the code snippet: <html> <head> <style type="text/css"> #conta ...

Struggling to properly reference an item within an array

Struggling to create an HTML page for a class project that utilizes a drop-down menu to display relevant information from an array? Check out my jsfiddle for the full HTML section. Any assistance would be greatly appreciated. I must admit, I'm not we ...

I'm struggling to understand why the jQuery find() method isn't functioning as expected

UPDATE: In a twist of events not caused by syntax errors, but rather by a loading issue with the html template, it turns out that the checkbox update was happening prematurely. So if you're searching for an element that seems to be missing, it may sim ...

Switch between webpage design for different devices (mobile/desktop) using javascript, jquery, and bootstrap

I am currently working on a webpage that contains various html elements with bootstrap classes applied to them. <div class="col-xs-12 col-lg-4"></div> My goal is to implement a button that, when clicked, will toggle the viewport/layout change ...

The website is not loading properly on Firefox and Internet Explorer

This site consists of a straightforward 5-page layout, featuring a form on the home page and a redirected 'thank you' page. While it operates smoothly in Chrome and Safari, there are compatibility issues with Firefox and IE. Nevertheless, it fun ...

What is the best way to position a full width input beside my div element?

Having a small challenge with my CSS/HTML code, I am trying to position a full-width input next to my div. This is my HTML : <div class="leftdiv"></div> <input type="text" id="title-input" placeholder="nothing"></div> And here is ...

Combining the jquery-UI slider functionality with the canvas rotate() method

Is there a way to rotate an image using html2canvas plugin and jQuery UI slider? I am new to programming and need some guidance on how to achieve this feature. Here is my current progress: http://jsfiddle.net/davadi/3d3wbpt7/3/ ` $("#slider").slider({ ...

Multiple pop-up windows scattered around a single webpage

Is there a more efficient way to display 50 different modals on one page without causing the HTML to become overwhelmingly large? I'm working on a project that requires multiple modal windows to open, sometimes triggering additional modals. I am look ...

What is the best way to delete a jQuery.bind event handler that has been created for an event

I have a div and I need to assign two scroll functions to it, but I also want to remove one of them after a certain condition is met. <div id="div1" class="mydivs"> something </div> <div id="div2">Some crap here</div> <script&g ...

The navigation buttons and slide indicators on the Bootstrap 5 carousel are failing to function properly

I am encountering an issue with my simple bootstrap carousel on my HTML page. The controls do not seem to be functioning properly and the indicators are not appearing. edit: After reviewing the documentation, I followed the instructions provided but the p ...

Ways to customize CSS styles for a single page?

<head> <link href="index.css" rel="stylesheet"> <style> #amor_di_mundo{ color:#ffffff; } </style> </head> <body> <div id='divL'> <div id='amor_di_mundo'>Amor di Mundo</div> <di ...

A pale box located at the lower part of a screen

There seems to be a persistent white box at the bottom of my page/view that I can't seem to remove. Even when inspecting elements, there are no visible elements present in that space, not even within the body or html tags. I have attempted various sol ...

The function Uri.EscapeDataString() helpfully changes the character "/" to %2F

I'm currently working on converting strings to URI format so that I can use them in URLs to display images on my webpage. My project is built using ASP.NET Core. So far, I've tried both System.Web.HttpUtility.UrlEncode() and Uri.EscapeDataString ...

My code gets disrupted when I switch between ids and classes

I'm currently learning about javascript and jquery, attempting to implement various scripts. I successfully executed the following script: jQuery(document).ready(function($) { var scroll_start = 0; var startchange = $('#homepage-header' ...

Are there methods to individually style components that have been generated through the .map() function?

My goal is to style each <Tuner /> component separately, which are being rendered for each item in the this.state.notes array using this.state.notes.map(). I want to position them individually on the page, but currently they all appear together. This ...

Alpinejs Mega Menu Issue: Hover Functionality Glitchy

I'm working on a complex Mega Menu project that is activated upon hovering, using the powerful combination of Tailwind CSS and Alpinejs. The functionality is mostly there, but I've encountered some bugs along the way. Despite my attempts to impl ...