1

How to put items in a grid, in which items will be touching container borders, and have specific gap between each items?

https://i.stack.imgur.com/simVM.png

It's a trivial but important problem to me. I make research and there is some methods(I don't like any of them and want find better solution)

1. Bootstrap method

DEMO: 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%;
    }
  }
}

I don't like this solution because of:

1) Negative margins in container to press items to it borders and make items touching it.

2)Need to add wrapper to container with overflow: hidden to hide horizontal scrollbar or padding-left and padding-right equal or bigger than negative margin of container.

3) Additional blocks in HTML: container-wrapper and item-inner

2. Nth-child method

More difficult than bootstrap method, and bad for media queries.

3. Grid layout method

DEMO: 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;
  }
}

My favourite. Just 4 lines of code! But I'm not sure about it compatibility (for example in my not updated Edge15 demo looks like this https://i.stack.imgur.com/P5kPM.png). I think it's need some time before we can fully use grid layout.

4. Fluid width method

DEMO: https://codepen.io/olegburakov/pen/jYWyeW

I write a mixin that calculates width of items. So you not need extra HTML or padding/margin properties. BUT it don't work when last row has more then one item and less then full row. I tried to fix this with :before :after but it didn't work.

.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);
    }
  }
}

So maybe I miss more interesting method without extra html/css and with good compatibility? Currently I use bootstrap method.

Oleg Burakov
  • 25
  • 1
  • 2
  • 9
  • I'm sure I'm missing something but i really don't understand what your **exact** problem is. What problem are you **actually** experiencing? – Paulie_D Feb 16 '18 at 17:26
  • I *think* this is related to this - https://stackoverflow.com/questions/42176419/targeting-flex-items-on-the-last-row – Paulie_D Feb 16 '18 at 17:28
  • I want simple not verbose method to align items in a grid. Because methods I already know have one of this disadvantages 1) needs extra HTML/CSS (like wrappers or additional css properties), 2) have bad compatibility 3) don't work in all cases I think maybe exist method that don't have this disadvantages? – Oleg Burakov Feb 16 '18 at 17:36
  • @OlegBurakov see if you want compatibility fo all browsers, obviously you have to write extra CSS...:) – Bhuwan Feb 16 '18 at 17:42
  • @Bhuwan I just want to be sure that I don't miss from my view some "magic method" :D – Oleg Burakov Feb 16 '18 at 17:52
  • @OlegBurakov As I know there are several ways to do it like using flexbox(bootstrap4), grid css, floats(bootstrap3) or simply using `inline-block` css property – Bhuwan Feb 16 '18 at 18:01
  • https://stackoverflow.com/a/48755146/3597276 – Michael Benjamin Feb 16 '18 at 23:55

1 Answers1

1

For the grid layout method and the compatibility problem, i really like the approach from Morten Rand-Hendriksen, if you make your site mobile first:

Forcing sites to look the same across all browsers is just a bad habit.

[...]

Accessible mobile-first layouts work well on all screen widths.

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


Here once for your code: (for the snippet pure CSS and no 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>

Maybe that's also a approach for you. :)

Arne
  • 995
  • 1
  • 8
  • 16
  • 1
    Thank you for response! Interesting approach if client don't demand identical design. And with this approach you can forget about IE and Edge grid bugs :) – Oleg Burakov Feb 17 '18 at 11:48
  • 1
    joke about two elephants in the room :D – Oleg Burakov Feb 17 '18 at 12:38
  • 1
    The whole talk of him is really good and "the elephants" are in a real nutshell. ;) The customer is "unfortunately" always such a topic, but due to the possibilities with new technologies with this approach, I would try to convince the customer exactly for this. If then statements like "but our mobile site is not converting so well" should come, i think you have to work/talk anyway in quite different places in a more and more mobile-first world. ;) – Arne Feb 17 '18 at 13:28
  • Yes, I sure in 2019 we 100% all use power of grid layout and don't think about old specifications in IE/Edge (like flex we use now). But to use this in 2019 we are already need to start doing it right NOW. Yes, with this verbose @supports construction and 3-4 additional lines of code but, after 6-12 months I hope we will use just grid layout without this additional lines. – Oleg Burakov Feb 17 '18 at 13:46