html+css 布局问题(定位项目)

Problems with html+css layout (positioning items)

我有这个代码:

我的fiddle:https://jsfiddle.net/v5j3L6e9/

基本上,我有 6 个框(带有图像和文字的 Div)。

我正在尝试将 "bottomzone" 居中(例如,这可能是一个液体布局,顶部区域是 header,然后是主要内容,max-width)

想法是"bottomzone"有一个固定的最大宽度,所以当分辨率较大时,它应该保持居中(同时"topzone"会随着分辨率的宽度而扩展)。

我不明白如何在不破坏盒子定位的情况下使底部区域居中(盒子必须保持分布并保持居中)。

此外,不确定我是否做了一个好的元素层次结构(在 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>

将这些 CSS 规则添加到 .bottomzone 选择器:

margin-left: auto;
margin-right: auto;

这可确保浏览器计算出相同数量的 space 用于左右边距。由于您在代码中使用 margin-top,您可以像这样简化它:

.bottomzone {
  margin: 50px auto 0;
  ...
}

其中 0 表示下边距。

如果您想使用 flex-box,您可以简单地使用包装元素 centering-wrapper 并设置 align-items: centerflex-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>