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

Detailed tutorial for installing mysql 8.0.12 under Windows

This article shares with you a detailed tutorial ...

Front-end advanced teaching you to use javascript storage function

Table of contents Preface Background Implementati...

How to prevent duplicate submission in jquery project

In new projects, axios can prevent duplicate subm...

How to find websites with SQL injection (must read)

Method 1: Use Google advanced search, for example...

Detailed explanation of the difference between alt and title

These two attributes are often used, but their di...

Input file custom button beautification (demo)

I have written such an article before, but I used...

Build a file management system step by step with nginx+FastDFS

Table of contents 1. Introduction to FastDFS 1. I...

Share some uncommon but useful JS techniques

Preface Programming languages ​​usually contain v...

Vue implements a simple marquee effect

This article shares the specific code of Vue to a...

Detailed explanation of javascript knowledge points

Table of contents 1. Basic Introduction to JavaSc...

How to run Python script on Docker

First create a specific project directory for you...

How to distinguish MySQL's innodb_flush_log_at_trx_commit and sync_binlog

The two parameters innodb_flush_log_at_trx_commit...