【问题标题】:Align items in a grid对齐网格中的项目
【发布时间】:2018-02-16 17:23:30
【问题描述】:

如何将项目放在网格中,其中项目将接触容器边界,并且每个项目之间有特定的间隙?

https://i.imgur.com/zO1dQmr.png

对我来说这是一个微不足道但很重要的问题。我做了研究,有一些方法(我不喜欢它们中的任何一个,想找到更好的解决方案)

1.引导方法

演示:https://codepen.io/olegburakov/pen/MQORVX

.container{
  overflow: hidden; 

  &__inner{
    display: flex;
    flex-wrap: wrap;
    margin-left: -10px;
    margin-right: -10px;  
  }

  &__item{
    padding: 10px;
    width: 100%;
    min-height: 100px;

    @media screen and (min-width: 480px){
      width: 50%;
    }
    @media screen and (min-width: 768px){
    width: 33.33%;
    }
    @media screen and (min-width: 1024px){
      width: 25%;
    }


    &-inner{
      background: olive;
      height: 100%;
    }
  }
}

我不喜欢这个解决方案,因为:

1) 容器中的负边距将项目压到它的边界并使项目接触它。

2)需要在容器中添加包装器溢出:隐藏以隐藏水平滚动条或padding-left和padding-right等于或大于容器的负边距。

3) HTML 中的附加块:container-wrapper 和 item-inner

2。 Nth-child 方法

比引导方法更难,对媒体查询不利。

3.网格布局方法

演示:https://codepen.io/olegburakov/pen/xYPeoK

.container{
  display: grid;
  grid-template-columns: 1fr;
  grid-gap: 20px;

  @media screen and (min-width: 480px){
      grid-template-columns: 1fr 1fr;
  }
  @media screen and (min-width: 768px){
    grid-template-columns: 1fr 1fr 1fr;
  }
  @media screen and (min-width: 1024px){
    grid-template-columns: 1fr 1fr 1fr 1fr;
  }

  &__item{
    width: 100%;
    min-height: 100px;
    background: olive;
  }
}

我的最爱。只需 4 行代码!但我不确定它的兼容性(例如在我未更新的 Edge15 演示中看起来像这样https://i.imgur.com/p8shL04.png)。我认为我们需要一些时间才能完全使用网格布局。

4.流体宽度法

演示:https://codepen.io/olegburakov/pen/jYWyeW

我编写了一个计算项目宽度的 mixin。所以你不需要额外的 HTML 或 padding/margin 属性。但是,当最后一行的项目多于一个项目且少于整行时,它不起作用。我尝试使用 :before :after 解决此问题,但没有成功。

.container{
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  background: tomato;

  &__item{
    @include fluid-width-item(100%, 15px, 15px);
    border: 1px solid #000;
    min-height: 100px;
    background-color: olive;
  }

  &__item{
    @media screen and (min-width: 480px){
      @include fluid-width-item(50%, 15px, 15px);
    }
    @media screen and (min-width: 768px){
      @include fluid-width-item(33.33%, 15px, 15px);
    }
    @media screen and (min-width: 1024px){
      @include fluid-width-item(20%, 15px, 5px);
    }
  }
}

所以也许我错过了没有额外 html/css 和良好兼容性的更有趣的方法?目前我使用引导方法。

【问题讨论】:

  • 我确定我遗漏了一些东西,但我真的不明白您的确切问题是什么。您实际上遇到了什么问题?
  • 我认为这与此有关 - stackoverflow.com/questions/42176419/…
  • 我想要简单而不冗长的方法来对齐网格中的项目。因为我已经知道的方法具有以下缺点之一 1) 需要额外的 HTML/CSS(如包装器或额外的 css 属性),2) 兼容性差 3) 并非在所有情况下都有效,我认为可能存在没有的方法这个缺点?
  • @OlegBurakov 看看你是否想要所有浏览器的兼容性,显然你必须编写额外的 CSS...:)
  • @Bhuwan 我只是想确保我不会错过一些“魔术方法”:D

标签: html css layout sass css-grid


【解决方案1】:

对于网格布局方法和兼容性问题,我真的很喜欢Morten Rand-Hendriksen的方法,如果你先让你的网站移动:

强制网站在所有浏览器中看起来都一样只是一个坏习惯。

[...]

可访问的移动优先布局适用于所有屏幕宽度。

https://www.youtube.com/embed/txZq7Laz7_4?start=1313&end=1587


此处为您的代码一次:(用于 sn-p 纯 CSS 和无 SASS)

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

.container {
  width: 100%;
  max-width: 1000px;
  margin: 0 auto;
}

.container__item {
  min-height: 100px;
  margin: 0 20px 20px 20px;
  background: olive;
}

@supports (grid-area: auto) {

  @media screen and (min-width: 480px) {
    .container {
      display: grid;
      grid-template-columns: 1fr 1fr;
      grid-gap: 20px;
      margin: 0;
    }
  
    .container__item {
      margin: 0;
    }
  }

  @media screen and (min-width: 768px) {
    .container {
      grid-template-columns: 1fr 1fr 1fr;
    }
  }

  @media screen and (min-width: 1024px) {
    .container {
      grid-template-columns: 1fr 1fr 1fr 1fr;
    }
  }

}
<div class="container">
  <div class="container__item"></div>
  <div class="container__item"></div>
  <div class="container__item"></div>
  <div class="container__item"></div>
  <div class="container__item"></div>
  <div class="container__item"></div>
</div>

也许这也是适合您的一种方法。 :)

【讨论】:

  • 感谢您的回复!如果客户不要求相同的设计,这是一种有趣的方法。使用这种方法,您可以忘记 IE 和 Edge 网格错误 :)
  • 开玩笑说房间里有两只大象:D
  • 关于他的整个谈话真的很好,“大象”真的很简单。 ;) 客户“不幸”总是这样的话题,但由于采用这种方法的新技术的可能性,我会尝试说服客户完全同意这一点。如果出现诸如“但我们的移动网站转换效果不佳”之类的陈述,我认为您必须在越来越多的移动优先世界中在完全不同的地方工作/交谈。 ;)
  • 是的,我敢肯定,在 2019 年,我们 100% 都使用了网格布局的力量,并且不要考虑 IE/Edge 中的旧规范(比如我们现在使用的 flex)。但是要在 2019 年使用它,我们现在就需要开始使用它。是的,有了这个冗长的 @supports 构造和 3-4 行额外的代码,但在 6-12 个月后,我希望我们将只使用网格布局而不使用这些额外的行。
猜你喜欢
  • 2021-10-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-04
  • 1970-01-01
  • 2021-04-08
  • 2018-02-06
  • 1970-01-01
相关资源
最近更新 更多