How can I move up the "content" and the "right" block responsive? The problem is I can't use sub nested grid. I don't need hacks: no margin-top because header can be a different height. No javascript. Only pure CSS. If at all possible.
Now my markup looks like this:
.wrapper {
border: 1px solid red;
display: grid;
grid-template-columns: 1fr 2fr 1fr;
grid-template-areas:
"aside header header"
"left content right";
grid-gap: 15px;
}
.header, .aside, .left, .content, .right {
border: 1px solid black;
padding: 10px;
}
.header {
grid-area: header;
height: 30px; /* in real case it's responsive height */
}
.aside {
grid-area: aside;
height: 80px; /* in real case it's responsive height */
}
.left {
grid-area: left;
}
.content {
grid-area: content;
background: yellow;
}
.right {
grid-area: right;
background: yellow;
}
.left, .content, .right {
height: 100px; /* in real case it's responsive height */
}
<div class="wrapper">
<!-- this order should be on mobile -->
<header class="header">header</header>
<aside class="aside">aside</aside>
<div class="left">left</div>
<div class="content">content</div>
<div class="right">right</div>
</div>
The grid-template-rows property specifies the number (and the heights) of the rows in a grid layout. The values are a space-separated list, where each value specifies the height of the respective row.
1FR=25% of the available space.
A fraction or 1FR is one part of the whole. 1 fraction is 100% of the available space. 2 fractions are 50% each. So, 1FR is 1/2 of the available space.
What does 1fr mean in the following code? grid-template-columns: 150px 150px 1fr 1fr; The first two columns will be two fraction units of the stated width. The third and fourth columns is 1 fraction unit of the remaining space in the grid.
A solution (using CSS only) is by adding another row to your grid-template-areas:
.wrapper {
border: 1px solid red;
display: grid;
grid-template-columns: 1fr 2fr 1fr;
grid-template-areas:
"aside header header"
"aside content right"
"left content right";
grid-gap: 15px;
}
.header, .aside, .left, .content, .right {
border: 1px solid black;
padding: 10px;
}
.header {
grid-area: header;
height:30px; /* in real case it's responsive height */
}
.aside {
grid-area: aside;
height: 80px; /* in real case it's responsive height */
}
.left {
grid-area: left;
}
.content {
grid-area: content;
background: yellow;
}
.right {
grid-area: right;
background: yellow;
}
.left, .content, .right {
height: 100px; /* in real case it's responsive height */
}
<div class="wrapper">
<!-- this order should be on mobile -->
<header class="header">header</header>
<aside class="aside">aside</aside>
<div class="left">left</div>
<div class="content">content</div>
<div class="right">right</div>
</div>
You could use this sort of thing (margin-top) to move the content and right closer to the header. If this alters the view on mobile and makes it messy you will need to create 2 views depending on device and use different css values for different devices.
.wrapper {
border: 1px solid red;
display: grid;
grid-template-columns: 1fr 2fr 1fr;
grid-template-areas:
"aside header header"
"left content right";
grid-gap: 15px;
}
.header, .aside, .left, .content, .right {
border: 1px solid black;
padding: 10px;
}
.header {
grid-area: header;
height: 30px; /* in real case it's responsive height */
}
.aside {
grid-area: aside;
height: 80px; /* in real case it's responsive height */
}
.left {
grid-area: left;
}
.content {
grid-area: content;
background: yellow;
margin-top: -50px;
}
.right {
grid-area: right;
background: yellow;
margin-top: -50px;
}
.left, .content, .right {
height: 100px; /* in real case it's responsive height */
}
<div class="wrapper">
<!-- this order should be on mobile -->
<header class="header">header</header>
<aside class="aside">aside</aside>
<div class="left">left</div>
<div class="content">content</div>
<div class="right">right</div>
</div>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With