首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >针对CSS列布局中的特定列

针对CSS列布局中的特定列
EN

Stack Overflow用户
提问于 2018-09-07 09:35:34
回答 1查看 2.4K关注 0票数 1

CSS列布局中,我需要针对第一列及其子列。

使用以下CSS代码:

代码语言:javascript
复制
.multi-column {
    @media (min-width: 768px) {
        -webkit-column-count: 2;
        -webkit-column-gap: 10px;
        -moz-column-count: 2;
        -moz-column-gap: 10px;
        column-count: 2;
        column-gap: 10px;

        > li {
            -webkit-column-break-inside: avoid;
            page-break-inside: avoid;
            break-inside: avoid;
            overflow: hidden;
        }
    }
}
  • 如何为第一列设置不同的background-color
  • 如何为上一列中的项设置不同的color

我可能需要一些像:first-column或者类似的伪选择器.

EN

回答 1

Stack Overflow用户

发布于 2018-09-07 10:17:40

重点是添加额外的包装器,这些包装器随后成为网格项,以生成组或列,并能够使用适当的选择器将其作为目标。

基本例子:

代码语言:javascript
复制
.grid-container {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
}

.grid-item {
  display: grid;
  /* other grid props which you'd normally have for the container */
}

.grid-item:first-child {background: blue} /* or: ":nth-child(1)", ":first-of-type", ":nth-of-type(1)" */
.grid-item:last-child {color: blue} /* ":nth-child(...)", ":nth-of-type(...)" */
代码语言:javascript
复制
<div class="grid-container">
  <div class="grid-item">
    <div>1.1</div>
    <div>1.2</div>
    <div>1.3</div>
  </div>
  <div class="grid-item">
    <div>2.1</div>
    <div>2.2</div>
    <div>2.3</div>
  </div>
  <div class="grid-item">
    <div>3.1</div>
    <div>3.2</div>
    <div>3.3</div>
  </div>
</div>

当然,您可以避免这样做,并使用众多的选择器,但这并不是真正的最佳,没有其他方式的自动柜员机。也许:first-column或类似的东西有一天会出现……

如果用例合适,另一种方法是将grid-template-rowsgrid-auto-flow: column和更具体的选择器结合起来。

代码语言:javascript
复制
.grid-container {
  display: grid;
  grid-template-rows: repeat(3, 1fr);
  grid-auto-flow: column;
}

.grid-item {
  display: grid; /* make them grid-items as well and then position their items, if any */
}

.grid-item:nth-child(-n + 3) {background: blue} /* selects first three */
.grid-item:nth-child(n + 7) {color: blue} /* selects all but the first 6 */
代码语言:javascript
复制
<div class="grid-container">
  <div class="grid-item">1</div>
  <div class="grid-item">2</div>
  <div class="grid-item">3</div>
  <div class="grid-item">4</div>
  <div class="grid-item">5</div>
  <div class="grid-item">6</div>
  <div class="grid-item">7</div>
  <div class="grid-item">8</div>
  <div class="grid-item">9</div>
</div>

或者只使用grid-template-columns和默认的水平流

代码语言:javascript
复制
.grid-container {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
}

.grid-item {
  display: grid; /* make them grid-items as well and then position their items, if any */
}

.grid-item:nth-child(3n + 1) {background: blue} /* selects every third starting from the first  */
.grid-item:nth-child(3n + 3) {color: blue} /* selects every third */
代码语言:javascript
复制
<div class="grid-container">
  <div class="grid-item">1</div>
  <div class="grid-item">2</div>
  <div class="grid-item">3</div>
  <div class="grid-item">4</div>
  <div class="grid-item">5</div>
  <div class="grid-item">6</div>
  <div class="grid-item">7</div>
  <div class="grid-item">8</div>
  <div class="grid-item">9</div>
</div>

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52219661

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档