As you can see, when I change the grid-gap, only the width of 1, 3 and 4 are updated.
The width of 2 is not updated at all.
I want it to update the width of 1 and the width of 2, 3 and 4.
The demo of MDN shows that it's possible to resize all items accordingly.
https://developer.mozilla.org/en-US/docs/Web/CSS/gap
Here's my code
.container {
display: grid;
width: 500px;
height: 100px;
gap: 20px; /* Try to change this, width of 2 isn't updated */
grid-template-columns: 2fr 1fr 1fr;
grid-template-rows: 1fr 1fr;
transition: gap 0.3s linear;
}
.child {
background-color: gray;
text-align: center;
font-size: 50px;
color: white;
line-height: 70px
}
.child-1 {
grid-row-start: 1;
grid-row-end: 3;
}
.child-2 {
grid-column-start: 2;
grid-column-end: 4;
}
<div class="container">
<div class="child child-1">1</div>
<div class="child child-2">2</div>
<div class="child">3</div>
<div class="child">4</div>
</div>
It's because the width of (2) include a gap so its width is 2fr + gap. changing the gap will also change the fr and the width will remain constant.
in this case 1fr = (500px - 2*gap)/4 so 2fr + gap = (500px - 2*gap)/2 + gap = 250px
Change the code and use a different structure where the width of (2) isn't constant:
.container {
display: grid;
width: 500px;
height: 100px;
grid-gap: 20px;
grid-template-columns: 4fr 1fr 1fr 1fr 1fr;
grid-template-rows: 1fr 1fr;
animation: change 1s linear infinite alternate;
}
.child {
background-color: gray;
text-align: center;
font-size: 50px;
color: white;
line-height: 70px;
grid-column:span 2;
}
.child-1 {
grid-row-start: 1;
grid-row-end: 3;
grid-column:span 1;
}
.child-2 {
grid-column-start: 2;
grid-column-end: 6;
}
#keyframes change {
to {
grid-gap:1px;
}
}
<div class="container">
<div class="child child-1">1</div>
<div class="child child-2">2</div>
<div class="child">3</div>
<div class="child">4</div>
</div>
In this case 1fr = (500px - 4*gap )/8 and the width of (2) is 4fr + 3*gap = (500px - 4*gap)/2 + 3*gap = 250px + gap But now the (3) and (4) will be constant because 2fr + gap=(500px - 4*gap)/4 + gap = 125px
Another structure where all will update:
.container {
display: grid;
width: 500px;
height: 100px;
grid-gap: 30px;
grid-template-columns: 6fr 1fr 1fr 1fr 1fr 1fr 1fr;
grid-template-rows: 1fr 1fr;
animation: change 1s linear infinite alternate;
}
.child {
background-color: gray;
text-align: center;
font-size: 50px;
color: white;
line-height: 70px;
grid-column:span 3;
}
.child-1 {
grid-row-start: 1;
grid-row-end: 3;
grid-column:span 1;
}
.child-2 {
grid-column-start: 2;
grid-column-end: 8;
}
#keyframes change {
to {
grid-gap:1px;
}
}
<div class="container">
<div class="child child-1">1</div>
<div class="child child-2">2</div>
<div class="child">3</div>
<div class="child">4</div>
</div>
The (2) will have a width equal to 250px + 2*gap. (3) and (4) will have a width equal to 125px + 0.5*gap
Another configuration:
.container {
display: grid;
width: 500px;
height: 100px;
grid-gap: 30px;
grid-template-columns: 2fr 4fr 2fr 1fr 1fr 2fr;
grid-template-rows: 1fr 1fr;
animation: change 1s linear infinite alternate;
}
.child {
background-color: gray;
text-align: center;
font-size: 50px;
color: white;
line-height: 70px;
grid-column:span 2;
}
.child-1 {
grid-row-start: 1;
grid-row-end: 3;
}
.child-2 {
grid-column-start: 3;
grid-column-end: 7;
}
#keyframes change {
to {
grid-gap:1px;
}
}
<div class="container">
<div class="child child-1">1</div>
<div class="child child-2">2</div>
<div class="child">3</div>
<div class="child">4</div>
</div>
Basically the trick is to avoid having a constant width for your elements.
Problem:
It's not easy to see it from outside but if you can Examine the Grid Layout in Firefox developer tools you can see the difference.
You can notice that the width of the two columns of 2 is changing yet the parent remains constant because it is spanning them both and breaking it would make the grid asymmetrical.
Solution:
You can use this Grid Generator and create some changes to the code for the following setup:
Now 1 spans two fractions of the layout, 2 spans four fractions while 3 and 4 spans two fractions each. In this way, the whole structure is symmetrical.
.container {
display: grid;
width: 500px;
height: 100px;
grid-gap: 20px; /* Try to change this, width of 2 isn't updated */
grid-template-columns: 4fr 1fr 1fr 1fr 1fr;
grid-template-rows: 1fr 1fr;
transition: grid-gap 1s ease-in-out;
}
.container:hover {
grid-gap: 0px;
}
.child {
background-color: gray;
text-align: center;
font-size: 50px;
color: white;
line-height: 70px;
}
.child-1 {
grid-area: 1 / 1 / 3 / 2;
}
.child-2 {
grid-area: 1 / 2 / 2 / 6;
}
.child-3 {
grid-area: 2 / 2 / 3 / 4;
}
.child-4 {
grid-area: 2 / 4 / 3 / 6;
}
<div class="container">
<div class="child child-1">1</div>
<div class="child child-2">2</div>
<div class="child child-3">3</div>
<div class="child child-4">4</div>
</div>
Related
enter image description here
How do I make my grid cell same size after spanning it over the next cell? This is what I have tried so far
.factory {
display: grid;
grid-template-columns: 1fr 1fr 2fr;
height: 600px;
gap: 10px;
margin-top: 100px;
padding: 10px;
}
.factory>div {
background-color: gray;
border: 1px green solid;
}
.image-1 {
width: 100%;
height: auto;
}
.div-3 {
grid-column: 3 / span 5;
}
.div-5 {
grid-column: 1 / span 2;
}
<div class="factory">
<div class="div-1">box1</div>
<div class="div-2">box2</div>
<div class="div-3">box3</div>
<div class="div-5">box5</div>
<div class="div-6">box6</div>
</div>
As you can see box6 is smaller than the rest of the boxes.
Your issue is that on div-3 you try to span 5 columns after it has started at column 3 which will make the browser attempt to add 5 more columns to the grid.
.div-3 {
grid-column: 3 / span 5;
}
You can either remove this section completely because you have already specified in your grid-template-columns: 1fr 1fr 2fr; that the third column should be double the space of the first two.
Another option would be to span 1
.div-3 {
grid-column: 3 / span 1;
}
You can see this clearly by using the inspection tools.
.factory {
display: grid;
grid-template-columns: 1fr 1fr 2fr;
height: 600px;
gap: 10px;
margin-top: 100px;
padding: 10px;
}
.factory>div {
background-color: gray;
border: 1px green solid;
}
.image-1 {
width: 100%;
height: auto;
}
/*.div-3 {
grid-column: 3 / span 5;
}*/
.div-5 {
grid-column: 1 / span 2;
}
<div class="factory">
<div class="div-1">box1</div>
<div class="div-2">box2</div>
<div class="div-3">box3</div>
<div class="div-5">box5</div>
<div class="div-6">box6</div>
</div>
I want to place box 2 on top of both other boxes by half of them, however, even having explicitly defined grid-template-columns propriety to 1fr it automatically creates another column.
Here is my attempt
index.html
<div class="grid-overlap">
<div class="box">1</div>
<div class="box">2</div>
<div class="box">3</div>
</div>
style.scss
.grid-overlap {
max-width: 40rem;
width: 95%;`your text`
margin: 2rem auto;
gap: 1rem;
display: grid;
grid-template-rows: repeat(4, 1fr);
grid-template-columns: 1fr;
.box {
width: 300px;
height: 300px;
margin: 0 auto;
text-align: center;
font-size: 3rem;
}
.box:nth-child(1) {
grid-row: 1 / 3;
background-color: dodgerblue;
}
.box:nth-child(2) {
background-color: red;
grid-row: 2 / 4;
z-index: 100;
}
.box:nth-child(3) {
grid-row: 3 / 5;
background-color: tomato;
}
}
Thanks in advance.
I am giving one example of overlap, try to see how it works and use it in your use case.
.grid-overlap {
display: grid;
grid-template-columns: repeat(4, 1fr);
grid-auto-rows: 100px;
}
.grid-overlap .box:nth-child(1) {
grid-row: 1/3;
grid-column: 1/3;
background-color: dodgerblue;
}
.grid-overlap .box:nth-child(2) {
background-color: rgba(255, 0, 0, 0.6);
grid-row: 2/4;
grid-column: 2/4;
}
<div class="grid-overlap">
<div class="box">1</div>
<div class="box">2</div>
</div>
Looking at the grid we can see that the second square starts in the middle of the first one and that the last one is positioned at one quarter along and three quarters down the first square.
This leads to a grid of width 6 and height 7 square cells.
As it's not possible to have both the grid imensions set at 300px and the width of the grid to be defined in rems (and % units) this snippet drops the 300px settings and sets the overall grid to be the width as defined in the question and the aspect ratio 6/7.
Note that the grid gap is not set (defaults to 0) as no gap was shown in the picture given in the question.
body {
width: 100vw;
height: 100vh;
}
.grid-overlap {
max-width: 40rem;
width: 95%;
aspect-ratio: 6 / 7;
margin: 2rem auto;
gap: 1rem;
gap: 0;
display: grid;
grid-template-rows: repeat(7, 1fr);
grid-template-columns: repeat(6, 1fr);
}
.box {
/*width: 300px;
height: 300px;*/
width: 100%;
height: 100%;
margin: 0 auto;
text-align: center;
font-size: 3rem;
}
.box:nth-child(1) {
grid-column: 1 / span 4;
grid-row: 1 / span 4;
background-color: dodgerblue;
}
.box:nth-child(2) {
background-color: red;
grid-column: 3 / span 4;
grid-row: 3 / span 4;
z-index: 100;
}
.box:nth-child(3) {
grid-column: 2/ span 4;
grid-row: 4 / span 4;
background-color: tomato;
}
<body>
<div class="grid-overlap">
<div class="box">1</div>
<div class="box">2</div>
<div class="box">3</div>
</div>
</body>
If the important dimensions were the 300px then use those to set the width of the overall grid.
I have the following layout:
.container {
width: 630px;
height: 630px;
display: grid;
column-gap: 10px;
grid-template-columns: 2fr 1fr 1fr;
grid-template-rows: auto auto auto;
}
.first, .second, .third, .fourth, .fifth, .sixth, .seventh {
width: 200px;
height: 200px;
background-color: pink;
}
.first, .second {
height: 400px;
}
.fourth {
grid-column: 3 / 4;
grid-row: 2 / 3;
background-color: yellow;
}
.seventh {
grid-column: 3 / 4;
grid-row: 3 / 4;
}
<div class='container'>
<div class='first'>1</div>
<div class='second'>2</div>
<div class='third'>3</div>
<div class='fourth'>4</div>
<div class='fifth'>5</div>
<div class='sixth'>6</div>
<div class='seventh'>7</div>
</div>
I would like the first and second column to have 2 rows, taking 3fr 1fr and the last column to have 3 rows taking up 2fr 1fr 1fr. The end result would be that box number 4 is nested between number 3 and 7 without breaking out of the rectangle that boxes in the first two columns created.
This final positioning should look like this:
Is this possible using css grid, and how do I do it?
You can try like below:
.container {
width: 700px;
height: 700px;
display: grid;
grid-template-columns: repeat(3, 1fr);
grid-auto-rows: auto;
grid-gap: 20px;
}
.first,
.second,
.third,
.fourth,
.fifth,
.sixth,
.seventh {
width: 100%;
height: 100%;
background-color: pink;
}
.first,
.second {
grid-row: 1 / 3;
}
.second,
.sixth {
}
.fourth {
background-color: yellow;
}
<div class='container'>
<div class='first'>1</div>
<div class='second'>2</div>
<div class='third'>3</div>
<div class='fourth'>4</div>
<div class='fifth'>5</div>
<div class='sixth'>6</div>
<div class='seventh'>7</div>
</div>
Is this what you wanted.
You can read more about grid-row here
Run snippet to see it working below
.container {
width: 700px;
height: 700px;
display: grid;
grid-template-columns: repeat(3, 1fr);
grid-auto-rows: auto;
grid-gap: 20px;
}
.first,
.second,
.third,
.fifth,
.sixth,
.seventh {
background-color: pink;
}
.first,
.second {
grid-row: 1 / 3;
}
.fourth {
background-color: yellow;
}
<div class='container'>
<div class='first'>1</div>
<div class='second'>2</div>
<div class='third'>3</div>
<div class='fourth'>4</div>
<div class='fifth'>5</div>
<div class='sixth'>6</div>
<div class='seventh'>7</div>
</div>
Both divs display correctly alone, but if both img-1 and img-2 divs are both in the container, the second div disappears.
HTML:
<div class="body-background">
<div class="background-img-1"></div>
<div class="background-img-2"></div>
</div>
CSS
.body-background {
display: grid;
grid-template-columns: repeat(5, 1fr);
grid-template-rows: 1fr;
width: 100%;
height: 100%;
}
.background-img-1 {
background: red;
grid-column: 1 / 3;
}
.background-img-2 {
background: blue;
grid-column: 2 / 4;
}
Shouldn't the boxes overlap normally?
Give both background divs a grid-row: 1 property (which becomes grid-row: 1 / 2). I don't think the browser likes having to give it implicitly to two overlapping cells.
.body-background {
display: grid;
grid-template-columns: repeat(5, 1fr);
grid-template-rows: 1fr;
width: 100vw;
height: 100vh;
}
.background-img-1 {
background-color: red;
grid-column: 1 / 3;
grid-row: 1;
}
.background-img-2 {
background-color: blue;
grid-column: 2 / 4;
grid-row: 1;
}
<div class="body-background">
<div class="background-img-1"></div>
<div class="background-img-2"></div>
</div>
Is there a way in css grid of saying 'after the second column, start another row'?
It seems straightforward enough defining how much height and width you want a grid cell to take up, but defining where you want cells to appear requires a lot of syntax - I feel like I'm missing something.
Like this layout:
main {
display: grid;
grid-gap: 10px;
max-width: 700px;
margin: 0 auto;
}
.block {
height: 200px;
}
.block--one {
background: coral;
grid-column-start: 1;
grid-column-end: 2;
grid-row-start: 1;
grid-row-end: 2;
}
.block--two {
background: cornflowerblue;
grid-column-start: 2;
grid-column-end: 6;
grid-row-start: 1;
grid-row-end: 2;
}
.block--three {
background: burlywood;
grid-column-start: 1;
grid-column-end: 4;
grid-row-start: 2;
grid-row-end: 4;
}
.block--four {
background: lightseagreen;
grid-column-start: 4;
grid-column-end: 6;
grid-row-start: 2;
grid-row-end: 4;
}
<main>
<div class="block block--one"></div>
<div class="block block--two"></div>
<div class="block block--three"></div>
<div class="block block--four"></div>
</main>
Shouldn't it be achievable with something like this?
main {
display: grid;
grid-gap: 10px;
max-width: 700px;
margin: 0 auto;
grid-template-rows: 2fr 2fr;
grid-template-columns: 2fr 4fr 5fr 1fr;
}
.block {
height: 200px;
}
.block--one {
background: coral;
}
.block--two {
background: cornflowerblue;
}
.block--three {
background: burlywood;
}
.block--four {
background: lightseagreen;
}
<main>
<div class="block block--one"></div>
<div class="block block--two"></div>
<div class="block block--three"></div>
<div class="block block--four"></div>
</main>
You can simplify the code by using a shorthand property.
In your first example, you're using all long-hand properties. For example, you have this:
.block--two {
background: cornflowerblue;
grid-column-start: 2;
grid-column-end: 6;
grid-row-start: 1;
grid-row-end: 2;
}
Like with other CSS features, such as borders, margins and padding, there's a shorthand property to consolidate multiple lines of code.
In this case, there's the grid-area property, which shortens the code above to:
grid-area { 1 / 2 / 2 / 6 }
The values flow in this order:
grid-row-start
grid-column-start
grid-row-end
grid-column-end
main {
display: grid;
grid-template-columns: repeat(5, 1fr);
grid-auto-rows: 200px;
grid-gap: 10px;
max-width: 700px;
margin: 0 auto;
}
.block--one {
background: coral;
/* no need to specify placement here; default aligns to row 1, column 1 */
}
.block--two {
background: cornflowerblue;
grid-area: 1 / 2 / 2 / -1;
}
.block--three {
background: burlywood;
grid-area: 2 / 1 / 3 / 4;
}
.block--four {
background: lightseagreen;
grid-area: 2 / 4 / 3 / -1;
}
<main>
<div class="block block--one"></div>
<div class="block block--two"></div>
<div class="block block--three"></div>
<div class="block block--four"></div>
</main>
More details: https://developer.mozilla.org/en-US/docs/Web/CSS/grid-area
A good way of defining the rows and columns can be by using grid-template-areas and grid-area which can define how many rows and columns a grid area should take up. This is especially helpful when changing the layout with #media tags, you only have to change the one attribute.
From your row and column definitions I can add each block to take up the space that I want it to.
See example for 2 rows by 4 columns for the 4 blocks.
main {
display: grid;
grid-gap: 10px;
max-width: 700px;
margin: 0 auto;
grid-template-rows: 2fr 2fr;
grid-template-columns: 2fr 4fr 5fr 1fr;
grid-template-areas:
"block1 block2 block2 block2"
"block3 block3 block4 block4";
}
.block {
height: 200px;
}
.block--one {
grid-area: block1;
background: coral;
}
.block--two {
grid-area: block2;
background: cornflowerblue;
}
.block--three {
grid-area: block3;
background: burlywood;
}
.block--four {
grid-area: block4;
background: lightseagreen;
}
<main>
<div class="block block--one"></div>
<div class="block block--two"></div>
<div class="block block--three"></div>
<div class="block block--four"></div>
</main>
main {
display: grid;
grid-gap: 10px;
max-width: 700px;
margin: 0 auto;
grid-auto-flow: row;
}
.block {
height: 200px;
}
.block--one {
background: coral;
grid-column: 1;
}
.block--two {
background: cornflowerblue;
grid-column: 2/ 6;
}
.block--three {
background: burlywood;
grid-column: 1 / 4;
}
.block--four {
background: lightseagreen;
grid-column: 4 / 6;
}
<main>
<div class="block block--one"></div>
<div class="block block--two"></div>
<div class="block block--three"></div>
<div class="block block--four"></div>
</main>
grid-auto-flow: row; Will start on new row if the element is not able to fit on the current row