html - 在不使用 CSS Grid 或 flexbox 的情况下,如何在容器中完美地居中网格?
问题描述
好的,所以我认为网格与中心完美对齐,只是意识到它有几个像素。我完全放弃了所有居中的尝试,并在网上查找,但找不到任何东西。
我知道我可以使用 CSS Grids、Flexbox 等,但我正在尝试学习如何在不使用任何帮助的情况下创建网站。所以我可以了解事情背后的原因。
小提琴:
https://jsfiddle.net/8L9ye7nj/5/
网格 HTML:
<div class="box-wrapper">
<div class="box-container">
<div class="box" id="stethoscope">
<div class="box-label">
<p>Book an appointment</p>
</div>
</div>
<div class="box" id="prescription">
<div class="box-label">
<p>Request a repeat prescription</p>
</div>
</div>
<div class="box" id="group">
<div class="box-label">
<p>Join the Patient Group</p>
</div>
</div>
</div>
</div>
网格 CSS:
.box {
float: left;
width: 25%;
height: 300px;
background-color: #252625;
color: #FFF;
position: relative;
padding: 15px;
margin: 0.5%;
}
.box-label {
position: absolute;
bottom: 0;
text-align: center;
background-color: rgba(0,0,0,0.5);
width: 100%;
padding: 7px 0;
left: 0;
}
.box-label:hover {
animation: box-stretch 1s forwards ease-in-out;
cursor: pointer;
}
.box-container {
width: 90%;
}
.box-container::after {
content: "";
clear: both;
display: table;
}
.box-wrapper {
background-color: #B21645;
padding: 30px;
}
解决方案
你怎么能分割盒子并使它们居中?
您可以使用calc使用数学表达式来计算 CSS 中的高度、宽度等。您可以在此处将框的宽度除以三。
.box {
display: inline-block;
width: calc(100% / 3);
}
需要考虑的事项
注意行内块元素之间的空间。您可以在此处阅读更多相关信息。
尽可能避免使用浮点数。大多数使用浮动完成的布局都可以使用 inline-block 来实现。浮动只是意味着获取一个元素,将其放在一侧,并让其他内容围绕它流动。就这样。
box-wrapper
并且box-container
只需要其中一个来将内容包装在里面。
代码片段
body {
margin: 0;
padding: 0;
}
* {
box-sizing: border-box;
}
.box-wrapper {
background-color: #b21645;
padding: 20px;
}
.box {
position: relative;
display: inline-block;
width: calc(100% / 3);
padding: 0 10px;
height: 300px;
overflow: hidden;
}
.box img {
height: 100%;
width: 100%;
object-fit: cover;
object-position: left top;
}
.box-label {
position: absolute;
bottom: 0;
width: calc(100% - 20px);
text-align: center;
background-color: rgba(0, 0, 0, .6);
padding: 10px 0;
transition: padding 0.3s;
}
.box-label:hover {
padding: 25px 0;
}
.box-label p {
font-family: Helvetica;
color: white;
font-size: 20px;
}
<div class="box-wrapper">
<div class="box">
<img src="https://images.unsplash.com/photo-1509027572446-af8401acfdc3?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=ef8f839186c5a6055d2802005b575194&auto=format&fit=crop&w=500&q=60" alt="" />
<div class="box-label">
<p>Some Title Here</p>
</div>
</div><div class="box">
<img src="https://images.unsplash.com/photo-1509027572446-af8401acfdc3?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=ef8f839186c5a6055d2802005b575194&auto=format&fit=crop&w=500&q=60" alt="">
<div class="box-label">
<p>Some Title Here</p>
</div>
</div><div class="box">
<img src="https://images.unsplash.com/photo-1509027572446-af8401acfdc3?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=ef8f839186c5a6055d2802005b575194&auto=format&fit=crop&w=500&q=60" alt="">
<div class="box-label">
<p>Some Title Here</p>
</div>
</div>
</div>
推荐阅读
- typescript - Firebase orderByChild - 检索嵌套对象
- scala - Spark:如何更新列表中的特定元素
- deep-learning - PyTorch AttributeError:“UNet3D”对象没有属性“大小”
- powerapps - 我在 Powerapps 中看不到数据表控件
- javascript - 从带有多个 JSON 对象的 JSON 读取数据以便显示
- r - 在 R 中使用基础图,图标题未出现
- python - 如何使用 requests.session 以便在后续获取请求中保留和重用标头
- python - 迭代时如何获得二维 Numpy 数组中的位置?
- mysql - 获取最新版本表并按其他表名称变量排序的 SQL 查询
- php - 删除早于日期的 WP 帖子的特色图片