Why is grid row height too tall? - css

I noticed the extra space between the title and the snippet(paragraph) as shown in the example. Is there a way to fix this, without giving up the grid? The second column, first row, is too tall...
.post-content {
display: grid;
grid-template-columns: 320px auto;
grid-template-rows: auto auto;
}
.item-thumbnail {
grid-column-start: 1;
grid-column-end: 2;
grid-row-start: 1;
grid-row-end: 3;
margin: 0 1rem 0 0;
}
.post-title {
grid-column-start: 2;
grid-column-end: 3;
grid-row-start: 1;
grid-row-end: 2;
}
.post-snippet {
grid-column-start: 2;
grid-column-end: 3;
grid-row-start: 2;
grid-row-end: 3;
}
.item-thumbnail {
background: #444;
padding: 50% 0;
}
<div class="post-content">
<div class="post-title">
title
</div>
<div class="post-snippet">
The lone lamp post of the one-street town flickered, not quite dead but definitely on its way out.
</div>
<div class="item-thumbnail">
</div>
</div>

in such case, define 3 rows where the left element will take all of them. You make the last row 1fr so that the first two will get sized to their content:
.post-content {
display: grid;
grid-template-columns: 320px auto;
grid-template-rows: auto auto 1fr;
}
.item-thumbnail {
grid-row: 1/span 3;
grid-column: 1;
margin: 0 1rem 0 0;
background: #444;
padding: 50% 0;
}
.post-title,
.post-snippet {
grid-column: 2;
}
<div class="post-content">
<div class="post-title">
title
</div>
<div class="post-snippet">
The lone lamp post of the one-street town flickered, not quite dead but definitely on its way out.
</div>
<div class="item-thumbnail">
</div>
</div>
Or 2 rows and the second one 1fr. You will have the same visual in your case but the difference is that the second text is taking more space (if you add more styles like background you will notice this)
.post-content {
display: grid;
grid-template-columns: 320px auto;
grid-template-rows: auto 1fr;
}
.item-thumbnail {
grid-row: 1/span 2;
grid-column: 1;
margin: 0 1rem 0 0;
background: #444;
padding: 50% 0;
}
.post-title,
.post-snippet {
grid-column: 2;
}
<div class="post-content">
<div class="post-title">
title
</div>
<div class="post-snippet">
The lone lamp post of the one-street town flickered, not quite dead but definitely on its way out.
</div>
<div class="item-thumbnail">
</div>
</div>

In your .post-content class, change grid-template-rows: auto auto; to grid-template-rows: max-content;
If I understood correctly, this should be your desired result.
.post-content {
display: grid;
grid-template-columns: 320px auto;
grid-template-rows: max-content;
}
.item-thumbnail {
grid-column-start: 1;
grid-column-end: 2;
grid-row-start: 1;
grid-row-end: 3;
margin: 0 1rem 0 0;
}
.post-title {
grid-column-start: 2;
grid-column-end: 3;
grid-row-start: 1;
grid-row-end: 2;
}
.post-snippet {
grid-column-start: 2;
grid-column-end: 3;
grid-row-start: 2;
grid-row-end: 3;
}
.item-thumbnail {
background: #444;
padding: 50% 0;
}
<div class="post-content">
<div class="post-title">
title
</div>
<div class="post-snippet">
The lone lamp post of the one-street town flickered, not quite dead but definitely on its way out.
</div>
<div class="item-thumbnail">
</div>
</div>

Related

When using CSS Grid is it possible to have a child expand beyond their parent's grid placement?

