How to solve the margin collapse problem in CSS

How to solve the margin collapse problem in CSS

First, let's look at three situations where margin collapse occurs:

1. When two adjacent block-level elements meet, the upper element has a bottom margin margin-bottom and the lower element has a top margin margin-top, then the vertical distance between them takes the larger of the two values.

<style>
  .box1 {
     width: 150px;
     height: 100px;
     margin-bottom: 20px;
     background-color: rgb(201, 239, 98);
  }
  .box2 {
     width: 100px;
     height: 100px;
     background-color: rebeccapurple;
     margin-top: 10px;
  }
</style>
   <div class="box1"></div>
   <div class="box2"></div>

At this time, the vertical distance between the two boxes is not 30px, but 20px.

Solution:

Try to only add margin to one box.

2. For two nested block elements, if the parent element has no top margin and border, the top margin of the parent element will be merged with the top margin of the child element, and the merged margin will be the larger of the two.

<style>
        .box1 {
            width: 150px;
            height: 100px;
            margin-top: 20px; 
            /* border: 1px solid #000000; */
            background-color: red;
        }

        .box2 {
            width: 100px;
            height: 100px;
            /* border: 1px solid #000000; */
            background-color: rebeccapurple;
            margin-top: 10px;
        }
    </style>
</head>

<body>
    <div class="box1">
        <div class="box2"></div>
    </div>
</body>

At this time, the two boxes will be merged, and the top margin will be 20px.

Solution:

① Define the top border for the parent element

② Define the top margin for the parent element

③Add overflow:hidden to the parent element;

④Add floating

⑤Add absolute positioning

3. If there is an empty block-level element, border, padding, inline content, height, and min-height do not exist, then there will be no obstruction between the upper and lower margins, and the upper and lower margins will be merged.

<p style="margin-bottom: 0px;">The distance between this paragraph and the following paragraph will be 20px</p>

<div style="margin-top: 20px; margin-bottom: 20px;"></div>

<p style="margin-top: 0px;">The distance between this paragraph and the paragraph above will be 20px</p>

It can be understood that the width of the middle div is 0 and the upper and lower margins are merged, and only the maximum value of the margin is taken.

This is the end of this article on how to solve the margin collapse problem in CSS. For more relevant CSS margin collapse content, please search 123WORDPRESS.COM’s previous articles or continue to browse the related articles below. I hope everyone will support 123WORDPRESS.COM in the future!

<<:  How to install ElasticSearch on Docker in one article

>>:  Example of implementing text wrapping in html (mixed text and images in html)

Recommend

Creation, constraints and deletion of foreign keys in MySQL

Preface After MySQL version 3.23.44, InnoDB engin...

Docker installation of MySQL (8 and 5.7)

This article will introduce how to use Docker to ...

HTML+CSS to create a top navigation bar menu

Navigation bar creation: Technical requirements: ...

Network management and network isolation implementation of Docker containers

1. Docker network management 1. Docker container ...

MySQL Daemon failed to start error solution

MySQL Daemon failed to start error solution A few...

Using System.Drawing.Common in Linux/Docker

Preface After the project is migrated to .net cor...

Vue3.0 adaptive operation of computers with different resolutions

First we need to install some dependencies npm i ...

Solution to elementui's el-popover style modification not taking effect

When using element-ui, there is a commonly used c...

Comparison of various ways to measure the performance of JavaScript functions

Table of contents Overview Performance.now Consol...

Detailed explanation of MySQL Explain

In daily work, we sometimes run slow queries to r...

Vue practice of preventing multiple clicks

Generally, click events will be divided into diff...

Do you know how many connections a Linux server can handle?

Preface First, let's see how to identify a TC...

Solve the problem of Tomcat10 Catalina log garbled characters

Running environment, Idea2020 version, Tomcat10, ...

Use of Linux dynamic link library

Compared with ordinary programs, dynamic link lib...

Personalized and creative website design examples (30)

Therefore, we made a selection of 30 combinations ...