Example of using negative margin to achieve average layout in CSS

Example of using negative margin to achieve average layout in CSS

For evenly distributed layouts, we generally use the negative margin method. The following figure shows an average layout.

In this case, we usually add a div between the parent element and the child element, and then set a negative margin-right for this div, with the value being the distance between the two child elements.
For example, 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. Above code:

//HTML
<div class="father">
    <div class="middle">
        <div class="son1"></div>
        <div class="son2"></div>
        <div class="son3"></div>
    </div>
</div>
//CSS
.son1,.son2,son3{
    margin-right:50px;
    width:100px;
}
.father{
    width:400px;
}
.middle{
    margin-right:-50px;
}

The middle layer margin-right:-50px is equivalent to "extending" 50px to the right, so that the son3 element has space to be in the same line as other elements.

This concludes this article about examples of using CSS to achieve average layout with negative margins. For more information about CSS average layout with negative margins, please search previous articles on 123WORDPRESS.COM or continue browsing the related articles below. I hope you will support 123WORDPRESS.COM in the future!

<<:  Simple implementation of html hiding scroll bar

>>:  The effect of zooming in on a Taobao store is similar to the principle of using a slideshow.

Recommend

CSS3 Tab animation example background switching dynamic effect

CSS 3 animation example - dynamic effect of Tab b...

JavaScript Canvas implements Tic-Tac-Toe game

This article shares the specific code of JavaScri...

border-radius method to add rounded borders to elements

border-radius:10px; /* All corners are rounded wi...

HTML sub tag and sup tag

Today I will introduce two HTML tags that I don’t...

Detailed explanation of views in MySQL

view: Views in MySQL have many similarities with ...

Complete steps to use mock.js in Vue project

Using mock.js in Vue project Development tool sel...

Methods for deploying MySQL services in Docker and the pitfalls encountered

I have been learning porters recently. I feel lik...

MySQL 5.7 installation and configuration tutorial under CentOS7 (YUM)

Installation environment: CentOS7 64-bit, MySQL5....

Detailed explanation of CSS text decoration text-decoration &amp; text-emphasis

In CSS, text is one of the most common things we ...

This article teaches you how to play with CSS border

Border Style The border-style property specifies ...

React implements import and export of Excel files

Table of contents Presentation Layer Business Lay...

Command to view binlog file creation time in Linux

Table of contents background analyze method backg...

Vue3 encapsulates its own paging component

This article example shares the specific code of ...

Example code for converting Mysql query result set into JSON data

Mysql converts query result set into JSON data Pr...