html - 如何用弹性盒子而不是网格来做这个设计?
问题描述
这是我使用 HTML 和 CSS 使用网格制作的设计。但我想用弹性盒子而不是网格来重新制作它。这个设计是用网格做的:
源代码:
演示:
* {
box-sizing: border-box;
}
.parent {
margin: 20px auto;
width: 800px;
height: 500px;
background-color: #ddd;
display: grid;
grid-template-columns: repeat(4, auto);
grid-template-rows: repeat(3, auto);
gap: 10px 10px;
justify-content: space-between;
}
.parent div {
background-color: red;
color: white;
padding: 20px;
font-size: 30px;
font-weight: bold;
text-align: center;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>CSS</title>
<link rel="stylesheet" href="css/master.css" />
</head>
<body>
<div class="parent">
<div>1</div>
<div>2</div>
<div>3</div>
<div>4</div>
<div>5</div>
<div>6</div>
<div>7</div>
<div>8</div>
<div>9</div>
</div>
</body>
</html>
我做了一个看起来像这样但使用弹性盒子的设计:
源代码:
演示:
* {
box-sizing: border-box;
}
.parent {
margin: 20px auto;
width: 800px;
height: 500px;
background-color: #ddd;
display: flex;
flex-wrap: wrap;
justify-content: space-between;
}
.parent div {
background-color: red;
color: white;
padding: 20px;
font-size: 30px;
font-weight: bold;
text-align: center;
border: 0.01px dotted black;
width: 50px;
height: 32%;
margin-right: 100px;
margin-left: 60px;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>CSS</title>
<link rel="stylesheet" href="css/master.css" />
</head>
<body>
<div class="parent">
<div style = "margin-left: 0px;">1</div>
<div>2</div>
<div>3</div>
<div style = "margin-right: 0px;">4</div>
<div style = "margin-left: 0px;">5</div>
<div>6</div>
<div>7</div>
<div style = "margin-right: 0px;">8</div>
<div style = "margin-left: 0px;">9</div>
</div>
</body>
</html>
我的问题:
- 我可以通过 flex box 以更好的方式创建这个设计吗?
解决方案
这就是我的做法,首先避免在 html 标签中使用样式,并使用空格而不是边距来定位元素。
* {
box-sizing: border-box;
}
.parent {
margin: 20px auto;
width: 800px;
height: 500px;
background-color: #ddd;
display: flex;
flex-direction: column;
}
.row {
display: flex;
flex-direction: row;
justify-content: space-between;
height: 100%;
width: 100%;
padding: 5px;
}
.row div {
background-color: red;
color: white;
padding: 20px;
font-size: 30px;
font-weight: bold;
text-align: center;
border: 0.01px dotted black;
width: 50px;
height: 100%;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>CSS</title>
<link rel="stylesheet" href="css/master.css" />
</head>
<body>
<div class="parent">
<div class="row">
<div>1</div>
<div>2</div>
<div>3</div>
<div>4</div>
</div>
<div class="row">
<div>5</div>
<div>6</div>
<div>7</div>
<div>8</div>
</div>
<div class="row">
<div>9</div>
</div>
</div>
</body>
</html>
推荐阅读
- python - 使用对数筛选 B 平滑数的索引超出范围错误
- android - ViewPager2如何更改子绘图顺序?
- php - 在 laravel 中使用 postgres bytea blob 字段上传图片
- android - 360度图像查看器在Android中具有运动和触摸功能
- reactjs - 当我单击 Material Ui 中的按钮时如何转到另一个组件
- java - 在 Piechart Android 中显示单个值
- sql - 两年之间无法获取数据
- wpf - 设置 AllowTransparency="False" 时 Telerik Radwindow 渲染不完整
- python - 在不使用 Matplotlib 的情况下测试点是否在椭圆内?
- pywinauto - pywinauto/examples/uninstall_7zip.py 在 Windows 10 上不起作用