I have a simple CSS grid with 5 columns and two rows. The first row contains 5 elements but I want the second row to contain the 5th element's child for all 5 columns, is this possible?
I want the red element (the child of element 5) to be 100% the width in the row below all the other elements.
.container {
display: grid;
grid-template-columns: [line1] min-content [line2] min-content [line3] min-content [line4] max-content [line5] max-content [end];
grid-template-rows: [row1-start] 10% [row1-end row2-start] auto [row2-end];
column-gap: 1em;
row-gap: 2em;
}
.element1 {
max-height: 2em;
grid-column-start: 1;
grid-column-end: 1;
grid-row-start: 1;
grid-row-end: 1;
background-color: pink;
}
.element2 {
max-height: 2em;
grid-column-start: 2;
grid-column-end: 2;
grid-row-start: 1;
grid-row-end: 1;
background-color: blue;
}
.element3 {
max-height: 2em;
grid-column-start: 3;
grid-column-end: 3;
grid-row-start: 1;
grid-row-end: 1;
background-color: orange;
}
.element4 {
max-height: 2em;
grid-column-start: 4;
grid-column-end: 4;
grid-row-start: 1;
grid-row-end: 1;
background: yellow;
}
.element5 {
grid-column-start: 5;
grid-column-end: 5;
grid-row-start: 1;
grid-row-end: 1;
justify-self: end;
background-color: purple;
}
.element-sub5 {
grid-column: 1 / 5;
grid-row: 2;
border: 1px solid red;
background-color: red;
padding: 0 1em;
margin-top: 2.5em;
width: 100%;
height: 10em;
float: left;
}
<div class="container">
<div class="element1">
element1
</div>
<div class="element2">
element2
</div>
<div class="element3">
element3
</div>
<div class="element4">
element4
</div>
<div class="element5">
element5
<span class="element-sub5">
REALLY BIG BOX OF TEXT
</span>
</div>
</div>
Example layout
It’s possible to set up a grid like this but only if your 5th items element isn’t inside it…
CSS
.parent {
display: grid;
grid-template-columns: repeat(5, 1fr);
grid-template-rows: repeat(2, 1fr);
grid-column-gap: 0px;
grid-row-gap: 0px;
}
.div1 { grid-area: 1 / 1 / 2 / 2; }
.div2 { grid-area: 1 / 2 / 2 / 3; }
.div3 { grid-area: 1 / 3 / 2 / 4; }
.div4 { grid-area: 1 / 4 / 2 / 5; }
.div5 { grid-area: 1 / 5 / 2 / 6; }
.div6 { grid-area: 2 / 1 / 3 / 6; }
HTML
<div class="parent">
<div class="div1"> </div>
<div class="div2"> </div>
<div class="div3"> </div>
<div class="div4"> </div>
<div class="div5"> </div>
<div class="div6"> </div>
</div>

CSS GRID: container with equal grid, with different numbers of columns and rows?

I'm trying to create container with square, equal grid. If I set for .container the same numbers of columns and rows, grid is equal. But if I create grid like below and insert into it items, grid with those items loose their equal shape. How to prevent that?
.c-containter {
display: grid;
grid-template-columns: repeat(16, 1fr);
grid-template-rows: repeat(8, 1fr);
grid-gap: 8px;
background-color: red;
}
.c-containter::before {
content: "";
padding-bottom: 100%;
display: inline-block;
vertical-align: top;
}
.e-container {
position: relative;
background-color: blue;
}
.e-container__item-1 {
grid-column-start: 1;
grid-column-end: 8;
grid-row-start: 1;
grid-row-end: 4;
}
.e-container__item-2 {
grid-column-start: 1;
grid-column-end: 8;
grid-row-start: 4;
grid-row-end: 9;
}
.e-container__item-3 {
grid-column-start: 8;
grid-column-end: 17;
grid-row-start: 1;
grid-row-end: 9;
}
<div class="c-containter">
<div class="e-container e-container__item-1"></div>
<div class="e-container e-container__item-2"></div>
<div class="e-container e-container__item-3"></div>
</div>

Define a new row in css grid

