Home > Web Front-end > CSS Tutorial > Implementation of layout at both ends of CSS layout

Implementation of layout at both ends of CSS layout

Guanhui
Release: 2020-06-28 18:06:21
forward
2735 people have browsed it

Implementation of layout at both ends of CSS layout

Recently during the development process, I encountered a layout that is aligned at both ends. It is laid out according to percentages. I have used flex layout before, but flex layout uses both ends. During layout, all bugs will appear. 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 it is laid out using ordinary css. Because I wrote this.

I found some tutorials on the Internet, and they all operate with hard-coded widths. I changed it into a percentage format and recorded it simply.
First apply 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>
Copy after login

Then add html

 <p class="max-box2">

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

            </p>
            <p class="inner">

            </p>
            <p class="inner">

            </p>
            <p class="inner">

            </p>
        </p>
    </p>
Copy after login

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

Implementation of layout at both ends of CSS layout

Finally summarize the formula

x = 10px; 即:想要的间距
y = 4     即:想要排列的个数
父级:  width: calc(100% + (x * 2));
子级:  width: calc(((100% - (x * 2)) - x * ( y - 1)) / y );
Copy after login

Recommended tutorial: "CSS Tutorial"

The above is the detailed content of Implementation of layout at both ends of CSS layout. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
css
source:jb51.net
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template