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

Should I use UTF-8 or GB2312 encoding when building a website?

Often when we open foreign websites, garbled char...

jQuery implements a simple carousel effect

Hello everyone, today I will share with you the i...

Implementing long shadow of text in less in CSS3

This article mainly introduces how to implement l...

Why does using limit in MySQL affect performance?

First, let me explain the version of MySQL: mysql...

Ansible automated operation and maintenance deployment method for Linux system

Ansible is a new automated operation and maintena...

CSS Sticky Footer Several Implementations

What is "Sticky Footer" The so-called &...

How to use mysqldump to backup MySQL data

1. Introduction to mysqldump mysqldump is a logic...

How to use Nginx to carry rtmp live server

This time we set up an rtmp live broadcast server...

MySQL 8.0.11 installation and configuration method graphic tutorial (win10)

This article records the installation and configu...

Usage of HTML H title tag

The usage of H tags, especially h1, has always bee...

JS uses map to integrate double arrays

Table of contents Preface Simulating data Merged ...

Solve the conflict between docker and vmware

1. Docker startup problem: Problem Solved: You ne...

How to fix the four sides of the table to scroll up, down, left and right

question: When I was doing project statistics rec...