Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Different height of CSS grid cells [duplicate]

Tags:

html

css

css-grid

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.

enter image description here

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>
like image 506
Lukas Pierce Avatar asked Aug 21 '17 07:08

Lukas Pierce


People also ask

How do you change the grid height in CSS?

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.

What is 1FR in CSS grid?

1FR=25% of the available space.

What does 1FR mean?

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?

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.


2 Answers

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>
like image 72
Nick Avatar answered Oct 03 '22 03:10

Nick


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>
like image 26
Dragomir Kolev Avatar answered Oct 03 '22 04:10

Dragomir Kolev