【问题标题】:CSS Grid: How do I put everything on a single row?CSS Grid:如何将所有内容放在一行上?
【发布时间】:2020-07-25 13:53:07
【问题描述】:

我正在尝试更多地使用网格,但我不知道什么应该是一个简单的问题。如何将所有元素放在一行中?

也就是说,我想为单行模拟flex 布局。

我知道我可以在下面的示例中使用grid-template-columns: 1fr 1fr 1fr 1fr;,但我不想设置特定的列数。

div#flex {
  display: flex;
  flex-direction: row; /* I know that’s not necessary */
}
div#flex>button {
  flex: 1;
}

div#grid {
  display: grid;
  grid-template-rows: 1fr;
}
div#grid>button {

}
<div id="flex">
  <button>One</button>
  <button>Two</button>
  <button>Three</button>
  <button>Four</button>
</div>

<div id="grid">
  <button>One</button>
  <button>Two</button>
  <button>Three</button>
  <button>Four</button>
</div>

【问题讨论】:

    标签: css flexbox css-grid


    【解决方案1】:

    您可以添加 grid-auto-flow: column; 代替:https://stackblitz.com/edit/angular-9midxc

    CSS:

    div#flex {
      display: flex;
      flex-direction: row; /* I know that’s not necessary */
    }
    div#flex>button {
      flex: 1;
    }
    
    div#grid {
      display: grid;
       grid-auto-flow: column; 
    }
    div#grid>button {
    
    }
    

    HTML:

    <div id="flex">
      <button>One</button>
      <button>Two</button>
      <button>Three</button>
      <button>Four</button>
    </div>
    
    <div id="grid">
      <button>One</button>
      <button>Two</button>
      <button>Three</button>
      <button>Four</button>
    </div>
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-16
    • 1970-01-01
    • 2021-08-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多