我有这样的代码:
我的小提琴:https://jsfiddle.net/v5j3L6e9/
基本上,我有6个框(带有图像和文本的Div)。
我试着将“底部区域”居中(例如,这可以是一个液体布局,顶部区域作为标题,然后是主要内容,具有最大宽度)
这个想法是“底部区域”有一个固定的最大宽度,所以当分辨率较大时,它应该保持居中(同时“顶部区域”将随着分辨率宽度的增加而扩展)。
我不明白如何在不破坏盒子定位的情况下居中底部区域(盒子必须保持分布并保持居中)。
此外,我不确定我是否做了一个很好的元素层次结构(在html+css上非常新)。
谢谢。
.topzone {
height: 100px;
background-color: blue;
}
.topzone h1 {
text-align: center;
}
.bottomzone {
margin-top: 50px;
max-width: 800px;
text-align: center;
display: flex;
justify-content: center;
flex-wrap: wrap;
}
.bottomzone div {
position: relative;
width: 30%;
margin: 0px 10px 0px 10px;
}
.bottomzone img {
width: 100%;
}
.bottomzone div h3 {
position: absolute;
top: 20px;
width: 100%;
}<div class="topzone">
<h1>Title 1</h1>
<h2>Title 2</h2>
</div>
<div class="bottomzone">
<div>
<img src="https://etc.usf.edu/clipart/21900/21988/square_21988_md.gif" alt="Just a box">
<h3>Box 1</h3>
</div>
<div>
<img src="https://etc.usf.edu/clipart/21900/21988/square_21988_md.gif" alt="Just a box">
<h3>Box 2</h3>
</div>
<div>
<img src="https://etc.usf.edu/clipart/21900/21988/square_21988_md.gif" alt="Just a box">
<h3>Box 3</h3>
</div>
<div>
<img src="https://etc.usf.edu/clipart/21900/21988/square_21988_md.gif" alt="Just a box">
<h3>Box 4</h3>
</div>
<div>
<img src="https://etc.usf.edu/clipart/21900/21988/square_21988_md.gif" alt="Just a box">
<h3>Box 5</h3>
</div>
<div>
<img src="https://etc.usf.edu/clipart/21900/21988/square_21988_md.gif" alt="Just a box">
<h3>Box 6</h3>
</div>
</div>
发布于 2020-03-19 04:15:54
如果你想使用flex-box,你可以简单地使用一个包装器元素centering-wrapper并设置align-items: center和flex-direction: column使项目水平居中。
.centering-wrapper {
display: flex;
flex-direction: column;
align-items: center;
}
.topzone {
height: 100px;
background-color: blue;
}
.topzone h1 {
text-align: center;
}
.bottomzone {
margin-top: 50px;
max-width: 800px;
text-align: center;
display: flex;
justify-content: center;
flex-wrap: wrap;
}
.bottomzone div {
position: relative;
width: 30%;
margin: 0px 10px 0px 10px;
}
.bottomzone img {
width: 100%;
}
.bottomzone div h3 {
position: absolute;
top: 20px;
width: 100%;
}<div class="topzone">
<h1>Title 1</h1>
<h2>Title 2</h2>
</div>
<div class="centering-wrapper">
<div class="bottomzone">
<div>
<img src="https://etc.usf.edu/clipart/21900/21988/square_21988_md.gif" alt="Just a box">
<h3>Box 1</h3>
</div>
<div>
<img src="https://etc.usf.edu/clipart/21900/21988/square_21988_md.gif" alt="Just a box">
<h3>Box 2</h3>
</div>
<div>
<img src="https://etc.usf.edu/clipart/21900/21988/square_21988_md.gif" alt="Just a box">
<h3>Box 3</h3>
</div>
<div>
<img src="https://etc.usf.edu/clipart/21900/21988/square_21988_md.gif" alt="Just a box">
<h3>Box 4</h3>
</div>
<div>
<img src="https://etc.usf.edu/clipart/21900/21988/square_21988_md.gif" alt="Just a box">
<h3>Box 5</h3>
</div>
<div>
<img src="https://etc.usf.edu/clipart/21900/21988/square_21988_md.gif" alt="Just a box">
<h3>Box 6</h3>
</div>
</div>
</div>
发布于 2020-03-19 04:10:05
将这些CSS规则添加到.bottomzone选择器:
margin-left: auto;
margin-right: auto;这可确保浏览器计算出用于左边距和右边距的相同空间量。由于您在代码中使用了margin-top,因此可以这样简化它:
.bottomzone {
margin: 50px auto 0;
...
}其中0表示底部边距。
https://stackoverflow.com/questions/60746795
复制相似问题