首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >html+css布局问题(定位项目)

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

Stack Overflow用户
提问于 2020-03-19 04:05:21
回答 2查看 61关注 0票数 0

我有这样的代码:

我的小提琴:https://jsfiddle.net/v5j3L6e9/

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

我试着将“底部区域”居中(例如,这可以是一个液体布局,顶部区域作为标题,然后是主要内容,具有最大宽度)

这个想法是“底部区域”有一个固定的最大宽度,所以当分辨率较大时,它应该保持居中(同时“顶部区域”将随着分辨率宽度的增加而扩展)。

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

此外,我不确定我是否做了一个很好的元素层次结构(在html+css上非常新)。

谢谢。

代码语言:javascript
复制
.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%;
}
代码语言:javascript
复制
<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>

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-03-19 04:15:54

如果你想使用flex-box,你可以简单地使用一个包装器元素centering-wrapper并设置align-items: centerflex-direction: column使项目水平居中。

代码语言:javascript
复制
.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%;
}
代码语言:javascript
复制
<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>

票数 0
EN

Stack Overflow用户

发布于 2020-03-19 04:10:05

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

代码语言:javascript
复制
margin-left: auto;
margin-right: auto;

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

代码语言:javascript
复制
.bottomzone {
  margin: 50px auto 0;
  ...
}

其中0表示底部边距。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60746795

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档