Is there a way in css grid of saying 'after the second column, start another row'?
It seems straightforward enough defining how much height and width you want a grid cell to take up, but defining where you want cells to appear requires a lot of syntax - I feel like I'm missing something.
Like this layout:
main {
display: grid;
grid-gap: 10px;
max-width: 700px;
margin: 0 auto;
}
.block {
height: 200px;
}
.block--one {
background: coral;
grid-column-start: 1;
grid-column-end: 2;
grid-row-start: 1;
grid-row-end: 2;
}
.block--two {
background: cornflowerblue;
grid-column-start: 2;
grid-column-end: 6;
grid-row-start: 1;
grid-row-end: 2;
}
.block--three {
background: burlywood;
grid-column-start: 1;
grid-column-end: 4;
grid-row-start: 2;
grid-row-end: 4;
}
.block--four {
background: lightseagreen;
grid-column-start: 4;
grid-column-end: 6;
grid-row-start: 2;
grid-row-end: 4;
}
<main>
<div class="block block--one"></div>
<div class="block block--two"></div>
<div class="block block--three"></div>
<div class="block block--four"></div>
</main>
Shouldn't it be achievable with something like this?
main {
display: grid;
grid-gap: 10px;
max-width: 700px;
margin: 0 auto;
grid-template-rows: 2fr 2fr;
grid-template-columns: 2fr 4fr 5fr 1fr;
}
.block {
height: 200px;
}
.block--one {
background: coral;
}
.block--two {
background: cornflowerblue;
}
.block--three {
background: burlywood;
}
.block--four {
background: lightseagreen;
}
<main>
<div class="block block--one"></div>
<div class="block block--two"></div>
<div class="block block--three"></div>
<div class="block block--four"></div>
</main>
You can simplify the code by using a shorthand property.
In your first example, you're using all long-hand properties. For example, you have this:
.block--two {
background: cornflowerblue;
grid-column-start: 2;
grid-column-end: 6;
grid-row-start: 1;
grid-row-end: 2;
}
Like with other CSS features, such as borders, margins and padding, there's a shorthand property to consolidate multiple lines of code.
In this case, there's the grid-area property, which shortens the code above to:
grid-area { 1 / 2 / 2 / 6 }
The values flow in this order:
grid-row-start
grid-column-start
grid-row-end
grid-column-end
main {
display: grid;
grid-template-columns: repeat(5, 1fr);
grid-auto-rows: 200px;
grid-gap: 10px;
max-width: 700px;
margin: 0 auto;
}
.block--one {
background: coral;
/* no need to specify placement here; default aligns to row 1, column 1 */
}
.block--two {
background: cornflowerblue;
grid-area: 1 / 2 / 2 / -1;
}
.block--three {
background: burlywood;
grid-area: 2 / 1 / 3 / 4;
}
.block--four {
background: lightseagreen;
grid-area: 2 / 4 / 3 / -1;
}
<main>
<div class="block block--one"></div>
<div class="block block--two"></div>
<div class="block block--three"></div>
<div class="block block--four"></div>
</main>
More details: https://developer.mozilla.org/en-US/docs/Web/CSS/grid-area
A good way of defining the rows and columns can be by using grid-template-areas and grid-area which can define how many rows and columns a grid area should take up. This is especially helpful when changing the layout with #media tags, you only have to change the one attribute.
From your row and column definitions I can add each block to take up the space that I want it to.
See example for 2 rows by 4 columns for the 4 blocks.
main {
display: grid;
grid-gap: 10px;
max-width: 700px;
margin: 0 auto;
grid-template-rows: 2fr 2fr;
grid-template-columns: 2fr 4fr 5fr 1fr;
grid-template-areas:
"block1 block2 block2 block2"
"block3 block3 block4 block4";
}
.block {
height: 200px;
}
.block--one {
grid-area: block1;
background: coral;
}
.block--two {
grid-area: block2;
background: cornflowerblue;
}
.block--three {
grid-area: block3;
background: burlywood;
}
.block--four {
grid-area: block4;
background: lightseagreen;
}
<main>
<div class="block block--one"></div>
<div class="block block--two"></div>
<div class="block block--three"></div>
<div class="block block--four"></div>
</main>
main {
display: grid;
grid-gap: 10px;
max-width: 700px;
margin: 0 auto;
grid-auto-flow: row;
}
.block {
height: 200px;
}
.block--one {
background: coral;
grid-column: 1;
}
.block--two {
background: cornflowerblue;
grid-column: 2/ 6;
}
.block--three {
background: burlywood;
grid-column: 1 / 4;
}
.block--four {
background: lightseagreen;
grid-column: 4 / 6;
}
<main>
<div class="block block--one"></div>
<div class="block block--two"></div>
<div class="block block--three"></div>
<div class="block block--four"></div>
</main>
grid-auto-flow: row; Will start on new row if the element is not able to fit on the current row

