search
HomeWeb Front-endCSS TutorialMagical use of negative margin in css layout and other implementations

I believe everyone has encountered such a need in project development. X (X>1) blocks should be placed in one row and the spacing between adjacent blocks should be the same.


Magical use of negative margin in css layout and other implementations

It probably looks like the above. Here are several ways to implement it.

1. Negative margin method

Set the width and margin of the element to fill the width of the parent, and then set the margin-left of the parent to be blank The width of negative white space


CSS CodeCopy content to clipboard

<style type="text/css">    
*{   
 margin: 0;   
 padding: 0;   
}   
img{   
 vertical-align: middle;   
}   
  
ul>li{   
 float: left;   
}   
  
ul>li>img{   
 width: 100%;   
}   
    
.test1{   
padding: 0 2%;   
margin-left: -3.3%;   
}   
    
.test1>li{   
width: 30%;   
margin-left: 3.3%;   
}   
  
</style>   
 <p>1.关于负margin的实现,由于margin是基于父级计算的,会有一定的偏差,但是用于移动端上,误差可以忽略不计</p>   
        <ul class="test1 clearfix">   
            <li><img  src="/static/imghwm/default1.png"  data-src="img/test.jpg"  class="lazy"  / alt="Magical use of negative margin in css layout and other implementations" ></li>   
            <li><img  src="/static/imghwm/default1.png"  data-src="img/test.jpg"  class="lazy"  / alt="Magical use of negative margin in css layout and other implementations" ></li>   
            <li><img  src="/static/imghwm/default1.png"  data-src="img/test.jpg"  class="lazy"  / alt="Magical use of negative margin in css layout and other implementations" ></li>   
        </ul>


The above error is caused by the calculation of the margin percentage of ul and li based on different elements. However, on the mobile terminal, due to the limited range of the window, the difference is very small. On PC, px is generally used. , so it can be ignored. (There are more ways below)

2. For the implementation of major websites, fill in the elements and use box-sizing. It requires ie8 and above to support


CSS CodeCopy content to the clipboard

<style type="text/css">   
*{   
    margin: 0;   
    padding: 0;   
}   
img{   
    vertical-align: middle;   
}   
.test1{   
    padding: 0 2%;   
    margin-left: -3.3%;   
}   
ul>li{   
    float: left;   
}   
.test1>li{   
    width: 30%;   
    margin-left: 3.3%;   
}   
ul>li>img{   
    width: 100%;   
}   
.test2>li{   
    width: 33.3%;   
    padding: 0 2%;   
    box-sizing: border-box;   
}   
.test3{   
    display: flex;   
    justify-content: space-between;   
       
}   
.test3>li{   
    width: 31.3%;   
    padding: 0 2%;   
    float: none;   
}   
.test4{   
    width: 1200px;   
    border: 1px solid red;   
    margin-left: -3.33%;   
}   
.test4>li{   
    width: 30%;   
    margin-left: 3.33%;   
}   
</style>   
<p>2.各大网站的实现,在元素内部进行填充,使用box-sizing,需要ie8及以上才支持</p>   
        <ul class="test2 clearfix">   
            <li><img  src="/static/imghwm/default1.png"  data-src="img/test.jpg"  class="lazy"  / alt="Magical use of negative margin in css layout and other implementations" ></li>   
            <li><img  src="/static/imghwm/default1.png"  data-src="img/test.jpg"  class="lazy"  / alt="Magical use of negative margin in css layout and other implementations" ></li>   
            <li><img  src="/static/imghwm/default1.png"  data-src="img/test.jpg"  class="lazy"  / alt="Magical use of negative margin in css layout and other implementations" ></li>   
        </ul>

This implementation has not been found yet What are the shortcomings? The code is simple and easy to understand (recommended)

3. The implementation of the flexible box model flex requires compatibility processing (old box + new box)


CSS CodeCopy content to clipboard

