为什么我不能在以下方案中对齐div /为什么最后一个容器不可见?

时间:2017-09-16 17:47:01

标签: html css web css-position box-sizing

我想在另一个父容器中对齐七个div,它们应该具有相同的高度(作为父容器)和宽度,除了第一个和最后一个。

我尝试过类似的东西:

HTML

<div id="weatherBar">
  <div class="arrow"></div>
  <div class="table-wrapper">
    <div>1</div>
  </div>
  <div class="table-wrapper">
    <div>2</div>
  </div>
  <div class="table-wrapper">
    <div>3</div>
  </div>
  <div class="table-wrapper">
    <div>4</div>
  </div>
  <div class="table-wrapper">
    <div>5</div>
  </div>
  <div class="arrow"></div>
</div>

CSS

div#weatherBar {
  position: relative;
  width: 60vw;
  max-width: 60vw;
  height: 10vh;
  left: 50%;
  transform: translateX(-50%);
  background-color: rgb(255, 255, 255);
  overflow: hidden;
  margin-top: 10vh;
}

div#weatherBar div.table-wrapper {
  min-height: 10vh;
  max-height: 10vh;
  min-width: 10vw;
  max-width: 10vw;
  border: solid 0.5px blue;
  float: left;
  overflow: hidden;
}

div#weatherBar div.arrow {
  min-height: 10vh;
  max-height: 10vh;
  min-width: 5vw;
  max-width: 5vw;
  float: left;
  overflow: hidden;
  background-color: red;
}

但是会发生的是最后一个div没有出现(我附上了一张图片),我不明白为什么。你能救我吗?

提前谢谢! : - )

Attached image

1 个答案:

答案 0 :(得分:1)

默认情况下,border元素未包含width元素,因为box-sizing默认为content-box

将其更改为border-box,以便width中的包含的边框使用:

* {
  box-sizing: border-box;
}

见下面的演示:

&#13;
&#13;
* {
  box-sizing: border-box;
}
div#weatherBar {
  position: relative;
  width: 60vw;
  max-width: 60vw;
  height: 10vh;
  left: 50%;
  transform: translateX(-50%);
  background-color: rgb(255, 255, 255);
  overflow: hidden;
  margin-top: 10vh;
}

div#weatherBar div.table-wrapper {
  min-height: 10vh;
  max-height: 10vh;
  min-width: 10vw;
  max-width: 10vw;
  border: solid 0.5px blue;
  float: left;
  overflow: hidden;
}

div#weatherBar div.arrow {
  min-height: 10vh;
  max-height: 10vh;
  min-width: 5vw;
  max-width: 5vw;
  float: left;
  overflow: hidden;
  background-color: red;
}
&#13;
<div id="weatherBar">
  <div class="arrow"></div>
  <div class="table-wrapper">
    <div>1</div>
  </div>
  <div class="table-wrapper">
    <div>2</div>
  </div>
  <div class="table-wrapper">
    <div>3</div>
  </div>
  <div class="table-wrapper">
    <div>4</div>
  </div>
  <div class="table-wrapper">
    <div>5</div>
  </div>
  <div class="arrow"></div>
</div>
&#13;
&#13;
&#13;