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.
Related
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 am inserting items from list to grid.
When I am inserting 2 items with same grid-column it's inserted one after another (which is good). But if next item is Could fit at top of rows I would like to place it there.
.container {
display: grid;
grid-gap: 20px;
grid-template-columns: repeat(7, 1fr);
}
.item1 {
grid-column: 3 / 4;
background-color: coral;
}
.item2 {
grid-column: 5 / 5;
background-color: red;
}
<div class="container">
<div class='item1'>1</div>
<div class='item1'>2</div>
<div class='item2'>3</div>
</div>
https://jsfiddle.net/epr5atvw/
How can i move item 3 so it will be next to item 1? My list is sorted and i cannot change it.
Use grid-auto-flow:column;
.container {
display: grid;
grid-gap: 20px;
grid-template-columns: repeat(7, 1fr);
grid-auto-flow: column;
}
.item1 {
grid-column: 3 / 4;
background-color: coral;
}
.item2 {
grid-column: 5 / 5;
background-color: red;
}
<div class="container">
<div class='item1'>1</div>
<div class='item1'>2</div>
<div class='item2'>3</div>
</div>
I'm working on a assignment in which I want to make two groups of css-grids mixed with each other like this:
I'm using the following code
.group1 .item1 {
grid-column: 1 / 4;
}
.group1 .item2 {
grid-column: 1;
}
.group1 .item3 {
grid-column: 2 / 4;
}
.group2 .item4 {
grid-column: 2 / 3;
}
.group2 .item5 {
grid-column: 3 / 4;
}
.group2 .item6 {
grid-column: 2 / 4;
}
.container {
display: grid;
grid-gap: 5px;
max-width: 1200px;
margin: 0 auto 100px auto;
border: 8px dashed #999;
}
<section class="part5 container">
<div class="container group1">
<div class="item item1">Item 1</div>
<div class="item item2">Item 2</div>
<div class="item item3">Item 3</div>
</div>
<div class="container group2">
<div class="item item4">Item 4</div>
<div class="item item5">Item 5</div>
<div class="item item6">Item 6</div>
</div>
</section>
I'm expecting the output to be like the [image] attached WITHOUT CHANGING HTML but I'm unable to get that output, please help me, I shall be very thankful to you for this act of kindness.
You could use display:contents to avoid the subcontainers to come in the way and use display grid and grid-area (grid-row/grid-column) to dispatch your elements.
But this is not yet working everywhere !
Demo of the idea
.part5 {
display: grid;
grid-template-colums: repeat(6, 1fr);
min-height: 100vh
}
.container.group1,
.container.group2 {
display: contents;
}
.item1 {
grid-column: 1/ span 6;
grid-row: 1;
border: solid;
color: tomato;
}
.item2 {
grid-row: 2 /span 3;
grid-column: 1 /span 2;
border: solid;
color: turquoise;
}
.item3 {
grid-row: 2;
grid-column: 3/span 4;
border: solid;
color: green;
}
.item4 {
grid-row: 3;
grid-column: 3 /span 2;
border: solid;
}
.item5 {
grid-row: 3;
grid-column: 5 / span 2;
border: solid;
color: brown;
}
.item6 {
grid-row: 4;
grid-column: 3 / span 4;
border: solid;
color: purple;
}
/* demo*/
* {
margin: 0;
}
[class^=item] {
display: flex;
align-items: center;
justify-content: center;
font-size: calc(2vh + 2vw)
}
<section class="part5 container">
<div class="container group1">
<div class="item item1">Item 1</div>
<div class="item item2">Item 2</div>
<div class="item item3">Item 3</div>
</div>
<div class="container group2">
<div class="item item4">Item 4</div>
<div class="item item5">Item 5</div>
<div class="item item6">Item 6</div>
</div>
</section>
https://css-tricks.com/get-ready-for-display-contents/
—a magical new display value that essentially makes the container disappear, making the child elements children of the element the next level up in the DOM.
from your code, it could be a short code update :
/*update */
.container {
display: contents
}
.part5 {
/* end update */
display: grid;
grid-gap: 5px;
max-width: 1200px;
margin: 0 auto 100px auto;
border: 8px dashed #999;
}
.group1 .item1 {
grid-column: 1 / 4;
}
.group1 .item2 {
grid-column: 1;
grid-row: 2/5;
}
.group1 .item3 {
grid-column: 2 / 4;
}
.group2 .item4 {
grid-column: 2 / 3;
}
.group2 .item5 {
grid-column: 3 / 4;
}
.group2 .item6 {
grid-column: 2 / 4;
}
.container {
display: contents
}
.part5 {
display: grid;
grid-gap: 5px;
max-width: 1200px;
margin: 0 auto 100px auto;
border: 8px dashed #999;
}
/*demo*/
div {
box-shadow: 0 0 0 2px lightgray;
<section class="part5 container">
<div class="container group1">
<div class="item item1">Item 1</div>
<div class="item item2">Item 2</div>
<div class="item item3">Item 3</div>
</div>
<div class="container group2">
<div class="item item4">Item 4</div>
<div class="item item5">Item 5</div>
<div class="item item6">Item 6</div>
</div>
</section>
The rough way is to set both groups on the same grid overlapping them :
.container {
display: grid;
width: 100%;
grid-template-columns: repeat(6, 1fr);
grid-template-rows: repeat(4, 1fr);
}
.group1 {
grid-row: 1 / span 4;
grid-column: 1 / span 6;
}
.group2 {
grid-template-rows: repeat(2, 1fr);
grid-column: 3 /span 4;
grid-row: 3 /span 3;
}
.item1 {
grid-column: 1 / span 6;
color: tomato;
}
.item2 {
grid-column: 1 / span 2;
grid-row: 2 / span 4;
color: turquoise;
}
.item3 {
grid-column: 3 / span 4;
color: green;
}
.item4 {
grid-column: 1 /span 3;
grid-row: 1;
}
.item5 {
grid-column: 4/span 3;
color: brown;
}
.item6 {
grid-column: 1/ span 6;
color: purple;
}
/* demo*/
[class^=item] {
border: solid;
display: flex;
align-items: center;
justify-content: center;
font-size: calc(2vh + 2vw);
background: lightgray;
min-height:20vh
}
<section class="part5 container">
<div class="container group1">
<div class="item item1">Item 1</div>
<div class="item item2">Item 2</div>
<div class="item item3">Item 3</div>
</div>
<div class="container group2">
<div class="item item4">Item 4</div>
<div class="item item5">Item 5</div>
<div class="item item6">Item 6</div>
</div>
</section
.container{
display: grid;
grid-template-columns: repeat(6,auto);
grid-gap: 5px;
grid-template-areas:
'item1 item1 item1 item1 item1 item1'
'item2 item2 item3 item3 item3 item3'
'item2 item2 item3 item3 item3 item3'
'item2 item2 item4 item4 item5 item5'
'item2 item2 item4 item4 item5 item5'
'item2 item2 item6 item6 item6 item6'
;
}
.box{
border: 2px solid black;
background-color: lightblue;
padding: 10px;
border-radius: 12px;
text-align: center;
}
#item1{
grid-area: item1;
}
#item2{
grid-area: item2;
}
#item3{
grid-area: item3;
}
#item4{
grid-area: item4;
}
#item5{
grid-area: item5;
}
#item6{
grid-area: item6;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div class="container">
<div class="box" id="item1">item-1</div>
<div class="box" id="item2">item-2</div>
<div class="box" id="item3">item-3</div>
<div class="box" id="item4">item-4</div>
<div class="box" id="item5">item-5</div>
<div class="box" id="item6">item-6</div>
</div>
</body>
</html>
display: subgrid
A clean and efficient way to solve this problem would be to use display: subgrid, which is a CSS Grid feature designed specifically for these sorts of layouts. Subgrids allow nested grid containers to recognize the grid lines of the primary grid container.
Unfortunately, this feature is not available yet. More details here:
Positioning content of grid items in primary container (subgrid feature)
grid-template-areas
Another clean and efficient way to solve the problem would be to make the primary container (.part5.container) a grid container, then arrange both child containers in the shape that you need using grid-template-areas.
Unfortunately, this feature is also not available yet. More details here:
grid-template-areas with ASCII art is not working
A possible solution
So here's a solution using CSS Grids and (to compensate for the missing features listed above) a little bit of absolute positioning. No changes to the HTML.
.part5.container {
display: grid;
border: 8px dashed #999;
height: 100vh;
grid-template-rows: auto auto;
grid-template-columns: 35% 1fr;
grid-template-areas: " group1 group1 "
" . group2 ";
}
.container.group1 {
grid-area: group1;
display: grid;
grid-template-rows: 50px 1fr;
grid-template-columns: 35% 1fr;
grid-gap: 5px;
position: relative;
}
.item1 {
grid-column: 1 / -1;
}
.item2 {
position: absolute;
top: 55px; /* top row height plus gap */
width: 35%; /* first column width */
height: calc(100vh - 71px); /* minus height of top row (50px) plus borders (16px)) */
}
.item3 {
grid-column: 2;
}
.container.group2 {
grid-area: group2;
display: grid;
grid-template-rows: 1fr 50px;
grid-template-columns: 1fr 1fr;
grid-gap: 5px;
margin: 5px 0 0 5px;
}
.item6 {
grid-column: 1 / -1;
}
.item {
background-color: lightgreen;
display: flex;
align-items: center;
justify-content: center;
}
body {
margin: 0;
}
* {
box-sizing: border-box;
}
<section class="part5 container">
<div class="container group1">
<div class="item item1">Item 1</div>
<div class="item item2">Item 2</div>
<div class="item item3">Item 3</div>
</div>
<div class="container group2">
<div class="item item4">Item 4</div>
<div class="item item5">Item 5</div>
<div class="item item6">Item 6</div>
</div>
</section>
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>
This question already has answers here:
Create a Masonry grid with flexbox (or other CSS)
(3 answers)
Closed 5 years ago.
Consider this example:
Notice that that 4th item is pushed to top instead of aligning with the 3rd item. I can't achieve this using flexbox's align-items: flex-end, neither with floats.
I am aware of achieving this by using masonry/isotope, but I would like to avoid using javascript just for this layout.
Is it possible to achieve using only CSS?
Yes, it's possible via CSS Grid Layout:
.grid {
display: grid;
grid-gap: 10px 30px;
grid-template-rows: auto 1fr auto;
}
/* styles just for demo */
.grid__item {
background-color: #e0e0e0;
font-size: 30px;
padding: 10px;
}
.b, .d {
align-self: flex-start;
}
.a {
grid-row: 1 / span 2;
/* setting height just for demo */
height: 200px;
}
.b {
grid-column: 2;
}
.c {
grid-row: 3;
}
.d {
grid-column: 2;
}
<div class="grid">
<div class="grid__item a">1</div>
<div class="grid__item b">2</div>
<div class="grid__item c">3</div>
<div class="grid__item d">4</div>
</div>
If you need IE\Edge support you should use old grid syntax. You can fake grid-gap using additional grid columns and rows. Demo:
.grid {
display: -ms-grid;
display: grid;
-ms-grid-columns: 1fr 30px 1fr;
grid-template-columns: 1fr 30px 1fr;
-ms-grid-rows: auto 10px 1fr 10px auto;
grid-template-rows: auto 10px 1fr 10px auto;
}
/* styles just for demo */
.grid__item {
background-color: #e0e0e0;
font-size: 30px;
padding: 10px;
}
.b, .d {
-ms-grid-row-align: start;
align-self: flex-start;
}
.a {
-ms-grid-row-span: 3;
grid-row: 1 / span 3;
/* setting height just for demo */
height: 200px;
}
.b {
-ms-grid-column: 3;
grid-column: 3;
}
.c {
-ms-grid-row: 5;
grid-row: 5;
}
.d {
-ms-grid-column: 3;
grid-column: 3;
-ms-grid-row: 3;
grid-row: 3;
}
<div class="grid">
<div class="grid__item a">1</div>
<div class="grid__item b">2</div>
<div class="grid__item c">3</div>
<div class="grid__item d">4</div>
</div>