Summary of 4 methods of div+css layout to achieve 2-end alignment of css

Summary of 4 methods of div+css layout to achieve 2-end alignment of css

The div+css layout to achieve 2-end alignment is often used in our web page typesetting. This article will summarize the methods that can be achieved:

HTML structure

Use CSS to align the divs in the demo.

<div class="box">
 <div class="demo">
     <div>1</div>
     <div>2</div> 
     <div>3</div>
 </div>
</div>

1. Negative margin method

This method requires an extra layer of nesting to be implemented, using the spacing of the elements as the margin overflow value of the middle layer.

<style>
.box{
     width:300px;margin:auto;overflow:hidden;border:1px solid #ddd;
}
.box .demo{
    margin-left:-10px;width:310px
}
.box .demo div{
     width:93.333px;/*(calculation: (300-10*2)/3)*/
     float:left;
     margin-left:10px;
}
</style>

2.display:inline-block/text-align:justify method

The justify method is simpler and more convenient. As long as a simple element is declared, the elements inside will be automatically aligned and laid out with equal spacing! There is no need to calculate the margin spacing between each list element, let alone modify the width of the parent container.

Note: The elements in the demo structure must contain [line breaks] or [spaces], otherwise writing them directly will not work.

<style>
.demo{
     margin:0;padding:0;
     text-align:justify;
     text-align-last:justify;/*Solve IE support*/
     line-height:0;/*Solve the extra blank space at the bottom of the standard browser container*/
}
@media all and (-webkit-min-device-pixel-ratio:0) {
  .demo{
     font-size:0;/*webkit clears the extra space in the last element after using [line break] or [space character] in the element*/
  }
}
.demo:after{/*text-align-last:justify is only supported by IE, standard browsers need to use the .demo:after pseudo-class to simulate a similar effect*/
     display:inline-block;
     overflow:hidden;
     width:100%;
     height:0;
     content:'';
     vertical-align:top;/*Opera browser solves the extra space at the bottom*/
}
.demo div{
     width:20%;
     display:inline-block;
     text-align:center;/*Cancel the influence of upper elements*/
     text-align-last:center;
     font-size:12px;
}
</style>

3. CSS3 property space-between

This method is based on webapp development based on the webkit kernel and winphone IE10 and above, and is often used for mobile terminal layout.

<style>
.demo{
    display:-webkit-box;
    display:-webkit-flex;
    display:-ms-flexbox;
    display:flex;
    -webkit-box-pack:justify;
    -webkit-justify-content:space-between;
    -ms-flex-pack:justify;
    justify-content:space-between;
}

.demo div{
     width:30%; 
}
</style>

4. CSS3 property column-count

The column attribute is a multi-column layout. To use column to achieve alignment at both ends, you only need to set the number of modules to be consistent with the number of columns. It is recommended for mobile terminal layout.

<style>
.demo{
     -webkit-column-count:3;-moz-column-count:3;column-count:3;
     -webkit-column-gap:10px;-moz-column-gap:10px;column-gap:10px; 
}

.demo div{
     
}
</style>

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

<<:  Six methods for nginx optimization

>>:  Pay attention to the use of HTML tags in web page creation

Recommend

MySQL5.7.21 decompressed version installation detailed tutorial diagram

Since I often install the system, I have to reins...

Detailed explanation of the use of CSS pointer-events attribute

In front-end development, we are in direct contac...

How to use Docker to build a tomcat cluster using nginx (with pictures and text)

First, create a tomcat folder. To facilitate the ...

How to view and set the mysql time zone

1. Check the database time zone show variables li...

Why node.js is not suitable for large projects

Table of contents Preface 1. Application componen...

Implementation of breakpoint resume in vue-video-player

In a recent project, I needed to implement the fu...

Detailed tutorial on downloading mysql on Windows 10

MySQL versions are divided into Enterprise Editio...

How to develop uniapp using vscode

Because I have always used vscode to develop fron...

Detailed explanation of Linux command unzip

Table of contents 1. unzip command 1.1 Syntax 1.2...

React event mechanism source code analysis

Table of contents Principle Source code analysis ...

Docker network principles and detailed analysis of custom networks

Docker virtualizes a bridge on the host machine. ...

Practice of using Vite2+Vue3 to render Markdown documents

Table of contents Custom Vite plugins Using vite-...

A brief introduction to the command line tool mycli for operating MySQL database

GitHub has all kinds of magic tools. Today I foun...

JavaScript Closures Explained

Table of contents 1. What is a closure? 1.2 Memoi...