当前位置:网站首页>Multi row and multi column flex layout
Multi row and multi column flex layout
2022-07-07 12:30:00 【Xiaoding Chong duck!】
1、 Three lines and three columns , Margins are all 20px, Every box Width and height are 200px

Use flex Layout , Set the width and height of the outer box to 640px(200*3+20*2), In the use of align-content: space-between and justify-content: space-between that will do , The code is as follows :
<style>
*{
padding:0;
margin: 0;
}
html,body{
width: 100%;
height: 100%;
}
.box{
width: 640px;
background: rgb(243, 171, 171);
height: 640px;
display: flex;
flex-wrap: wrap;
align-content: space-between;
justify-content: space-between;
}
.Abox{
background: yellow;
width: 200px;
height: 200px;
}
</style>
<body>
<div class="box">
<div class="Abox">1</div>
<div class="Abox">2</div>
<div class="Abox">3</div>
<div class="Abox">4</div>
<div class="Abox">5</div>
<div class="Abox">6</div>
<div class="Abox">7</div>
<div class="Abox">8</div>
<div class="Abox">9</div>
</div>
</body>2、 Multiple rows and three columns , Margins are all 20px, Every box Width and height are 200px, Highly adaptive content

Or use align-content: space-between and justify-content: space-between, Height has js calculated , In setting up to dom On :
<style>
*{
padding:0;
margin: 0;
}
html,body{
width: 100%;
height: 100%;
}
.box{
width: 640px;
background: rgb(243, 171, 171);
/* height: 640px; */
display: flex;
flex-wrap: wrap;
align-content: space-between;
justify-content: space-between;
}
.Abox{
background: yellow;
width: 200px;
height: 200px;
}
</style>
<body>
<div class="box">
<div class="Abox">1</div>
<div class="Abox">2</div>
<div class="Abox">3</div>
<div class="Abox">4</div>
<div class="Abox">5</div>
<div class="Abox">6</div>
<div class="Abox">7</div>
<div class="Abox">8</div>
<div class="Abox">9</div>
<div class="Abox">10</div>
<div class="Abox">11</div>
<div class="Abox">12</div>
</div>
</body>
<script>
let parentDom = document.querySelector('.box');
let children = parentDom.children;
let rowNums = Math.ceil(children.length / 3);
parentDom.style.height = rowNums * 200 + (rowNums - 1) * 20 + 'px';
</script>3、 Three rows and many columns , Margins are all 20px, Every box Width and height are 200px, Width adaptive content

Law 1 :
Or use align-content: space-between and justify-content: space-between, At this time, the width is determined by the content , So you can go through js Calculate and dynamically set the width :
<style>
*{
padding:0;
margin: 0;
}
html,body{
width: 100%;
height: 100%;
}
.box{
background: rgb(243, 171, 171);
height: 640px;
display: flex;
flex-wrap: wrap;
flex-direction: column;
align-content: space-between;
justify-content: space-between;
}
.Abox{
background: yellow;
width: 200px;
height: 200px;
}
</style>
<body>
<div class="box">
<div class="Abox">1</div>
<div class="Abox">2</div>
<div class="Abox">3</div>
<div class="Abox">4</div>
<div class="Abox">5</div>
<div class="Abox">6</div>
<div class="Abox">7</div>
<div class="Abox">8</div>
<div class="Abox">9</div>
<div class="Abox">10</div>
<div class="Abox">11</div>
<div class="Abox">12</div>
</div>
</body>
<script>
let parentDom = document.querySelector('.box');
let children = parentDom.children;
let columnNums = Math.ceil(children.length / 3);
parentDom.style.width = columnNums * 200 + (columnNums - 1) * 20 + 'px';
</script>Law two :
Spindle use justify-content: space-between, Cross axis use align-content: flex-start, At this time, there is margin control , Don't use js Control the width of the parent box ,width Set to fit-content:
The effect is as follows , At this time, the width of the parent box is only the width of a list of child elements , I have encountered this problem before , Namely flex-direction: column when , The width of multiple columns of child elements cannot expand the width of parent elements , Therefore, this plan cannot .

4、 Three lines and three columns , Margins are all 20px, Every box Width and height are 200px, Highly adaptive content , Child elements cannot be covered
The method of question two is used , If you find that the last line is less than three, there will be a space in the middle , It's not the effect we want :

