【问题标题】:How to align content in different columns of grid如何对齐网格不同列中的内容
【发布时间】:2023-01-29 14:53:07
【问题描述】:

这是我的简单网格:

  display: grid;
  grid-template-columns: 70% 30%;
  grid-gap: 2px;

因此,在第一列中,我想将内容左对齐,在第二列中,我想将内容右对齐:

|Column1             |     Column2|
|content             |     content|

【问题讨论】:

标签: css


【解决方案1】:

您可以选择网格中的项目并使它们分别显示:根据需要将其内容向左或向右弯曲和对齐。

这是两列网格的简单 sn-p。它只为演示提供颜色,因此您可以看到对齐是正确的。

.grid {
  display: grid;
  grid-template-columns: 70% 30%;
  grid-gap: 2px;
  width: 90vw;
  background: black;
}

.grid>* {
  display: flex;
  background: cyan;
}

.grid>*:nth-child(1) {
  justify-content: left;
  background: yellow;
}

.grid>*:nth-child(2) {
  justify-content: right;
}
<div class="grid">
  <div>Column1<br>content</div>
  <div>Column2<br>content</div>
</div>

【讨论】:

    【解决方案2】:

    好的..对于我刚刚设置的第二列DIV:

    <div style={{justifySelf: 'end'}}>
    

    【讨论】:

      【解决方案3】:
      1. 将网格创建为容器
      2. 然后创建两个不同的div,一个用于左侧,一个用于右侧
      3. 然后对齐内容

        所以它看起来像

        <div className="container">
          <div className="left"></div>
          <div className="right"></div>
        </div>
        

        然后将文本在左侧 div 上向左对齐,在右侧 div 上向右对齐。

      【讨论】:

      • 是的..但我想更新现有的应用程序设计。我们想采用网格方法。所以我想我可以用不同的方式对齐两列
      猜你喜欢
      • 1970-01-01
      • 2021-04-24
      • 2018-07-09
      • 2019-09-03
      • 1970-01-01
      • 2017-08-21
      • 2016-09-17
      • 2020-10-07
      • 1970-01-01
      相关资源
      最近更新 更多