当前位置:网站首页>解决flex布局warp自动换行下最后一行居中问题
解决flex布局warp自动换行下最后一行居中问题
2022-08-02 03:26:00 【rockage】
1、现象:
- 这是一段很简单的flex弹性布局代码:
HTML:
<div>
<div class="flex-test">
<div class="box-test">1</div>
<div class="box-test">2</div>
<div class="box-test">3</div>
<div class="box-test">4</div>
<div class="box-test">5</div>
<div class="box-test">6</div>
<div class="box-test">7</div>
<div class="box-test">8</div>
</div>
</div>
CSS这边是这样:
<style> .flex-test {
display: flex; flex-direction: row; flex-wrap: wrap; justify-content: start; background: blue; } .box-test {
height: 100px; width: 100px; background: red; margin: 10px; } </style>
- justify-content: start 的时候,结果是这样的:
- 现在,我们需要将所有红色格子整体居中,很简单:
将 justify-content: start
改成:justify-content: center
就行了,结果如下:
- 嗯,OK ! 是我们想要的效果!
- 接下来,将可视宽度收窄(比如在手机上显示),变成了这样:
- 纳尼?怎么变成这个鬼样子了?flex 布局下,warp之后自动换行,换行后的内容又被居中了。这不太符合一般的排版规则,看起来怪怪的不是吗?
2、解决
- 搜了很多资料,发现这个问题还不怎么好解决,算是 flex 弹性布局的一个缺陷吧,如果要最后一行与第一行对齐,我们只能将 justify-content 设为 start, 但这样一来就失去了居中的效果。屏幕右边会空出一截来。但是,为了居中,我们将 justify-content 设为 center后,最后一行又非常别扭地立在屏幕正中,也很突兀。
- 解决方案是换用grid布局:
HTML:
<div class="grid-test">
<div class="box-test">1</div>
<div class="box-test">2</div>
<div class="box-test">3</div>
<div class="box-test">4</div>
<div class="box-test">5</div>
<div class="box-test">6</div>
<div class="box-test">7</div>
<div class="box-test">8</div>
</div>
</div>
CSS:
.grid-test {
display: grid;
grid-template-columns: repeat(auto-fill, 100px);
grid-gap: 10px;
justify-content: center;
background: blue;
}
.box-test {
height: 100px;
width: 100px;
background: red;
margin: 10px;
}
- 换用以上代码后,显示如下:
这是我们想要的效果了,<( ̄︶ ̄)[GO!]~~~
后记
- 一直觉得flex弹性布局是HTML排版的终极方案了,没想到也有不周全的地方,看来还是得双修才是正道。
边栏推荐
猜你喜欢
链动2+1无限循环系统,2022年起盘成功率超高的模式
C language uses stack to calculate infix expressions
Pycharm打包项目为exe文件
3000字入门图神经网络
文件包含漏洞
uniapp | Compilation error after updating with npm update
The shooting range that web penetration must play - DVWA shooting range 1 (centos8.2+phpstudy installation environment)
SQL注入(6)
CSRF (Cross Site Request Forgery)
After Alibaba Cloud sets up domain name resolution redirection, I cannot use Chrome to access it
随机推荐
hackmyvm: may walkthrough
c语言用栈实现计算中缀表达式
The focus of the Dom implementation input triggers
广告电商「私域打工人」职业前景:你离月薪6万,还差多远?
xxe of CTF
英语每日打卡
[sebastian/diff] A historical change extension library for comparing two texts
hackmyvm: juggling walkthrough
PHP realizes the automatic reverse search prompt of the search box
重点考:金融资产概述、交易性金融资产的概念、交易性金融资产的账务处理(取得、持有。出售)、
Smart Tips for Frida Scripting in Kali Environment
hackmyvm-hopper walkthrough
Pycharm打包项目为exe文件
DarkHole: 2 vulnhub walkthrough
一个网络安全小白鼠的学习之路—nmap高级用法之脚本使用
hackmyvm: controller walkthrough
web渗透必玩的靶场——DVWA靶场 1(centos8.2+phpstudy安装环境)
Mysql创建索引
元宇宙是一个炒作的科幻概念,还是互联网发展的下半场?
Kali install IDEA