当前位置:网站首页>解决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排版的终极方案了,没想到也有不周全的地方,看来还是得双修才是正道。
边栏推荐
- 英语每日打卡
- GreenOptic: 1 vulnhub walkthrough
- Warzone: 3 (Exogen) vulnhub walkthrough
- hackmyvm: controller walkthrough
- Shuriken: 1 vulnhub walkthrough
- PHP反序列化漏洞
- Phonebook
- 动力:2 vulnhub预排
- web渗透必玩的靶场——DVWA靶场 1(centos8.2+phpstudy安装环境)
- After Alibaba Cloud sets up domain name resolution redirection, I cannot use Chrome to access it
猜你喜欢

Orasi: 1 vulnhub walkthrough

Shuriken: 1 vulnhub walkthrough

CTF-网鼎杯往届题目

12. What is JS

The shooting range that web penetration must play - DVWA shooting range 1 (centos8.2+phpstudy installation environment)

v-bind usage: class dynamic binding object array style style and function method

Alfa: 1 vulnhub walkthrough

hackmyvm: may walkthrough

账务处理程序、记账凭证账务处理程序、汇总记账凭证账务处理程序、科目汇总表账务处理程序、会计信息化概述、信息化环境下会计账务处理的基本要求(此章出1道小题)

(3) string
随机推荐
The learning path of a network security mouse - the basic use of nmap
[sebastian/diff] A historical change extension library for comparing two texts
VIKINGS: 1 vulnhub walkthrough
4. PHP array and array sorting
Pycharm打包项目为exe文件
云安全笔记:云原生全链路加密
敏感信息泄露
(7) superficial "crawlers" process (concept + practice)
pytorch:保存和加载模型
管理会计(对内)指引、管理会计要素及其具体内容(可能考,考前记一下,推荐记一下四个大点即可)、
GreenOptic: 1 vulnhub walkthrough
会计账簿、会计账簿概述、会计账簿的启用与登记要求、会计账簿的格式和登记方法
Smart Tips for Frida Scripting in Kali Environment
文件包含漏洞
命令执行漏洞
hackmyvm: controller walkthrough
同态加密:CKKS原理之旋转(Rotation)
Shuriken: 1 vulnhub walkthrough
借贷记账法下的账户结构、借贷记账法的记账规则、借贷记账法下的账户对应关系与会计分录
hackmyvm: may walkthrough