How to fix css grid-rows that are not spanning correctly?

I have content that won't span 2 rows unless I give it a position: absolute for some reason, but then it overlaps the footer on pages with longer content. This is an example on the site I'm working on: https://carlisleacademymaine.com/pony-club-riding-center-program/
When I test it on the simple code below it seems to work fine, but on my site it doesn't. Maybe something else on the site is causing problems, but I can't figure it out.
.container {
display: grid;
width: 100%;
grid-template-columns: 400px auto;
grid-template-rows: 40px minmax(760px, auto) auto auto;
min-height: 100%;
grid-gap: 0;
position: relative;
grid-template-areas: none;
}
.header {
background-color: aqua;
grid-column: 1 / span 2;
grid-row: 1 / 2;
}
.sidebar {
background-color: red;
grid-column: 1 / span 1;
grid-row: 2 / 3;
}
.main-content {
background-color: chartreuse;
grid-column-start: 2;
grid-column-end: 3;
grid-row-start: 2;
grid-row-end: 4;
}
.sidebar-widget {
background-color: lightblue;
grid-column: 1 / span 1;
grid-row: 3 / 4;
}
.footer {
background-color: aqua;
grid-column: 1 / span 2;
grid-row: 4 / 5;
}
<div class="container">
<div class="header">header</div>
<div class="sidebar">sidebar</div>
<div class="main-content">content </div>
<div class="sidebar-widget">extras</div>
<div class="footer">This is my foot</div>
</div>

CSS3 Grid - why the rows are not positioned as per column/row stmt

I'm trying to design a tv remote control with up/OK/down/left/right buttons using a grid layout. The issue is that the result is unexpected to me. I simply want to define 3 rows and 3 columns with specific position and space equally divided between them. What am I doing wrong? The result looks like this
Up left OK
right down
Instead of
Up
|
<left----OK---Right-->
|
Down
Code
#grid {
display: grid;
grid-template-columns:
/* 1 */ auto
/* 2 */ auto
/* 3 */ auto;
grid-template-rows:
/* 4 */ auto
/* 5 */ auto
/* 6 */ auto;
}
#up { grid-column: 2; grid-row: 1; }
#ok { grid-column: 2; grid-row: 2; }
#down { grid-column: 2; grid-row: 3; }
#left { grid-column: 1; grid-row: 3; }
#right { grid-column: 3; grid-row: 3; }
}
<div id="grid">
<div class="up">Up</div>
<div class="left">left</div>
<div class="ok">OK</div>
<div class="right">right</div>
<div class="down">down</div>
</div>
Your main problem is that you have set classes on your inner elements, and are styling ids
#grid {
display: grid;
grid-template-columns:
/* 1 */ auto
/* 2 */ auto
/* 3 */ auto;
grid-template-rows:
/* 4 */ auto
/* 5 */ auto
/* 6 */ auto;
width: 200px;
}
#grid div {
width: 50px;
height: 50px;
background-color: lightblue;
margin: 3px;
}
.up { grid-column: 2; grid-row: 1; }
.ok { grid-column: 2; grid-row: 2; }
.down { grid-column: 2; grid-row: 3; }
.left { grid-column: 1; grid-row: 2; }
.right { grid-column: 3; grid-row: 2; }
<div id="grid">
<div class="up">Up</div>
<div class="left">left</div>
<div class="ok">OK</div>
<div class="right">right</div>
<div class="down">down</div>
</div>

Resources