【问题标题】:DIV borders not lining up [duplicate]DIV边界没有对齐[重复]
【发布时间】:2020-07-27 03:16:16
【问题描述】:

我正在尝试使用弹性框创建一个包含 3 个 div 的表格,但是当我为 div 添加边框时,边框并没有在下一个 div 的顶部对齐。任何有鹰眼斑点的人我都想念什么? 谢谢!

.content div {
  width: 350px;
  border: solid;
  padding: 6px;
}

p {
  font-size: medium;
  font-weight: 700;
  color: #000;
  margin-left: 10px;
}

.content {
  height: 50px;
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
}

.red {
  display: flex;
  align-items: center;
  justify-content: center;
}

.green {
  display: flex;
  align-items: center;
  justify-content: center;
}

.blue {
  display: flex;
  align-items: center;
  justify-content: center;
}
<div class="content">
  <div class="red"><img src="https://i.ibb.co/7YJB7QC/flag.png" alt="flag" border="0" width="40" height="40">
    <p>100% Made in the USA</p>
  </div>
  <div class="green"><img src="https://i.ibb.co/dmDyGKH/100.png" alt="100" border="0" width="40" height="40">
    <p>100% Happiness Guaranteed</p>
  </div>
  <div class="blue"><img src="https://i.ibb.co/zmK0Jk0/check.png" alt="check" border="0" width="40" height="40">
    <p>100% Secure Checkout</p>
  </div>
  <table>
  </table>
</div>

【问题讨论】:

  • 因为flexbox没有border-collapse?是这个意思吗?
  • 他们正在排队,但边框围绕着整个 div。所以你会看到顶部 div 的底部边框和下一个 div 的顶部边框接触。您可以将中间 div 的border-top 和border-bottom 设置为none。
  • 善用代码 sn-p,@jackstrum。欢迎来到 SO

标签: html css flexbox


【解决方案1】:

如果你指的是因为盒子模型的粗边框,每个元素的边框都在彼此的正下方,看起来边框是 2px。一个很好的技巧是在每个元素上都没有顶部或底部边框,然后调整第一个或最后一个。

这是我的解决方案,我对所有项目应用了无边框底部,并将边框添加回最后一个元素的底部。

.content div {
  width: 350px;
  border: solid;
  padding: 6px;
  border-bottom: none;
}

.content div:last-child {
  border-bottom: solid;
}

.content div {
  width: 350px;
  border: solid;
  padding: 6px;
  border-bottom: none;
}

.content div:last-child {
  border-bottom: solid;
}

p {
  font-size: medium;
  font-weight: 700;
  color: #000;
  margin-left: 10px;
}

.content {
  height: 50px;
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
}

.red {
  display: flex;
  align-items: center;
  justify-content: center;
}

.green {
  display: flex;
  align-items: center;
  justify-content: center;
}

.blue {
  display: flex;
  align-items: center;
  justify-content: center;
}
<div class="content">
  <div class="red"><img src="https://i.ibb.co/7YJB7QC/flag.png" alt="flag" border="0" width="40" height="40">
    <p>100% Made in the USA</p>
  </div>
  <div class="green"><img src="https://i.ibb.co/dmDyGKH/100.png" alt="100" border="0" width="40" height="40">
    <p>100% Happiness Guaranteed</p>
  </div>
  <div class="blue"><img src="https://i.ibb.co/zmK0Jk0/check.png" alt="check" border="0" width="40" height="40">
    <p>100% Secure Checkout</p>
  </div>
</div>

【讨论】:

  • .content div:last-child 将不起作用。
  • 你说得对,我没有看到内容 div 底部的那个空表。杰克,你需要那张空桌子吗?如果您删除它,它将起作用 - 请参阅代码 sn-p 进行更新
【解决方案2】:

Flexbox 项目不会折叠边框。

如果您希望边框像表格边框一样操作,display 它们就像 tabletable-rowtable-cell

div.content {
  width: 350px;
  display: table;
  text-align: center;
  border-spacing: 0;
  border-collapse: collapse;
}

div.content div.row {
  border: solid 2px black;
  display: table-row;
}

div.row p {
  display: table-cell;
}

div.content div.row img {
  height: 40px;
  margin-right: 10px;
  vertical-align: middle;
}
<div class="content">
  <div class="red row">
    <p>
        <img src="https://i.ibb.co/7YJB7QC/flag.png" alt="flag"></img>

      100% Made in the USA
    </p>
  </div>
  <div class="green row">

    <p>
        <img src="https://i.ibb.co/dmDyGKH/100.png" alt="100"></img>
      100% Happiness Guaranteed
    </p>
  </div>
  <div class="blue row">
    <p>
    <img src="https://i.ibb.co/zmK0Jk0/check.png" alt="check"></img>

      100% Secure Checkout
    </p>
   </div>
  </div>

【讨论】:

  • OP 使用了“table”这个词,但它不是 HTML 意义上的表格 - 从他们的 sn-p 看来,这只是他们描述布局的方式(如你所知,为此使用 HTML 表格绝对不是要走的路:) )
  • 在那个表格显示折叠边框中,我认为它是可辩护的。因为它不是真正的柱状数据,你是对的,&lt;table&gt; 和朋友没有位置。 display: table 更好。
【解决方案3】:

尝试设置边框的粗细,例如1px

.content div {
  width: 350px;
  border: 1px solid;
  padding: 6px;
}

【讨论】:

  • 请不要只发布代码作为答案,还要解释您的代码的作用以及它如何解决问题的问题。带有解释的答案通常更有帮助,质量更高,更有可能吸引投票。
猜你喜欢
  • 2012-11-13
  • 2014-08-07
  • 2018-08-25
  • 2019-11-04
  • 1970-01-01
  • 2019-12-26
  • 2021-04-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多