当前位置:网站首页>解决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排版的终极方案了,没想到也有不周全的地方,看来还是得双修才是正道。
边栏推荐
- 英语每日打卡
- (3) string
- v-bind usage: class dynamic binding object array style style and function method
- The CTF introductory notes of SQL injection
- The learning path of a network security mouse - the basic use of nmap
- Scrapy crawler encounters redirection 301/302 problem solution
- 文件上传漏洞
- hackmyvm: kitty walkthrough
- Kali环境下Frida编写脚本智能提示
- 解密:链动2+1的商业模式
猜你喜欢

Eric靶机渗透测试通关全教程

Cookie is used to collect the admin privileges CTF foundation problem

How to determine the direction based on two coordinate points on the map

Offensive and defensive world - novice MISC area 1-12

redis未授权访问(4-unacc)

Command Execution Vulnerability

不懂“赚钱逻辑”,你永远都是社会最底层(广告电商)

Phonebook

ES6 iterator explanation example

VIKINGS: 1 vulnhub walkthrough
随机推荐
PHP deserialization vulnerability
英语每日打卡
hackmyvm-hopper预排
(4) 函数、Bug、类与对象、封装、继承、多态、拷贝
Summary of php function vulnerabilities
PHP实现搜索框的自动反查提示
账务处理程序、记账凭证账务处理程序、汇总记账凭证账务处理程序、科目汇总表账务处理程序、会计信息化概述、信息化环境下会计账务处理的基本要求(此章出1道小题)
云安全笔记:云原生全链路加密
Eric靶机渗透测试通关全教程
(1) print()函数、转义字符、二进制与字符编码 、变量、数据类型、input()函数、运算符
A code audit notes (CVE - 2018-12613 phpmyadmin file contains loopholes)
SQL注入(7)
4. PHP array and array sorting
(8) requests, os, sys, re, _thread
战场:3(双子叶植物)vulnhub走读
CTF之xxe
Pycharm打包项目为exe文件
阿里云MySQL5.7安装以及部分主要问题(总和)
Smart Tips for Frida Scripting in Kali Environment
How to calculate the distance between two points on the earth (with formula derivation)