Example code of CSS layout at both ends (using parent's negative margin)

Example code of CSS layout at both ends (using parent's negative margin)

Recently, during the development process, I encountered a layout that was aligned at both ends. The layout was based on percentages. I had used flex layout before, but when using flex layout at both ends, all kinds of bugs would occur. For example, when the following is dynamically generated, three or more columns will distribute the following list on both sides.
Although it can be solved, I still want to see how to layout it using ordinary CSS. Because I wrote this.

I found some tutorials online, and they all had fixed widths for operations. I changed it into percentage format and recorded it simply.
First, CSS

<style>
        * {
            padding: 0px;
            margin: 0px;
            box-sizing: border-box;
        }
        
        .max-box {
            max-width: 1200px;
            margin: 0px auto;
        }
        
        .box {
            overflow: hidden;
            width: calc(100% + 20px);
            margin-left: -10px;
        }
        
        .inner {
            height: 100px;
            border: solid 1px red;
            float: left;
            margin-left: 10px;
            width: calc(((100% - 20px) - 10px * 3) / 4);
        }
        
        .max-box2 {
            max-width: 1200px;
            margin: 50px auto;
            border: solid 1px red;
            height: 200px;
        }
    </style>

Then on to html

 <div class="max-box2">

    </div>
    <div class="max-box">
        <div class="box">
            <div class="inner">

            </div>
            <div class="inner">

            </div>
            <div class="inner">

            </div>
            <div class="inner">

            </div>
        </div>
    </div>

Put these into HTML and you can see the effect.
As shown below

insert image description here

Finally, let’s summarize the formula

x = 10px; i.e. the desired spacing y = 4 i.e. the desired number of rows Parent: width: calc(100% + (x * 2));
Child: width: calc(((100% - (x * 2)) - x * ( y - 1)) / y );

Summarize

This is the end of this article about CSS layout - two-end layout (using parent's negative margin). For more relevant CSS layout parent's negative margin content, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

<<:  Detailed explanation of Linux netstat command

>>:  The concrete implementation of JavaScript exclusive thinking

Recommend

What are HTML inline elements and block-level elements and their differences

I remember a question the interviewer asked durin...

JS implements simple example code to control video playback speed

introduction I discovered a problem before: somet...

Install and configure ssh in CentOS7

1. Install openssh-server yum install -y openssl ...

Brief analysis of the introduction and basic usage of Promise

Promise is a new solution for asynchronous progra...

Practical record of solving MySQL deep paging problem

Table of contents Preface Why does limit deep pag...

How to add a disk in Vmware: Expand the disk

This article describes how to add or expand a dis...

Vue project implements graphic verification code

This article example shares the specific code of ...

JavaScript implements the pot-beating game of Gray Wolf

1. Project Documents 2. Use HTML and CSS for page...

Solve the problem of installing Theano on Ubuntu 19

Solution: Directly in the directory where you dow...

Two ways to clear float in HTML

1. Clear floating method 1 Set the height of the ...

Personalized and creative website design examples (30)

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

In-depth analysis of the role of HTML <!--...--> comment tags

When we check the source code of many websites, w...