【问题标题】:css/html ul table row elements distribute horizontallycss/html ul 表格行元素水平分布
【发布时间】:2018-09-10 10:11:45
【问题描述】:

我试图将三个盒子均匀地分布在整行上。我尝试添加“宽度:100%”并删除最后一个孩子的填充,但它似乎没有解决问题。

最后,三个盒子应该有相同的宽度,分布在行的最大宽度上,它会根据屏幕大小自行调整。

.bottom_section {
  display: table;
  height: 100%;
}

.bottom_section ul {
  display: table-row;
  height: 100%;
}

.bottom_section ul li {
  width: 33.33%;
  height: 100%;
  display: table-cell;
  padding: 0 30px 20px 0;
}

.bottom_section ul li:last-child {
  padding-right: 0;
}

.bottom_section ul li div {
  padding: 25px 15px;
  height: 100%;
  background: right top no-repeat #FFF;
}

.bottom_section ul li div h3 {
  margin-bottom: 10px;
  padding-top: 35px;
  font-size: 18px;
  font-weight: 600;
  color: #FF5C1B;
  text-align: center;
}

.bottom_section ul li:first-child div h3 {
  background: center top no-repeat;
}

.bottom_section ul li:nth-child(2) div h3 {
  background: center top no-repeat;
}

.bottom_section ul li:last-child div h3 {
  background: center top no-repeat;
}

.bottom_section ul li div p {
  font-weight: 300;
  font-size: 14px;
  color: #6e6e6e;
  line-height: 24px;
}
<section class="bottom_section">
  <ul>
    <li>
      <div style="text-align: center;">
        <i class="fa fa-car" style="font-size:60px;color:red;"></i>
        <h3>Some text</h3>
        <p>Some text</p>

      </div>
    </li>
    <li>
      <div style="text-align: center;">
        <i class="fa fa-car" style="font-size:60px;color:red;"></i>
        <h3>Some text</h3>
        <p>Some text</p>

      </div>
    </li>
    <li>
      <div style="text-align: center;">
        <i class="fa fa-car" style="font-size:60px;color:red;"></i>
        <h3>Some text</h3>
        <p>Some text</p>

      </div>
    </li>
  </ul>
</section>

提前感谢任何提示

编辑:在前几个答案之后,似乎实际上分布很好。问题是填充,因为除了最后一个子元素(0px)之外,所有元素都有 30px 的填充权。我向左右添加了填充,并且没有向“第一个孩子”添加左侧填充,但是中间的盒子更小了。仍在尝试并期待优雅的解决方案。谢谢

【问题讨论】:

  • 编辑:在前几个答案之后,似乎实际上分布很好。问题是填充,因为除了最后一个子元素(0px)之外,所有元素都有 30px 的填充权。我向左右添加了填充,并且没有向“第一个孩子”添加左侧填充,但是中间的盒子更小了。仍在尝试并期待优雅的解决方案。谢谢

标签: html css html-table html-lists


【解决方案1】:

由于您需要一维格式,我建议使用flexbox

.bottom_section {}

.bottom_section ul {
  display: flex;
  list-style: none;
  width: 100%;
  margin: 0;
  padding: 0;
}

.bottom_section ul li {
  width: 33.33%;
  padding: 0 30px 20px 0;
}

.bottom_section ul li:last-child {
  padding-right: 0;
}

.bottom_section ul li div {
  padding: 25px 15px;
  height: 100%;
  background: right top no-repeat #FFF;
}

.bottom_section ul li div h3 {
  margin-bottom: 10px;
  padding-top: 35px;
  font-size: 18px;
  font-weight: 600;
  color: #FF5C1B;
  text-align: center;
}

.bottom_section ul li:first-child div h3 {
  background: center top no-repeat;
}

.bottom_section ul li:nth-child(2) div h3 {
  background: center top no-repeat;
}

.bottom_section ul li:last-child div h3 {
  background: center top no-repeat;
}

.bottom_section ul li div p {
  font-weight: 300;
  font-size: 14px;
  color: #6e6e6e;
  line-height: 24px;
}
<section class="bottom_section">
  <ul>
    <li>
      <div style="text-align: center;">
        <i class="fa fa-car" style="font-size:60px;color:red;"></i>
        <h3>Some text</h3>
        <p>Some text</p>

      </div>
    </li>
    <li>
      <div style="text-align: center;">
        <i class="fa fa-car" style="font-size:60px;color:red;"></i>
        <h3>Some text</h3>
        <p>Some text</p>

      </div>
    </li>
    <li>
      <div style="text-align: center;">
        <i class="fa fa-car" style="font-size:60px;color:red;"></i>
        <h3>Some text</h3>
        <p>Some text</p>

      </div>
    </li>
  </ul>
</section>

【讨论】:

  • 谢谢,类似于brahim 的解决方案,您的解决方案同样有效,只是实际问题是填充,因为最后一个孩子没有填充权,因此比其他两个大。请参阅带有绿色边框的 brahim 解决方案
【解决方案2】:

这似乎适用于底部部分宽度为 100% 的 Chrome。 (刚刚添加了绿色边框,因此它显示列) 你用的是什么浏览器?

.bottom_section {
  display: table;
  height: 100%;
  width:100%;
}

.bottom_section ul {
  display: table-row;
  height: 100%;
}

.bottom_section ul li {
  width: 33.33%;
  height: 100%;
  display: table-cell;
  padding: 0 30px 20px 0;
  border:1px solid green;
}

.bottom_section ul li:last-child {
  padding-right: 0;
}

.bottom_section ul li div {
  padding: 25px 15px;
  height: 100%;
  background: right top no-repeat #FFF;
}

.bottom_section ul li div h3 {
  margin-bottom: 10px;
  padding-top: 35px;
  font-size: 18px;
  font-weight: 600;
  color: #FF5C1B;
  text-align: center;
}

.bottom_section ul li:first-child div h3 {
  background: center top no-repeat;
}

.bottom_section ul li:nth-child(2) div h3 {
  background: center top no-repeat;
}

.bottom_section ul li:last-child div h3 {
  background: center top no-repeat;
}

.bottom_section ul li div p {
  font-weight: 300;
  font-size: 14px;
  color: #6e6e6e;
  line-height: 24px;
}
<section class="bottom_section">
  <ul>
    <li>
      <div style="text-align: center;">
        <i class="fa fa-car" style="font-size:60px;color:red;"></i>
        <h3>Some text</h3>
        <p>Some text</p>

      </div>
    </li>
    <li>
      <div style="text-align: center;">
        <i class="fa fa-car" style="font-size:60px;color:red;"></i>
        <h3>Some text</h3>
        <p>Some text</p>

      </div>
    </li>
    <li>
      <div style="text-align: center;">
        <i class="fa fa-car" style="font-size:60px;color:red;"></i>
        <h3>Some text</h3>
        <p>Some text</p>

      </div>
    </li>
  </ul>
</section>

【讨论】:

  • 你是对的,这是我之前尝试过的。绿色边框变得非常清楚,问题在于填充,因为最后一个孩子没有填充权,因此使其更大。
猜你喜欢
  • 1970-01-01
  • 2018-03-28
  • 2023-02-02
  • 2012-10-31
  • 2019-05-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多