【问题标题】:Display: inline-block last-child width 100% doesn't align with its siblings显示:inline-block last-child width 100% 与其兄弟不对齐
【发布时间】:2021-05-27 09:21:07
【问题描述】:

我正在使用 CSS display: inline-block

.container div
{
  display: inline-block;
    width: calc((100% - 13px) / 2);
    margin: 2px;
    background: #fff;
    border-radius: 10px;
    color: #5e5e5e;
    cursor: pointer;
    margin-bottom: 6px;
    box-shadow: 0 1px 3px rgba(0,0,0,0.12), 0 1px 2px rgba(0,0,0,0.24);
    transition: all 0.3s cubic-bezier(.25,.8,.25,1);
    border-bottom: 2px solid #80D0C7;
    position: relative;
}

.container div:last-child
{
  width: 100%;
}
<div class="container">
    <div class="red">Red</div>
    <div class="green">green</div>
    <div class="blue">blue</div>
    <div class="orange">orange</div>
    <div class="black">black</div>
</div>

我将width: 100% 放在 div last-child 上。但它提供了额外的填充/边距。 我的问题,如何使 div last-child 适合宽度 100% 而没有任何额外的填充/边距?

【问题讨论】:

    标签: html css padding margin


    【解决方案1】:

    .container 中使用display: flex,然后在所有div 和最后一组flex-basis: 100% 中使用flex-basis: calc(50% - 4px)(其中4px 是每个div 的边距之和)

    .container {
      display: flex;
      flex-wrap: wrap;
    }
    
    .container div {
      background: #fff;
      border-radius: 10px;
      color: #5e5e5e;
      cursor: pointer;
      margin: 2px 2px 6px;
      box-shadow: 0 1px 3px rgba(0, 0, 0, 0.12), 0 1px 2px rgba(0, 0, 0, 0.24);
      transition: all 0.3s cubic-bezier(.25, .8, .25, 1);
      border-bottom: 2px solid #80D0C7;
      position: relative;
      flex: 0 calc(50% - 4px)
    }
    
    .container div:last-child {
      flex-basis: 100%;
    }
    <div class="container">
      <div class="red">Red</div>
      <div class="green">green</div>
      <div class="blue">blue</div>
      <div class="orange">orange</div>
      <div class="black">black</div>
    </div>

    【讨论】:

    • 嗨,它仍然向我们展示了那里的额外边距/填充。请看一下
    • 在哪里?你必须更具体,也许是真正问题的截图
    • 是的,我更新了我的问题。请看一下
    • @HiDayurieDave 看看更新后的答案
    • 太棒了...为您的回答干杯。 (y)
    猜你喜欢
    • 2012-11-02
    • 2013-07-04
    • 2011-12-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-26
    • 1970-01-01
    相关资源
    最近更新 更多