Make Grid-rows Take Up Minimum Space
Solution 1:
Note the differences between the following three variations.
1. grid-template-rows: auto auto auto auto
This set-up creates four explicit rows that adjust to the height of their content. The fourth row is based on the height of the text. The first, second and third rows are based on the height of the image that is spanning across all three rows. The auto
value distributes space equally among the three rows. (See sections 7.2, 11.3 and 11.8 in the grid spec.)
body {
background: #ddd;
color: #222;
font-family: Roboto, sans-serif;
}
* {
margin: 0; padding: 0;
}
.movie {
margin: 20px auto;
display: grid;
grid-column-gap: 20px;
grid-template:
'img date''img head''img sub''desc desc';
grid-template-columns: 100px auto;
grid-template-rows: auto auto auto auto; /* relevant code */width: 500px;
background: #f4f4f4;
border-radius: 3px;
padding: 20px;
}
.movie__date {
grid-area: date;
}
.movie__img {
grid-area: img;
width: 100%;
}
.movie__description {
grid-area: desc;
margin-top: 20px;
}
.movie__subtite {
grid-area: sub;
}
.movie__heading {
grid-area: head;
}
<divclass="movie"><imgsrc="https://image.tmdb.org/t/p/w1280/nPTjj6ZfBXXBwOhd7iUy6tyuKWt.jpg"class="movie__img"></img><pclass="movie__date">03/17/18</p><h1class="movie__heading">Call Me By Your Name</h1><pclass="movie__subtitle">some subtitle of variable length</p><pclass="movie__description">Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.</p></div>
2. grid-template-rows: auto auto 1fr 1fr
You wrote:
"...but that leaves me with some ugly white-space between the subtitle and the description."
That's because when you apply the fr
to multiple tracks, the entire space in the container is considered, and the free space is distributed equally among those tracks. In this case, the last two rows get an equal share of the free space. The image (which is not a factor in the track sizing calculation) ends up far above the final row.
More details here: The difference between percentage and fr units in CSS Grid Layout
body {
background: #ddd;
color: #222;
font-family: Roboto, sans-serif;
}
* {
margin: 0; padding: 0;
}
.movie {
margin: 20px auto;
display: grid;
grid-column-gap: 20px;
grid-template:
'img date''img head''img sub''desc desc';
grid-template-columns: 100px auto;
grid-template-rows: auto auto 1fr 1fr;
width: 500px;
background: #f4f4f4;
border-radius: 3px;
padding: 20px;
}
.movie__date {
grid-area: date;
}
.movie__img {
grid-area: img;
width: 100%;
}
.movie__description {
grid-area: desc;
margin-top: 20px;
}
.movie__subtite {
grid-area: sub;
}
.movie__heading {
grid-area: head;
}
<divclass="movie"><imgsrc="https://image.tmdb.org/t/p/w1280/nPTjj6ZfBXXBwOhd7iUy6tyuKWt.jpg"class="movie__img"></img><pclass="movie__date">03/17/18</p><h1class="movie__heading">Call Me By Your Name</h1><pclass="movie__subtitle">some subtitle of variable length</p><pclass="movie__description">Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.</p></div>
3. grid-template-rows: auto auto 1fr auto
In this case you're setting all rows to content-height (auto
), except the third row (subtitle
). By setting this row to 1fr
you are telling it to consume all free space in the container. That pins the first two rows to the top and the last row to the bottom. I think this is the solution you want.
body {
background: #ddd;
color: #222;
font-family: Roboto, sans-serif;
}
* {
margin: 0; padding: 0;
}
.movie {
margin: 20px auto;
display: grid;
grid-column-gap: 20px;
grid-template:
'img date''img head''img sub''desc desc';
grid-template-columns: 100px auto;
grid-template-rows: auto auto 1fr auto;
width: 500px;
background: #f4f4f4;
border-radius: 3px;
padding: 20px;
}
.movie__date {
grid-area: date;
}
.movie__img {
grid-area: img;
width: 100%;
}
.movie__description {
grid-area: desc;
margin-top: 20px;
}
.movie__subtite {
grid-area: sub;
}
.movie__heading {
grid-area: head;
}
<divclass="movie"><imgsrc="https://image.tmdb.org/t/p/w1280/nPTjj6ZfBXXBwOhd7iUy6tyuKWt.jpg"class="movie__img"></img><pclass="movie__date">03/17/18</p><h1class="movie__heading">Call Me By Your Name</h1><pclass="movie__subtitle">some subtitle of variable length</p><pclass="movie__description">Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.</p></div>
Post a Comment for "Make Grid-rows Take Up Minimum Space"