<style type="text/css">   
*{   
    margin: 0;   
    padding: 0;   
}   
img{   
    vertical-align: middle;   
}   
.test1{   
    padding: 0 2%;   
    margin-left: -3.3%;   
}   
ul>li{   
    float: left;   
}   
.test1>li{   
    width: 30%;   
    margin-left: 3.3%;   
}   
ul>li>img{   
    width: 100%;   
}   
.test2>li{   
    width: 33.3%;   
    padding: 0 2%;   
    box-sizing: border-box;   
}   
.test3{   
    display: flex;   
    justify-content: space-between;   
       
}   
.test3>li{   
    width: 31.3%;   
    padding: 0 2%;   
    float: none;   
}   
.test4{   
    width: 1200px;   
    border: 1px solid red;   
    margin-left: -3.33%;   
}   
.test4>li{   
    width: 30%;   
    margin-left: 3.33%;   
}   
</style>   
<p>3.弹性盒模型flex的实现,需要做兼容处理(旧盒子+新盒子),仅为演示,没做兼容处理</p>   
        <ul class="test3">   
            <li><img  src="/static/imghwm/default1.png"  data-src="img/test.jpg"  class="lazy"  / alt="Magical use of negative margin in css layout and other implementations" ></li>   
            <li><img  src="/static/imghwm/default1.png"  data-src="img/test.jpg"  class="lazy"  / alt="Magical use of negative margin in css layout and other implementations" ></li>   
            <li><img  src="/static/imghwm/default1.png"  data-src="img/test.jpg"  class="lazy"  / alt="Magical use of negative margin in css layout and other implementations" ></li>   
        </ul>

How can we do without flex in this situation? , the flexible box model should be specially designed to handle this situation, but there are old and new box models, and each browser implements them differently. Therefore, in general, the attributes of both sets of box models need to be added. (Just do it if you like, the effect is great)

4.classname implementation

Add a separate class to the elements that need special processing, and then do the corresponding deal with. It can be processed in the background or front-end (backend processing is recommended)


CSS CodeCopy content to the clipboard

<style type="text/css">   
*{   
    margin: 0;   
    padding: 0;   
}   
img{   
    vertical-align: middle;   
}   
.test1{   
    padding: 0 2%;   
    margin-left: -3.3%;   
}   
ul>li{   
    float: left;   
}   
.test1>li{   
    width: 30%;   
    margin-left: 3.3%;   
}   
ul>li>img{   
    width: 100%;   
}   
.test2>li{   
    width: 33.3%;   
    padding: 0 2%;   
    box-sizing: border-box;   
}   
.test3{   
    display: flex;   
    justify-content: space-between;   
       
}   
.test3>li{   
    width: 31.3%;   
    padding: 0 2%;   
    float: none;   
}   
.test4{   
    padding: 0 2%;   
}   
.test4>li{   
    width: 30%;   
    margin-left: 5%;   
}   
.test4>li.first{   
    margin: 0;   
}   
.test5{   
    padding: 0 2%;   
}   
.test5>li{   
    width: 30%;   
    margin-left: 5%;   
}   
.test5>li:first-child{   
    margin: 0;   
}   
</style>   
<p>4.classname实现</p>   
        <ul class="test4 clearfix">   
            <li class="first"><img  src="/static/imghwm/default1.png"  data-src="img/test.jpg"  class="lazy"  / alt="Magical use of negative margin in css layout and other implementations" ></li>   
            <li><img  src="/static/imghwm/default1.png"  data-src="img/test.jpg"  class="lazy"  / alt="Magical use of negative margin in css layout and other implementations" ></li>   
            <li><img  src="/static/imghwm/default1.png"  data-src="img/test.jpg"  class="lazy"  / alt="Magical use of negative margin in css layout and other implementations" ></li>   
        </ul>

5.css selector implementation

:first-child :first-type-of :nth-child() There is no technology in these implementations Difficulty, you can check the css document and pay attention to the compatibility.


##CSS CodeCopy the content to the clipboard

