How to use negative margin technology to achieve average layout in CSS

How to use negative margin technology to achieve average layout in CSS

We usually use float layout to solve the compatibility issues of CSS in IE browser. For example, Taobao uses float layout to implement it. We will see a layout like the one shown below on the Taobao page.

We call this evenly distributed layout of pictures an average layout. So how is this layout achieved? Some students may think that this is not difficult. They can simply set the width of the parent element div to be equal to the sum of the widths of the child elements. Is it really that easy? Let’s try it.

test

If we set the width of each child element to 100px, there are 3 child elements in total, and give each child element a margin-right of 50px, then the width of the parent element should be 100x3+50x2=400px. Let's set some styles. The html and css codes are as follows.

//HTML
<div class="father clearfix">
   <div class="son"></div>
   <div class="son"></div>
   <div class="son"></div>
</div>
//CSS
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

.clearfix::after {
  content: '';
  display: block;
  clear: both;
}

.father {
  width: 400px;
  height: 100px;
  margin: 10px;
  background-color: #d5a4cf;
  border: 1px solid black;
}

.son {
  border: 1px solid black;
  background-color: #fff1ac;
  float: left;
  margin-right: 50px;
  width: 100px;
  height: 100px;
}

The effect is as follows

We found that the last child element was pushed down. Why is that? Don’t ask why, because that’s CSS. So how should we solve this problem? Some students said that it is affected by the 1px border of the parent element. Let’s try changing it to outline.

We found that it still didn’t work. What should we do now? Next, I will introduce a “negative margin” method. We add a middle layer between the parent element and the child element, and set the margin-right of this div to -50px. Let's try it again.

//HTML
<div class="father clearfix">
  <div class="middle clearfix">
    <div class="son"></div>
    <div class="son"></div>
    <div class="son"></div>
  </div>
</div>
//CSS
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

.clearfix::after {
  content: '';
  display: block;
  clear: both;
}

.father {
  width: 400px;
  height: 100px;
  margin: 10px;
  background-color: #d5a4cf;
  outline: 1px solid black;
}

.son {
  border: 1px solid black;
  background-color: #fff1ac;
  float: left;
  margin-right: 50px;
  width: 100px;
  height: 100px;
}

.middle {
  margin-right: -50px;
}

At this time, we found that the middle div "wrapped" the three child elements by "extending" the right margin, thus achieving an average layout .

in conclusion

Through our testing, we found a solution to the average layout problem, and we hope it will be helpful to everyone. For more information about CSS average layout, please pay attention to other related articles on 123WORDPRESS.COM!

<<:  Why can't my tomcat start?

>>:  Good website copywriting and good user experience

Recommend

Sending emails in html is easy with Mailto

Recently, I added a click-to-send email function t...

MySQL 5.5.56 installation-free version configuration method

The configuration method of MySQL 5.5.56 free ins...

Detailed explanation of the solution to Ubuntu dual system stuck when starting

Solution to Ubuntu dual system stuck when startin...

Detailed tutorial for installing MySQL 8.0.11 compressed version under win10

After reinstalling my computer recently, I downlo...

MySQL 8.0.13 download and installation tutorial with pictures and text

MySQL is the most commonly used database. You mus...

VPS builds offline download server (post-network disk era)

motivation Due to learning needs, I purchased a v...

MySQL 8.0.25 installation and configuration method graphic tutorial

The latest download and installation tutorial of ...

Detailed implementation plan of Vue front-end exporting Excel files

Table of contents 1. Technology Selection 2. Tech...

How to install PostgreSQL and PostGIS using yum on CentOS7

1. Update the yum source The PostgreSQL version o...

Complete the search function in the html page

Recently I've been working on a framework tha...

How to use map to allow multiple domain names to cross domains in Nginx

Common Nginx configuration allows cross-domain se...

Detailed explanation of how Angular handles unexpected exception errors

Written in front No matter how well the code is w...

Detailed explanation of the use of MySQL group links

Grouping and linking in MYSQL are the two most co...