What causes margins to collapse and how does display:inline-block prevent it?

Explore the concept of margin collapse in this example.

.ct1{
background: red;
margin:40px;
border:1px solid;width:100%;
}

.ct2{
background: pink;
margin:40px;
border:1px solid;width:100%;
}
<div class="ctbox">
<div class="ct1">first</div>
<div class="ct2">second</div>
</div>

Experience how margins collapse occurs between two divs ct1 and ct2, resulting in a distance that is not simply 40px + 40px = 80px, but rather just 40px.

By adding the CSS property display:inline-block; to either ct1 or ct2, you can eliminate the margins collapse and ensure that the distance between them is indeed 80px. Discover why display:inline-block; helps prevent margins collapse.

Answer №1

According to the concept of Margin Collapsing

  • When dealing with a floated box, its margins do not collapse with any other box, including its in-flow children.
  • The margins of elements that create new block formatting contexts (like floats and elements with 'overflow' set to anything but 'visible') do not collapse with their in-flow children.
  • Margins of inline-block boxes also do not collapse, even with their in-flow children.

...

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

CSS can create a semi-circle shape using borders and outlines

Attempting to craft a CSS circle that mirrors the image below: ...with just a single div: <div class="myCircle"></div> solely utilizing CSS rules. No SVG, WebGL, DirectX, etc. permitted. My initial approach involved constructing a complete ...

JavaScript form not working on submission

When my form is submitted, a function is called that successfully redirects users on desktop to either the download-confirmation or download-failure page. However, when testing on mobile devices such as iOS and iPad, the redirection does not seem to work. ...

Why does a black model appear when loading a GLTF model with materials?

I am currently attempting to load a 3D model in glb format. Below is the code snippet: Expected Outcome: Image Current Outcome: Image var renderer = new THREE.WebGLRenderer(); renderer.setSize(1000, 1000); renderer.setPixelRatio(window.devicePixelRati ...

What is the best way to select the initial descendant that matches a specific

How can we target only the first matching descendant element, without affecting others further down the hierarchy? For example: If the HTML structure is like this: <div class="wrapper"> <table> <tbody> <tr> < ...

Writing CSS rules for generating HTML code when sending emails through the command line in Go

When trying to compose HTML with CSS for email delivery using Go command line execution, errors related to CSS properties are popping up. For instance, it's showing "not found" error in the terminal for properties like background: rgb(255, 255, 255) o ...

Is it possible to generate a 3D building structure using an existing image with Jquery and Css?

Have you checked out this website made in flash? Do you think we could achieve the same using jquery and css? Visit here If you click on any building labeled "Release" You will see a hover animation effect on one of the buildings. I would like to replica ...

Creative CSS Expansion Navigation

I've come up with an innovative concept for an expandable panel: The default state of the panel is hidden Clicking a button exposes the panel As the panel emerges, it smoothly expands upwards from the bottom of its parent at a consistent pace, creati ...

What is the reason for the disparity between CSS box-sizing content-box and border-box?

Consider the following example where I only changed the CSS box-sizing property from content-box to border-box. The resulting output difference is significant. Give this code a try: .box1 { width: 300px; height: 100px; border: 1px solid blue; pa ...

Arrange and display similar objects together

I have a list of items in a listView that need to be visually grouped based on their class, displayed within boxes. For example, I have 5 items with the following classes: <div class="1"></div> <div class="1"></div> <div class= ...

I'm encountering difficulties in automatically populating the category field from an API

Hey there! I have set up a signup form and I am trying to automatically fetch categories from the server API to populate an input area. Everything seems to be in place, but for some reason, I am unable to retrieve the data. API: Here is the code I'm ...

Move a div by dragging and dropping it without creating duplicates

I'm in need of some assistance. It seems like a simple task, but I'm having trouble finding a solution. I've managed to create draggable elements that work smoothly without any issues. The drag and drop functionality is working perfectly. ...

Tips for emphasizing a table row according to its value

I am currently developing a Google web application that involves CRUD data generated from Google Sheets. The search function is working perfectly, but I am trying to enhance it by highlighting specific rows based on the data value in Column 7. For example ...

Is there a way to ensure that spans are inline with a div?

My structure is pretty straightforward: <div></div><span></span><span></span> However, I'm looking to have them all appear on a single line. Currently, they are rendering like this: <div /> <span />&l ...

Bootstrap is causing issues with unidentified div elements

I recently embarked on creating a slideshow using HTML, CSS, and jQuery. After completing the slideshow, I decided to add an interactive page beneath it. To streamline the layout process, I opted to utilize Bootstrap. However, upon loading Bootstrap, I en ...

Validation of Style

Is there a way to translate the following CSS code into JavaScript? Basically, I want it to apply the style rules after the onchange event. .appnitro input:required:valid, .appnitro textarea:required:valid { box-shadow: 0 0 5px #5cd053; border-c ...

Set default selected value for dropdown list using radio buttons

Could anyone provide guidance on how to reset a dropdown list value using a radio button selection? Specifically, I need the dropdown list to reset to its default "selected" option when a particular radio button is clicked. Any recommended approaches usin ...

The absence of PHP GET variable being set

I am encountering an issue with my registration php class. It is designed to display a form and upon clicking the registration button, it triggers a function in a login javascript file. This JavaScript file utilizes ajax to send data to an index.php file. ...

Google Extension PHP Plugin

Is it feasible to integrate a PHP file into a Google Extension for Chrome? I've been exploring the idea of creating an extension and most resources only mention HTML, CSS, and JavaScript. Any guidance on using PHP in the extension would be highly valu ...

There is a mismatch in the host name: please consider using a domain name (e.g., https://myname.myorg.com) instead of https://localhost

My current setup involves an HTML application that sends an AJAX request to a Qt C++ HTTP Server running on my local computer, hosted at "https://localhost:8081". However, I am facing an issue with a host name mismatch causing the AJAX request to fail. Is ...

Eliminate gaps between lines in a wrapped Flexbox layout

I am trying to eliminate the spaces between rows in a horizontal list flexbox. I am creating an alphabetical list and I want it to flow seamlessly without any gaps. This is what I have so far: #example { display: block; margin-left: auto; margin-r ...