This article mainly introduces how to evenly distribute elements using CSS3 flex layout. I'll leave a note for myself and share it with you. The details are as follows: Example 1: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Flex Layout</title> <style> *{ padding:0; margin:0; } body,html{ height: 100vh; min-width: 800px; } .container{ display:flex; flex-wrap:wrap; display: -webkit-flex; /* Safari */ } .container>.item{ border: 1px solid black; flex:1; height:100px; background: #abcdef; } </style> </head> <body> <div class="container"> <div class="item"></div> <main class="item"></main> <div class="item"></div> </div> </body> </html> Example 2: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Flex Layout</title> <style> *{ padding:0; margin:0; box-sizing: border-box; } body,html{ width: 100%; height: 100vh; min-width: 800px; display:flex; display: -webkit-flex; /* Safari */ justify-content: center; align-items: center; } .container{ width: 300px; height: 300px; display:flex; display: -webkit-flex; /* Safari */ flex-wrap: wrap; } .container>.item{ flex:0 0 33.3%; height:100px; background: #abcdef; border: 1px solid red; } main{ flex:0 0 33.3%; height:100px; background-color: #ccc; border: 1px solid red; } </style> </head> <body> <div class="container"> <div class="item">left</div> <main>main</main> <div class="item">right</div> <div class="item">left</div> <main>main</main> <div class="item">right</div> <div class="item">left</div> <main>main</main> <div class="item">right</div> </div> </body> </html> flex:0 0 33.3% is equivalent to flex-basis:33.3%, which makes the width of each element 33.3% of the outer container, so a maximum of three elements can be arranged in each row. flex-wrap:wrap means that each line will automatically wrap when it is full. Example 3: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Flex Layout</title> <style> *{ padding:0; margin:0; box-sizing: border-box; } body,html{ width: 100%; height: 100vh; min-width: 800px; display:flex; display: -webkit-flex; /* Safari */ justify-content: center; align-items: center; } .container{ width: 300px; height: 300px; margin: 50px; display:flex; display: -webkit-flex; /* Safari */ flex-wrap: wrap; justify-content: space-between; } .container>.item{ flex:0 0 30%; height:90px; background: #abcdef; border: 1px solid red; } main{ flex:0 0 30%; height:90px; background-color: #ccc; border: 1px solid red; } </style> </head> <body> <div class="container"> <div class="item">left</div> <main>main</main> <div class="item">right</div> <div class="item">left</div> <main>main</main> <div class="item">right</div> <div class="item">left</div> <main>main</main> <div class="item">right</div> </div> </body> </html> justify-content:space-between means that the extra space in the main axis direction is evenly distributed between two items. This concludes this article about sample code for evenly distributing elements using CSS3 flex layout. For more information on evenly distributing elements using flex, please search previous articles on 123WORDPRESS.COM or continue browsing the following related articles. I hope you will support 123WORDPRESS.COM in the future! |
<<: Detailed View of Hidden Columns in MySQL
>>: Use thead, tfoot, and tbody to create a table
This article is part of a special series on the 2...
For MySQL 5.5, if the character set is not set, t...
Before using idea to write JSP files, you need to...
A recent business involves such a requirement tha...
The database enables slow query logs Modify the c...
Table of contents 1. Scenario description: 2. Cas...
In daily website maintenance and management, a lo...
A word in advance: Suddenly I received a task to ...
Index definition: It is a separate database struc...
Cause of the problem: At first, the default yum s...
Preface lvm (Logical Volume Manager) logical volu...
String extraction without delimiters Question Req...
1. Install the Linux system on the virtual machin...
I have read a lot of knowledge and articles about...
In this blog, I will walk you through the process...