<style type="text/css">   
*{   
    margin: 0;   
    padding: 0;   
}   
img{   
    vertical-align: middle;   
}   
.test1{   
    padding: 0 2%;   
    margin-left: -3.3%;   
}   
ul>li{   
    float: left;   
}   
.test1>li{   
    width: 30%;   
    margin-left: 3.3%;   
}   
ul>li>img{   
    width: 100%;   
}   
.test2>li{   
    width: 33.3%;   
    padding: 0 2%;   
    box-sizing: border-box;   
}   
.test3{   
    display: flex;   
    justify-content: space-between;   
       
}   
.test3>li{   
    width: 31.3%;   
    padding: 0 2%;   
    float: none;   
}   
.test4{   
    padding: 0 2%;   
}   
.test4>li{   
    width: 30%;   
    margin-left: 5%;   
}   
.test4>li.first{   
    margin: 0;   
}   
.test5{   
    padding: 0 2%;   
}   
.test5>li{   
    width: 30%;   
    margin-left: 5%;   
}   
.test5>li:first-child{   
    margin: 0;   
}   
</style>   
<p>5.css选择器实现(注意ie兼容性)</p>   
        <ul class="test5 clearfix">   
            <li><img  src="/static/imghwm/default1.png"  data-src="img/test.jpg"  class="lazy"  / alt="Magical use of negative margin in css layout and other implementations" ></li>   
            <li><img  src="/static/imghwm/default1.png"  data-src="img/test.jpg"  class="lazy"  / alt="Magical use of negative margin in css layout and other implementations" ></li>   
            <li><img  src="/static/imghwm/default1.png"  data-src="img/test.jpg"  class="lazy"  / alt="Magical use of negative margin in css layout and other implementations" ></li>   
        </ul>

Paste all the DEMO


I almost forgot that there is another situation when X=2, set the width, float left on the left and float right on the right.

In fact, when X=3, there is another way to deal with it. The left and right elements float left and right respectively, and the middle element is set to be absolutely positioned and centered relative to the parent.

Note that due to the indivisibility, it cannot be calculated as perfectly as box-sizing, but reasonable application is no problem at all in the project.

The above is the entire content of this article. I hope it will be helpful to everyone's learning. I also hope that everyone will support the PHP Chinese website.

Please pay attention to the PHP Chinese website for more wonderful uses of negative margins in css layout and other implementation-related articles!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
How much specificity do @rules have, like @keyframes and @media?How much specificity do @rules have, like @keyframes and @media?Apr 18, 2025 am 11:34 AM

I got this question the other day. My first thought is: weird question! Specificity is about selectors, and at-rules are not selectors, so... irrelevant?

Can you nest @media and @support queries?Can you nest @media and @support queries?Apr 18, 2025 am 11:32 AM

Yes, you can, and it doesn't really matter in what order. A CSS preprocessor is not required. It works in regular CSS.

Quick Gulp Cache BustingQuick Gulp Cache BustingApr 18, 2025 am 11:23 AM

You should for sure be setting far-out cache headers on your assets like CSS and JavaScript (and images and fonts and whatever else). That tells the browser

In Search of a Stack That Monitors the Quality and Complexity of CSSIn Search of a Stack That Monitors the Quality and Complexity of CSSApr 18, 2025 am 11:22 AM

Many developers write about how to maintain a CSS codebase, yet not a lot of them write about how they measure the quality of that codebase. Sure, we have

Datalist is for suggesting values without enforcing valuesDatalist is for suggesting values without enforcing valuesApr 18, 2025 am 11:08 AM

Have you ever had a form that needed to accept a short, arbitrary bit of text? Like a name or whatever. That's exactly what is for. There are lots of

Front Conference in ZürichFront Conference in ZürichApr 18, 2025 am 11:03 AM

I'm so excited to be heading to Zürich, Switzerland for Front Conference (Love that name and URL!). I've never been to Switzerland before, so I'm excited

Building a Full-Stack Serverless Application with Cloudflare WorkersBuilding a Full-Stack Serverless Application with Cloudflare WorkersApr 18, 2025 am 10:58 AM

One of my favorite developments in software development has been the advent of serverless. As a developer who has a tendency to get bogged down in the details

Creating Dynamic Routes in a Nuxt ApplicationCreating Dynamic Routes in a Nuxt ApplicationApr 18, 2025 am 10:53 AM

In this post, we’ll be using an ecommerce store demo I built and deployed to Netlify to show how we can make dynamic routes for incoming data. It’s a fairly

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Will R.E.P.O. Have Crossplay?
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor