0

I am using CSS grid to display 5 cards. The cards aligned perfectly in a single row in desktop. But, when I switch to a mobile device, they are displayed as per the image below.

Is it possible to specify to display only two rows in the CSS grid.

enter image description here

8
  • do you mean you need to hide div 5 in mobile ? Commented Feb 11, 2020 at 15:10
  • 1
    You really can't as CSS-Grid will create implicit rows for any wrapping. On mobile how many items should there be in each row? Commented Feb 11, 2020 at 15:10
  • Questions seeking code help must include the shortest code necessary to reproduce it in the question itself preferably in a Stack Snippet. See How to create a Minimal, Reproducible Example Commented Feb 11, 2020 at 15:11
  • Possibly related to this question: stackoverflow.com/questions/43662552/… In case you want to hide the 5th element just set "display: none" in a media query. Commented Feb 11, 2020 at 15:17
  • 1
    @VinaySharma SHOW YOUR CODE Commented Feb 11, 2020 at 16:35

3 Answers 3

2

You cannot specify only 2 rows but you can try to set the last rows to a minmax(0,0) value aside overflow:hidden; ... to hide them :

section {
  display: grid;
  grid-template-columns: 1fr 1fr;
  grid-gap: 1em;
  overflow: hidden;
  grid-template-rows: minmax(100px, 1fr) minmax(100px, 1fr) minmax(0px, 0); /* third-row won't show, next might , grid-gap will increase height of section if set */
}

section {
  counter-reset: divs;
}

div {
  border: solid;
}

div:before {
  counter-increment: divs;
  content: counter(divs)
}
p:before {
color:red;
  content: counter(divs)
  }
<section>
  <div></div>
  <div></div>
  <div></div>
  <div></div>
  <div></div>
</section>
<p> boxes are standing here </p>

this hides the third row, if more , some element will show up since a grid-gap set will increase height for each extra rows.


You may use margin instead grid-gap and add for each extra rows to hide the mimax(0,0) value for the grid-template-rows.

Another demo below showing a single row (out of 7) and 2 boxes (out of 14).

section {counter-reset:divs;}
div {border:solid;margin:0.5em;}
div:before {counter-increment:divs;content:counter(divs)}

section {
  display:grid;
  grid-template-columns:1fr 1fr;   
  overflow:hidden;
  grid-template-rows: 
    minmax(100px,1fr) /* row to be seen */
    minmax(0,0)
    minmax(0,0)
    minmax(0,0)  
    minmax(0,0) 
    minmax(0,0)  
    minmax(0,0)  ;/* 7 rows values set , 6 rows can be hidden */
}

p:before {
color:red;
  content: counter(divs)
  }
<section>
  <div></div>
  <div></div>
  <div></div>
  <div></div>
  <div></div>
  <div></div>
  <div></div>
  <div></div>
  <div></div>
  <div></div>
  <div></div>
  <div></div>
  <div></div>
  <div></div> 
</section>
<p> boxes are standing here.</p>

demo (hide/show) so many rows

Sign up to request clarification or add additional context in comments.

5 Comments

Hi, that's not working as expected because if I try to hide second row as well, the second row is being displayed a little bit and is not being hidden completely.
@VinaySharma remove the grid-gap and use margin instead. ;) like the second example i have made explaining this case. i'll edit the second snippet example to show a single row .
Thanks for sharing that point. Actually I was using grid-gap: 10px which was adding a gap both horizontally as well as vertically, instead used grid-gap: 0 10px which is now being applied only horizontally.
@VinaySharma great, i've made a demo from the last snippet where you can choose amount of rows to show/hide. codepen.io/gc-nomade/pen/PoqNoxg
Hey, thanks very much for that. That's an amazing job! Well, I have posted another question if you could help me with :) stackoverflow.com/q/60238044/11220479
1

Just hide the last element in case the grid wraps. I made a working example.

<div class="wrapper">
  <div class="box a">A</div>
  <div class="box b">B</div>
  <div class="box c">C</div>
  <div class="box d">D</div>
  <div class="box e">E</div>
</div>

--

.wrapper {
  display: grid;
  grid-auto-flow: column;
}

.wrapper {
  display: grid;
  grid-auto-flow: column;
}

@media only screen and (max-width: 800px) {
  .wrapper {
      grid-auto-flow: row;
      grid-template-columns: repeat(2, minmax(300px, 500px));
  }
  .e{
    display: none;
  }
}

Fiddle: https://jsfiddle.net/k2zLqx1d/

1 Comment

Hi, my requirement is to have common class for all div. Let's suppose I have 100's of such div then it won't be feasible to specify class name as a, b, c, d.. and so on for all.
0

You can work with css. Either you create a mobile-display-none class that will hide your content at a specific breakpoint. Or if you're receiving this data from an arrayyou can modify the array or specify in code wich index the items should be rendered to. However you will need a breakpoint for that as well so i'd just go for straight css @media screen. If you'd like to make the 5th div as third element in that row you might want to adjust your grid.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.