Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make the width of 1fr + 1fr = 2fr (with grid gap)

Tags:

css

css-grid

What's the right way to have my first two columns have the same width total as my third column? if I am splitting them up like so:

.grid {
    display:grid;
    grid-template-columns:1fr 1fr 2fr;
    grid-gap:2em;
}
<div class="grid">
    <div class="small">Col 1</div>
    <div class="small">Col 2</div>
    <div class="large">Col 3</div>
</div>

Example here: https://codepen.io/shorty2240/pen/baGYoW

Example

I was assuming I could make another grid below this one, with the same grid gap, and 1fr 1fr, and it would line up, but obviously it won't.

I would like to avoid nesting the first two items if possible as the actual project is outputting the three items automatically and wrapping around the first two could be problematic.

like image 682
Chris Avatar asked Mar 08 '23 09:03

Chris


1 Answers

You should make the grid 4 columns wide and define your .large to use up 2 columns.

This is actually in line with the working of columns in popular css frameworks like bootstrap and foundation.

grid-template-columns:1fr 1fr 1fr 1fr;

.large {
  grid-column-start: 3;
  grid-column-end: 5;
}

Or use any of the shorthand syntaxes for both setting 4 columns and grid-column.

like image 90
René Avatar answered Mar 16 '23 18:03

René