Issue
I'm trying to achieve a layout like in the image below using CSS Grid Layout:
I've tried to do this myself, but was at this layout below:
The Grid container has the red background, and the items has the green background.
This is my code that I used to try:
<div class="wrapper">
<div class="box1">One</div>
<div class="box2">Two</div>
<div class="box3">Three</div>
<div class="box4">Four</div>
</div>
html,
body {
width: 100%;
height: 100%;
}
body {
margin: 0;
display: flex;
justify-content: center;
align-items: center;
background-color: rgb(3, 3, 41);
}
.wrapper {
background-color: red;
width: 50%;
height: 50%;
display: grid;
grid-template-columns: repeat(3, 1fr);
grid-template-rows: repeat(3, 100px);
}
.wrapper div {
background-color: greenyellow;
border: 0.2em solid green;
}
.box1 {
grid-column: 1 / span 1;
grid-row: 2 / span 1;
}
.box2 {
grid-column: 2 / span 1;
grid-row: 1 / span 1;
}
.box3 {
grid-column: 2 / span 1;
grid-row: 3 / span 1;
}
.box4 {
grid-column: 3 / span 1;
grid-row: 2 / span 1;
}
I tried using fractional values for the tracks to span span: 2.5
, but still won't work.
I really want to know if it's possible to achieve such a layout with CSS Grid. Or if you've got a better solution, I'd still appreciate.
Solution
You don't want 3 rows, you want (I think) 6, then span the div as 2 rows each.
* {
box-sizing: border-box;
}
body {
margin: 0;
display: flex;
justify-content: center;
align-items: center;
background-color: rgb(3, 3, 41);
}
.wrapper {
background-color: red;
padding: 10px;
width: 100%;
gap: 4px;
display: grid;
grid-template-columns: repeat(3, 1fr);
grid-template-rows: repeat(6, 50px);
}
.wrapper div {
background-color: greenyellow;
border: 0.2em solid green;
}
.box1 {
grid-column: 1 / span 1;
grid-row: 2 / span 2;
}
.box2 {
grid-column: 2 / span 1;
grid-row: 1 / span 2;
}
.box3 {
grid-column: 2 / span 1;
grid-row: 3 / span 2;
}
.box4 {
grid-column: 3 / span 1;
grid-row: 2 / span 2;
}
<div class="wrapper">
<div class="box1">One</div>
<div class="box2">Two</div>
<div class="box3">Three</div>
<div class="box4">Four</div>
</div>
Answered By - Paulie_D
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.