【问题标题】:Equal height columns with CSS使用 CSS 的等高列
【发布时间】:2016-02-22 04:25:50
【问题描述】:

我想在我的 css 表中使用百分比。不幸的是,它对我不起作用。

这段代码有什么问题?我应该使用 flexbox 代替表格吗?

我想使用表格,因为我想要相同高度的列。

ul {
  list-style: none;
  margin: 0;
  display: table;
  table-layout: fixed;
  width: 100%;
}

li {
  width: 50%;
  display: table-cell;
}
<ul>
  <li>1</li>
  <li>2</li>
  <li>3</li>
  <li>4</li>
  <li>5</li>
  <li>6</li>
</ul>

【问题讨论】:

  • 所以你希望列表项是窗口宽度的一半?那么 ul 将是 300% 宽,而不是 100%。您可以选择:百分比加起来是正确的数字,或者您需要省略其中一个值并让浏览器自行计算宽度。
  • 我是否正确地理解了您正在尝试让两个列表项彼此相邻并且下一个应该出现在下一行中?
  • 你不能将列分成多行而不用行包裹它们。您必须为此使用 flexbox,这是我的初学者视觉指南prettyminimal.com/flexbox

标签: html css flexbox css-tables


【解决方案1】:

使用 Flexbox 的等高列

HTML

<ul>
  <li>1</li>
  <li>2</li>
  <li>3</li>
  <li>4</li>
  <li>5</li>
  <li>6</li>
</ul>

CSS

ul { display: flex; }

使用上面的简单代码,您可以在列表项中放置任意数量的内容,并且所有列表项的高度相同。

DEMO


注意事项:


浏览器支持: 所有主流浏览器都支持 Flexbox,except IE < 10。一些最新的浏览器版本,例如 Safari 8 和 IE10,需要vendor prefixes。要快速添加前缀,请使用Autoprefixer。更多详情this answer

【讨论】:

    【解决方案2】:

    这是一个使用 ul/li 元素的示例,2 列使用百分比并且高度相等。

    由于表格更喜欢表格/行/单元格布局,因此我对您的 html 进行了一些重组。

    * {
        margin: 0;
    }
    html, body {
        height: 100%;
    }
    .table {
      display: table;
      width: 90%;
      height: 60%;
      padding-top: 5%;
      margin: 0 auto;
    }
    ul {
      display: table-row;
      list-style: none;
      margin: 0;
    }
    
    li {
      display: table-cell;
      width: 50%;
      border: 1px solid #999;
    }
    <div class="table">
      <ul>
        <li>1</li>
        <li>2</li>
      </ul>
      <ul>
        <li>3</li>
        <li>4</li>
      </ul>
      <ul>
        <li>5</li>
        <li>6</li>
      </ul>
    </div>

    【讨论】:

      【解决方案3】:

      我对与@Michael_B 不同的问题的解释给出了答案。在li 元素上使用width: 50%; 的初始样式,我认为期望的结果是让六个列表项流入2 列和3 行。这种情况使用 CSS 表格无法轻易解决,但使用 flexbox 相对简单。

      ul {
        list-style: none;
        margin: 0;
        width: 100%;
        /*kills the irritating intial padding on uls in webkit*/
        -webkit-padding-start: 0;
        display: flex;
        flex-flow: row wrap;
        /*stretch is the default value, but it will force items to stretch to match others in their row*/
        align-items: stretch;
        /*below properties just emphasize the layout*/
        padding: 2px;
        background-color: green;
      }
      li {
        /*forces border-width and padding to be part of the declared with (50% in this case)*/
        box-sizing: border-box;
        width: 50%;
        /*list-item, inline-block, and block work equally well*/
        display: list-item;
        /*below properties just emphasize the layout*/
        border: white 2px solid;
        background-color: lightgreen;
      }
      /*I set this property to show what happens if 1 item has a different height*/
      
      li:nth-child(3) {
        height: 40px;
      }
      <ul>
        <li>1</li>
        <li>2</li>
        <li>3</li>
        <li>4</li>
        <li>5</li>
        <li>6</li>
      </ul>

      【讨论】:

        猜你喜欢
        • 2013-06-07
        • 2011-01-08
        • 2012-12-22
        • 1970-01-01
        • 1970-01-01
        • 2023-02-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多