This question already has answers here:
Centering in CSS Grid
(9 answers)
Closed 4 years ago.
I have a CSS Grid below and I would like the content(letters for now) to be vertically aligned within the cells. What's the best way to accomplish this? vertical-align:middle on the cells doesn't seem to do anything, align-items:center; on the grid container worked, but then the heights were all different.
body {
margin: 40px;
}
.wrapper {
display: grid;
grid-gap: 10px;
grid-template-columns: [col] 100px [col] 100px [col] 100px;
grid-template-rows: [row] auto [row] auto [row] ;
background-color: #fff;
color: #444;
}
.box {
background-color:#444;
color:#fff;
padding:20px;
font-size:150%;
}
.a {
grid-column: col / span 2;
grid-row: row 1 / 3;
}
.b {
grid-column: col 3 / span 1;
grid-row: row ;
}
.c {
grid-column: col 3 / span 1;
grid-row: row 2 ;
}
.d {
grid-column: col / span 1;
grid-row: row 3;
}
.e {
grid-column: col 2 / span 1;
grid-row: row 3;
}
.f {
grid-column: col 3 / span 1;
grid-row: row 3;
}
<div class="wrapper">
<div class="box a">A</div>
<div class="box b">B</div>
<div class="box c">C</div>
<div class="box d">D</div>
<div class="box e">E</div>
<div class="box f">F</div>
</div>
If you just want them vertically centered you can add display:flex; and align-items: center; to the box class:
body {
margin: 40px;
}
.wrapper {
display: grid;
grid-gap: 10px;
grid-template-columns: [col] 100px [col] 100px [col] 100px;
grid-template-rows: [row] auto [row] auto [row];
background-color: #fff;
color: #444;
}
.box {
background-color: #444;
color: #fff;
padding: 20px;
font-size: 150%;
display: flex;
align-items: center;
}
.a {
grid-column: col / span 2;
grid-row: row 1 / 3;
}
.b {
grid-column: col 3 / span 1;
grid-row: row;
}
.c {
grid-column: col 3 / span 1;
grid-row: row 2;
}
.d {
grid-column: col / span 1;
grid-row: row 3;
}
.e {
grid-column: col 2 / span 1;
grid-row: row 3;
}
.f {
grid-column: col 3 / span 1;
grid-row: row 3;
}
<div class="wrapper">
<div class="box a">A</div>
<div class="box b">B</div>
<div class="box c">C</div>
<div class="box d">D</div>
<div class="box e">E</div>
<div class="box f">F</div>
</div>
Related
I have a grid container and I want my items to be placed at bottom.
I have this Actual
I want something like this
Expected
.container {
display: grid;
grid-template-columns: repeat(14, 1fr);
grid-template-rows: repeat(6, 1fr);
grid-gap: 8px;
width: 200px;
background: blue;
}
.item {
background-color: red;
color: white;
}
.item-1 {
grid-column: span 5;
grid-row: span 6;
}
.item-2 {
grid-column: span 4;
grid-row: span 5;
}
.item-3 {
grid-column: span 3;
grid-row: span 4;
}
<div class="container">
<div class="item item-1">Item 1</div>
<div class="item item-2">Item 2</div>
<div class="item item-3">Item 3</div>
</div>
PS: I've already tried place-items: end; and align-items: end;
You can use the two value start / end syntax for grid-column and grid-row to establish the start and end of your items.
For example grid-row: 2 / span 3; would start at the 2nd grid line and span an additional 3 more down to the 5th grid line.
.container {
display: grid;
grid-template-columns: repeat(14, 1fr);
grid-template-rows: repeat(6, 1fr);
grid-gap: 8px;
width: 200px;
background: blue;
}
.item {
background-color: red;
color: white;
}
.item-1 {
grid-column: span 5;
grid-row: span 6;
}
.item-2 {
grid-column: 6 / span 4;
grid-row: 2 / span 5;
}
.item-3 {
grid-column: 10 / span 3;
grid-row: 3 / span 4;
}
<div class="container">
<div class="item item-1">Item 1</div>
<div class="item item-2">Item 2</div>
<div class="item item-3">Item 3</div>
</div>
Note: In response to the edit about align-items, this only works when the layout isn't being defined by spanning multiple rows.
Code:
.wrapper {
display: grid;
position: relative;
grid-template-columns: repeat(4, 1fr);
grid-auto-rows: 25vh;
width: 100%;
}
.prova {
border: 1px solid;
}
.wrapper div:nth-child(2) {
grid-column: 3;
grid-row: 2 / 4;
}
.wrapper div:nth-child(5) {
grid-column: 1 / 3;
grid-row: 1 / 3;
}
width: 100%;
background-color: none;
overflow: auto;
position: fixed;
}
<div class="wrapper">
<div class="prova">1</div>
<div class="prova">2</div>
<div class="prova">3</div>
<div class="prova">4</div>
<div class="prova">5</div>
<div class="prova">6</div>
<div class="prova">7</div>
<div class="prova">8</div>
<div class="prova">9</div>
<div class="prova">10</div>
<div class="prova">11</div>
<div class="prova">12</div>
</div>
As you can see here, in the last row of the grid there are 4 cells but I'd like them to become just one long cell, or to add another cell below them as bis as I just said!
you can use grid-column: span 4; on your last child to make it span all columns:
.wrapper {
display: grid;
position: relative;
grid-template-columns: repeat(4, 1fr);
grid-auto-rows: 25vh;
width: 100%;
}
.prova {
border: 1px solid;
}
.wrapper div:nth-child(2) {
grid-column: 3;
grid-row: 2 / 4;
}
.wrapper div:nth-child(5) {
grid-column: 1 / 3;
grid-row: 1 / 3;
}
.wrapper div:last-child {
grid-column: span 4;
}
<div class="wrapper">
<div class="prova">1</div>
<div class="prova">2</div>
<div class="prova">3</div>
<div class="prova">4</div>
<div class="prova">5</div>
<div class="prova">6</div>
<div class="prova">7</div>
<div class="prova">8</div>
<div class="prova">9</div>
<div class="prova">10</div>
<div class="prova">11</div>
<div class="prova">12</div>
<div class="prova">13</div>
</div>
I'm trying to create a section on my html where I can show some grids. Can you help me with this please? I'm attaching a pic where I'm showing what I want to do. Yellow color is how it is right now, and blue is the final result.
.wrapper {
display: grid;
grid-gap: 20px;
justify-content: center;
grid-template-rows: 100px 100px 100px;
grid-template-columns: 100px 100px 100px;
color: #444;
}
.box {
background-color: #444;
color: #fff;
border-radius: 5px;
padding: 20px;
font-size: 150%;
}
.a {
grid-column: 1 / 3;
grid-row: 1;
}
.b {
grid-column: 3;
grid-row: 1 / 3;
}
.c {
grid-column: 1;
grid-row: 2;
}
.d {
grid-column: 2;
grid-row: 2;
}
<div class="wrapper">
<div class="box a">A</div>
<div class="box b">B</div>
<div class="box c">C</div>
<div class="box d">D</div>
</div>
I start learning working with the CSS grid. I am making a grid there is looking like this. I need the item 3 and 4 to align with item 1 horizontally.
The height on all items should give 700px, so that part should fit. I am thinking if I am doing something wrong in the code regarding the rows and columns?
.wrapper {
display: grid;
grid-template-columns: repeat(11, 1fr);
grid-gap: 1em;
}
.wrapper>div {
background-color: #eee;
padding: 1em;
}
.wrapper>div:nth-child(odd) {
background-color: #ddd;
}
.item1 {
grid-row: 1 / 3;
grid-column: 1/6;
height: 700px;
}
.item2 {
grid-row: 1 / 1;
grid-column: 6/12;
height: 340px;
}
.item3 {
grid-row: 2 / 3;
grid-column: 6/9;
height: 350px;
}
.item4 {
grid-row: 2/3;
grid-column: 9/12;
height: 350px;
}
<div class="wrapper">
<div class="item1">
This is item 1
</div>
<div class="item2">
This is item 2
</div>
<div class="item3">
This is item 3
</div>
<div class="item4">
This is item 4
</div>
</div>
A couple of changes should help. Firstly, change the grid-gap on the wrapper from 1em to 10px. This helps with the gap issue with 1em usually being 16px by default. Then just add box-sizing: border-box; to the .wrapper > div.
Here's a working example:
.wrapper {
display:grid;
grid-template-columns:repeat(11,1fr);
grid-gap: 10px;
}
.wrapper > div {
background-color: #eee;
padding: 1em;
box-sizing: border-box;
}
.wrapper > div:nth-child(odd) {
background-color: #ddd;
}
.item1 {
grid-row: 1 / 3;
grid-column: 1/6;
height: 700px;
}
.item2 {
grid-row: 1 / 1;
grid-column: 6/12;
height: 340px;
}
.item3 {
grid-row: 2 / 3;
grid-column: 6/9;
height: 350px;
}
.item4 {
grid-row:2/3;
grid-column: 9/12;
height: 350px;
}
<div class="wrapper">
<div class="item1">
This is item 1
</div>
<div class="item2">
This is item 2
</div>
<div class="item3">
This is item 3
</div>
<div class="item4">
This is item 4
</div>
</div>
Your problem was the padding:1em on each of the grid elements. This makes them bigger than you expect.
I've amended your example below. I hope this helps :-)
.wrapper {
display: grid;
grid-template-columns: repeat(11, 1fr);
grid-gap: 1em;
}
.wrapper > div {
background-color: #eee;
padding: 1em;
}
.wrapper > div:nth-child(odd) {
background-color: #ddd;
}
.item1 {
grid-row: 1 / 3;
grid-column: 1/6;
height: 700px;
}
.item2 {
grid-row: 1 / 1;
grid-column: 6/12;
}
.item3 {
grid-row: 2 / 3;
grid-column: 6/9;
}
.item4 {
grid-row: 2/3;
grid-column: 9/12;
}
<div class="wrapper">
<div class="item1">This is item 1</div>
<div class="item2">This is item 2</div>
<div class="item3">This is item 3</div>
<div class="item4">This is item 4</div>
</div>
Look at this codepen:
https://codepen.io/rachelandrew/pen/WQNqKy
body {
margin: 40px;
font: 80% Arial, Helvetica, sans-serif;
}
.wrapper {
display: grid;
align-items: center;
background: no-repeat url(https://s3-us-west-2.amazonaws.com/s.cdpn.io/12005/grid.png);
grid-gap: 10px;
grid-template-columns: repeat(6, 150px);
grid-template-rows: repeat( 4, 150px);
background-color: #fff;
color: #444;
}
.box {
border: 1px solid #444;
padding: 20px;
font-size: 150%;
}
.a {
grid-column: 1 / 3;
grid-row: 1 / 3;
}
.b {
grid-column: 3 / 5;
grid-row: 1 / 3;
}
.c {
grid-column: 1 / 3;
grid-row: 3 / 6;
}
.d {
grid-column: 3 / 5;
grid-row: 3 / 6;
}
.e {
grid-column: 5 / 7;
grid-row: 1 / 6;
align-self: stretch;
}
<div class="wrapper">
<div class="box a">
<p>This is box A. </p>
</div>
<div class="box b">
<p>This is box B.</p>
</div>
<div class="box c">
<p>This is box C.</p>
</div>
<div class="box d">
<p>This is box D.</p>
</div>
<div class="box e">
<p>Each of the boxes on the left has a grid area of 3 columns and 3 rows (we're counting the gutter col/row). </p>
<p>The align-items property is used to align the content inside each grid-area.</p>
<p>Other values of align-items are:</p>
<ul>
<li>stretch</li>
<li>start</li>
<li>end</li>
<li>center</li>
</ul>
</div>
</div>
from
https://gridbyexample.com/examples/example24/
Element a has these rules:
.a {
grid-column: 1 / 3;
grid-row: 1 / 3;
}
without align-items: center;
it takes the first two square (x,y)
as the rule states, but if I apply the rule
align-items: center to the parent
the size becomes smaller.
Can anyone explain why, please ?
The HTML structure of a grid container consists of three levels:
the container
the item
the content
Each of these levels represents a separate element.
When you apply align-items: center to the container, it applies to the grid item. That is exactly what is happening in your code sample.
If you want the content of the grid item centered, then you don't target it from the primary container (2 levels up). You target it from the grid item (the parent).
You can center the text using a nested grid or even flex container.
.wrapper {
display: grid;
/* align-items: center; */
grid-gap: 10px;
grid-template-columns: repeat(6, 150px);
grid-template-rows: repeat( 4, 150px);
}
.box {
display: flex; /* new */
align-items: center; /* new; vertical alignment */
justify-content: center; /* new (and optional); horizontal alignment */
}
revised codepen
body {
margin: 40px;
font: 80% Arial, Helvetica, sans-serif;
}
.wrapper {
display: grid;
/* align-items: center; */
background: no-repeat url(https://s3-us-west-2.amazonaws.com/s.cdpn.io/12005/grid.png);
grid-gap: 10px;
grid-template-columns: repeat(6, 150px);
grid-template-rows: repeat( 4, 150px);
background-color: #fff;
color: #444;
}
.box {
border: 1px solid #444;
padding: 20px;
font-size: 150%;
display: flex;
align-items: center;
justify-content: center;
/* optional */
}
.a {
grid-column: 1 / 3;
grid-row: 1 / 3;
}
.b {
grid-column: 3 / 5;
grid-row: 1 / 3;
}
.c {
grid-column: 1 / 3;
grid-row: 3 / 6;
}
.d {
grid-column: 3 / 5;
grid-row: 3 / 6;
}
.e {
grid-column: 5 / 7;
grid-row: 1 / 6;
align-self: stretch;
}
<div class="wrapper">
<div class="box a">
<p>This is box A. </p>
</div>
<div class="box b">
<p>This is box B.</p>
</div>
<div class="box c">
<p>This is box C.</p>
</div>
<div class="box d">
<p>This is box D.</p>
</div>
</div>
More details here: Centering in CSS Grid