I have a grid layout with two rows:
- A header and
- a content section which sometimes has two columns, and sometimes has only one.
Basically your typical header with content layout, except I want to dynamically turn off the right column sometimes.
- The height of the header must be determined by its content. OK:
grid-template-rows: min-content auto;
- The header must span the one or both columns. But this fails. I've tried the following approaches:
This will only work when there is one column. If there are two columns, this row won't span over them.
.header {
grid-row-start: 1;
grid-column-start: 1;
grid-column-end: auto;
}
Same as
.header {
grid-row-start: 1;
grid-column: 1 / span auto;
}
This will only work when there is two columns. It creates a place on the screen for a second column, even when there isn't one.
.header {
grid-row-start: 1;
grid-column: 1 / span 2;
}
- Finally, I'm a bit puzzled by the fact that the two columns aren't the same width. Probably related to
auto-fill
, but can't really get my head around that, is there a solution for that?
This is the code:
.header {
grid-row-start: 1;
grid-column-start: 1;
grid-column-end: auto;
}
.left {
grid-row-start: 2;
grid-column-start: 1;
grid-column-end: 1;
}
.right {
grid-row-start: 2;
grid-column-start: 2;
grid-column-end: 2;
}
.wrapper {
height: 100%;
width: 100%;
display: grid;
grid-gap: 10px;
grid-column: 1 / -1;
grid-template-rows: min-content auto;
}
<div style="height: 300px;">
<div class="wrapper">
<div class="header" style="background-color:#006;">
HEADER
</div>
<div class="left" style="background-color:#600;">
LEFT
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim.
</div>
<div
class="right"
v-if="true"
style="background-color:#060;">
RIGHT
</div>
</div>
</div>
Result looks like: