【问题标题】:Aligning content within columns based on column position根据列位置对齐列内的内容
【发布时间】:2018-07-06 21:16:56
【问题描述】:

我正在创建一个包含三列的网格。每个网格框都有一个 div,其最大宽度为280px,有时网格列比这更宽。我想对齐网格框中的内容,使左列向左对齐,中心列与中心对齐,右列向右对齐。请参阅下图了解我想要的结果:

目前我在网格容器元素上使用 CSS justify-items: justify 规则。我的当前结果如下:

我可以用 CSS 做什么来在我想要的布局图中生成布局?

【问题讨论】:

  • 你可以使用 flex..?
  • @BhuwanBhatt 是的,为什么我不明白为什么不
  • "目前我正在使用 CSS justify-items: justify" 在哪里?您没有包含您的代码。请添加minimal reproducible example

标签: html css flexbox css-grid


【解决方案1】:

您可以为此使用 CSS Grid,但(据我所知)您必须将项目定位到特定的 grid-columns 并使用 justify-self 属性根据需要对齐它们。

特殊情况是一行只有一个项目。

fiddle

.container {
  display: grid;
  grid-gap: 1rem;
  grid-template-columns: repeat(3, 1fr);
}

.item {
  width: 100px;
  height: 300px;
  background: pink;
}

.item:nth-of-type(3n) {
  justify-self: end;
}

.item:nth-of-type(3n + 2) {
  justify-self: center;
}

.item:nth-of-type(3n + 1):last-of-type {
  grid-column: 2;
  justify-self: center;
}
<div class="container">
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
</div>

【讨论】:

    【解决方案2】:

    最简单的解决方案是让您的item 宽33%,然后使用nth-child 选择器将每个列的内容左、中、右对齐。

    为此,您可以使用 inline-blockflex 等,在第一个示例中,我使用了 Flexbox。

    堆栈sn-p

    .flex {
      display: flex;
      flex-wrap: wrap;
    }
    
    .flex .item {
      width: calc((100% / 3) - 10px);
      border: 1px dotted black;
      box-sizing: border-box;
      margin: 5px;
      display: flex;
    }
    
    .flex .item:nth-child(3n+2) div {
      margin: 0 auto;                  /* center align every 3rd, start from item 2  */
    }
    
    .flex .item:nth-child(3n+3) div {
      margin-left: auto;               /* right align every 3rd, start from item 3  */
    }
    
    .flex .item div {
      max-width: 280px;
      padding: 20px;
      background: lightgray;
    }
    <div class="flex">
      <div class="item">
        <div>Some content that can be max 280px wide</div>
      </div>
      <div class="item">
        <div>Some content that can be max 280px wide</div>
      </div>
      <div class="item">
        <div>Some content that can be max 280px wide</div>
      </div>
      <div class="item">
        <div>Some content that can be max 280px wide</div>
      </div>
      <div class="item">
        <div>Some content that can be max 280px wide</div>
      </div>
    </div>

    对于那些需要支持不支持 Flexbox(或有错误行为)的浏览器的人,这个使用 inline-blockfloat

    .parent::after {
      content: '';
      display: block;
      clear: both;
    }
    
    .parent .item {
      float: left;
      width: calc((100% / 3) - 10px);
      border: 1px dotted black;
      box-sizing: border-box;
      margin: 5px;
    }
    
    .parent .item:nth-child(3n+2) {
      text-align: center;              /*  center align every 3rd, start from item 2  */
    }
    
    .parent .item:nth-child(3n+3) {
      text-align: right;               /*  right align every 3rd, start from item 3  */
    }
    
    .parent .item div {
      display: inline-block;
      max-width: 280px;
      padding: 20px;
      background: lightgray;
      text-align: left;
    }
    <div class="parent">
      <div class="item">
        <div>Some content that can be max 280px wide</div>
      </div>
      <div class="item">
        <div>Some content that can be max 280px wide</div>
      </div>
      <div class="item">
        <div>Some content that can be max 280px wide</div>
      </div>
      <div class="item">
        <div>Some content that can be max 280px wide</div>
      </div>
      <div class="item">
        <div>Some content that can be max 280px wide</div>
      </div>
    </div>

    【讨论】:

      【解决方案3】:

      您可以使用 Flexbox。 Flexbox 使得实现这些布局变得非常容易。

      你必须使用justify-content:space-between

      Fiddle Link

      【讨论】:

      • 嗨,我发现了这个问题!如果我的项目少于行数,则内容会粘在每一侧,从而在项目之间留下巨大的间隙。查看图片了解更多信息:i.imgur.com/FLWOeLk.png
      • 少于行数....?不明白...你能分享一个小提琴链接吗...?
      • 请查看 imgur 链接。您可以看到第三行的中间列是空的,因为该行中没有足够的项目来填满整个空间。
      猜你喜欢
      • 2022-01-08
      • 2023-02-23
      • 1970-01-01
      • 2021-02-07
      • 1970-01-01
      • 2015-06-15
      • 2017-08-25
      • 2013-09-11
      • 2019-10-29
      相关资源
      最近更新 更多