Law 1 :
Use not shown div fill , To carry on the placeholder :
<style>
*{
padding:0;
margin: 0;
}
html,body{
width: 100%;
height: 100%;
}
.box{
width: 640px;
height: 640px;
background: rgb(243, 171, 171);
display: flex;
flex-wrap: wrap;
align-content: space-between;
justify-content: space-between;
}
.Abox{
background: yellow;
width: 200px;
height: 200px;
}
</style>
<body>
<div class="box">
<div class="Abox">1</div>
<div class="Abox">2</div>
<div class="Abox">3</div>
<div class="Abox">4</div>
<div class="Abox">5</div>
<div class="Abox">6</div>
<div class="Abox">7</div>
<div class="Abox">8</div>
</div>
</body>
<script>
let parentDom = document.querySelector('.box');
let children = parentDom.children;
let n = children.length % 3;
// Don't cover
if (n) {
while (3 - n > 0) {
let div = document.createElement('div');
div.className = 'Abox';
div.style.visibility = 'hidden';
parentDom.appendChild(div);
n++;
}
}
</script>The effect is as follows :

Law two :
Spindle use justify-content: flex-start, Cross axis use align-content: space-between, The right margin is margin Set up , The last column nth-child(3n) Do not set the right margin :
<style>
*{
padding:0;
margin: 0;
}
html,body{
width: 100%;
height: 100%;
}
.box{
width: 640px;
background: rgb(243, 171, 171);
height: 640px;
display: flex;
flex-wrap: wrap;
align-content: space-between;
justify-content: flex-start;
}
.Abox{
background: yellow;
width: 200px;
height: 200px;
margin-right: 20px;
}
.Abox:nth-child(3n) {
margin: 0;
}
</style>
<body>
<div class="box">
<div class="Abox">1</div>
<div class="Abox">2</div>
<div class="Abox">3</div>
<div class="Abox">4</div>
<div class="Abox">5</div>
<div class="Abox">6</div>
<div class="Abox">7</div>
<div class="Abox">8</div>
</div>
</body>in addition , above 1、2、3 These situations that can be covered can also be realized by using the method of two .
边栏推荐
- 免备案服务器会影响网站排名和权重吗?
- Unity中SmoothStep介绍和应用: 溶解特效优化
- Processing strategy of message queue message loss and repeated message sending
- 【统计学习方法】学习笔记——第五章:决策树
- 顶级域名有哪些?是如何分类的?
- Xiaohongshu microservice framework and governance and other cloud native business architecture evolution cases
- 2022 年第八届“认证杯”中国高校风险管理与控制能力挑战赛
- Is it safe to open Huatai's account in kainiu in 2022?
- ps链接图层的使用方法和快捷键,ps图层链接怎么做的
- Completion report of communication software development and Application
猜你喜欢

【深度学习】图像多标签分类任务,百度PaddleClas

SQL Lab (36~40) includes stack injection, MySQL_ real_ escape_ The difference between string and addslashes (continuous update after)

Up meta - Web3.0 world innovative meta universe financial agreement

"Series after reading" my God! It's so simple to understand throttling and anti shake~

Tutorial on principles and applications of database system (007) -- related concepts of database

【统计学习方法】学习笔记——逻辑斯谛回归和最大熵模型

Unity中SmoothStep介绍和应用: 溶解特效优化

跨域问题解决方案

ps链接图层的使用方法和快捷键,ps图层链接怎么做的

Unity 贴图自动匹配材质工具 贴图自动添加到材质球工具 材质球匹配贴图工具 Substance Painter制作的贴图自动匹配材质球工具
随机推荐
平安证券手机行开户安全吗?
Static routing assignment of network reachable and telent connections
Introduction and application of smoothstep in unity: optimization of dissolution effect
111.网络安全渗透测试—[权限提升篇9]—[Windows 2008 R2内核溢出提权]
千人规模互联网公司研发效能成功之路
解决 Server returns invalid timezone. Go to ‘Advanced’ tab and set ‘serverTimezone’ property manually
Static comprehensive experiment
Visual studio 2019 (localdb) \mssqllocaldb SQL Server 2014 database version is 852 and cannot be opened. This server supports version 782 and earlier
<No. 9> 1805. Number of different integers in the string (simple)
H3C HCl MPLS layer 2 dedicated line experiment
Problem: the string and characters are typed successively, and the results conflict
ps链接图层的使用方法和快捷键,ps图层链接怎么做的
数据库系统原理与应用教程(009)—— 概念模型与数据模型
Attack and defense world - PWN learning notes
idea 2021中文乱码
The hoisting of the upper cylinder of the steel containment of the world's first reactor "linglong-1" reactor building was successful
AirServer自动接收多画面投屏或者跨设备投屏
【统计学习方法】学习笔记——第五章:决策树
【统计学习方法】学习笔记——支持向量机(上)
SQL Lab (46~53) (continuous update later) order by injection