Is this possible with CSS Grid? - html

I want the "Next" button of this section to go full width when the other div has been taken away. I need the middle div to stay as part of the same container as I will be putting this in the middle in a media query for tablet / desktop.
<div class="container">
<div class="item item--1">Page 1 of 20</div>
<div class="item item--2">Previous</div>
<div class="item item--3">Next</div>
</div>
<div class="container" style="margin-top: 40px;">
<div class="item item--1">Middle</div>
<div class="item item--2">Next</div>
</div>
.container {
display: grid;
grid-template-rows: 1fr 1fr;
grid-template-columns: 1fr 1fr;
}
.item {
background: yellow;
border: 1px solid grey;
text-align: center;
}
.item--1 {
text-align: center;
grid-row: 1;
grid-column: 1 / span 2;
}
.item--2 {
grid-row: 2;
grid-column: 1fr;
}
.item--3 {
grid-row: 2;
grid-column: 1fr;
}
https://codepen.io/chrismorrison/pen/xjjwxQ?editors=1100

You can do this by retaining the class on the "Next" div (there seems little reason to change it) and the targeting it differently when it follows the "Previous" div using the adjacent selector. +
.container {
display: grid;
grid-template-rows: 1fr 1fr;
grid-template-columns: 1fr 1fr;
}
.item {
background: yellow;
border: 1px solid grey;
text-align: center;
}
.item--1 {
text-align: center;
grid-row: 1;
grid-column: 1 / span 2;
}
.item--2 {
grid-row: 2;
grid-column: 1;
}
.item--3 {
grid-row: 2;
grid-column: 1 / span 2;
}
.item--2 + .item--3 {
grid-column: 2 / span 1;
}
<div class="container">
<div class="item item--1">Page 2 of 20</div>
<div class="item item--2">Previous</div>
<div class="item item--3">Next</div>
</div>
<div class="container" style="margin-top: 40px;">
<div class="item item--1">Page 1 of 20</div>
<div class="item item--3">Next</div>
</div>
Frankly, a better solution would be flexbox like so. This works with both Previous and Next buttons out of the box.
.container {
display: flex;
flex-wrap: wrap;
margin-top: 20px
}
.item {
background: yellow;
border: 1px solid grey;
text-align: center;
}
.item--1 {
text-align: center;
flex: 1 0 100%;
}
.item--2,
.item--3 {
flex: 1;
}
<div class="container">
<div class="item item--1">Page 1 of 20</div>
<div class="item item--3">Next</div>
</div>
<div class="container">
<div class="item item--1">Page 2 of 20</div>
<div class="item item--2">Previous</div>
<div class="item item--3">Next</div>
</div>
<div class="container">
<div class="item item--1">Page 20 of 20</div>
<div class="item item--2">Previous</div>
</div>

Related

How to make the cell of a grid as long as as entire grid

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>

How to have header in the first colmn

I'm trying to display header's elements in the first column with display: grid;
Here a snippet, plus a [codepen]
.grid-container {
margin: 1rem;
display: grid;
grid-template-columns: 2fr 2fr 1fr;
align-items: baseline;
}
.h1 {
grid-column: 1 / 2;
grid-row: 1 / 3;
}
.h2 {
grid-column: 1 / 2;
grid-row: 2 / 3;
}
.flex {
display: flex;
}
.col {
flex-direction: column;
margin: 1rem;
}
body {
background-color: aliceblue;
display: flex;
flex-direction: column;
justify-content: center;
align-content: center;
align-items: center;
text-align: center;
}
p {
margin: 0;
}
.presentation {
margin-bottom: 3rem;
}
.box {
margin: 1rem 2rem;
padding: 1rem;
background-color: #000;
color: white;
width: 80%;
}
.ok {
background: green;
}
.not-ok {
background: darkred;
}
<body>
<div class="presentation">
<h1>Hi there</h1>
<p>I'm trying to display the headers in the 1st column with grid</p>
</div>
<div class="box ok">
<div>Result wanted using flexbox</div>
<div class="flex">
<div class="col">
<div>header 1</div>
<div>header 2</div>
</div>
<div class="col">
<div>b0</div>
<div>b1</div>
</div>
<div class="col">
<div>c0</div>
<div>c1</div>
</div>
</div>
</div>
<div class="box not-ok">
<div>Grid example not working</div>
<div class="grid-container">
<div class="h1">header 1</div>
<div class="h2">header 2</div>
<div>b0</div>
<div>b1</div>
<div>c0</div>
<div>c1</div>
</div>
</div>
</body>
Thanks you for the help,
PS : I saw this post but it's 10 years old so..
So assign the headings to the first column.
div {
outline: 1px solid grey;
padding: 0 0.5em
}
.grid-container {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 0 1em;
grid-auto-flow: column;
}
.h1,
.h2 {
grid-column: 1;
}
<div class="grid-container">
<div class="h1">header 1</div>
<div class="h2">header 2</div>
<div>b0</div>
<div>b1</div>
<div>c0</div>
<div>c1</div>
</div>
If you use the HTML structure that you have for the flex example then you can use grid, telling items which column they are to go into by selection using nth-child.
.grid {
margin: 1rem;
display: grid;
grid-template-columns: 2fr 2fr 1fr;
align-items: baseline;
}
.grid .col:nth-child(1) div {
grid-column: 1 / 2;
}
.grid .col:nth-child(2) div {
grid-column: 2 / 3;
}
.grid .col:nth-child(3) div {
grid-column: 3 / 4;
}
body {
background-color: aliceblue;
display: flex;
flex-direction: column;
justify-content: center;
align-content: center;
align-items: center;
text-align: center;
}
<div class="box ok">
<div>Result wanted using grid</div>
<div class="grid">
<div class="col">
<div>header 1</div>
<div>header 2</div>
</div>
<div class="col">
<div>b0</div>
<div>b1</div>
</div>
<div class="col">
<div>c0</div>
<div>c1</div>
</div>
</div>
</div>

why box 2 not spanning 2 rows? instead it only make a empty space! i need that box span to 2 rows

I am new to CSS grid but I am having issues with the 'grid-row' not working; my 'movement' div does not seem to span from where I set the row from and does not go all the way down.
.app {
margin: 4rem;
display: grid;
grid-template-columns: 4fr 3fr;
grid-template-rows: auto repeat(3, 10vh) auto;
column-gap: 1rem;
row-gap: 2rem;
}
.box {
height: 10vh;
background-color: rgba(255, 166, 0, 0.637);
}
.balance {
grid-column: 1 / 3;
}
.movement {
grid-row: 2 / 4;
}
<div class="app">
<div class="box balance">1</div>
<div class="box movement">2</div>
<div class="box summary">3</div>
<div class="box function1">4</div>
<div class="box function2">5</div>
<div class="box function3">6</div>
</div>
this is the output I'm getting:
This is because of the height property for box class.
.box {
height: 10vh;
background-color: rgba(255, 166, 0, 0.637);
}
.app {
margin: 4rem;
display: grid;
grid-template-columns: 4fr 3fr;
grid-template-rows: auto repeat(3, 10vh) auto;
column-gap: 1rem;
row-gap: 2rem;
}
.box {
/* height: 10vh; */
background-color: rgba(255, 166, 0, 0.637);
}
.balance {
grid-column: 1 / 3;
}
.movement {
grid-row: 2 / 4;
}
<div class="app">
<div class="box balance">1</div>
<div class="box movement">2</div>
<div class="box summary">3</div>
<div class="box function1">4</div>
<div class="box function2">5</div>
<div class="box function3">6</div>
</div>

Make full part of grid page a different background color

I want background color from block 3 to block 6 to be different, but without any padding and margin. Like its full page background color.How can i do it?
.container {
display: grid;
grid-template-columns: 1fr 1fr;
column-gap: 1.5%;
row-gap: 1.5%;
margin-bottom: 30%;
}
div {padding:5%; border:1px gray solid;}
<div class = "container">
<div>
1
</div>
<div>
2
</div>
<div>
3
</div><div>
4
</div><div>
5
</div>
<div>
6
</div>
<div>
7
</div>
</div>
Check this:
.container {
display: grid;
grid-template-columns: repeat(2, 1fr);
grid-template-rows: repeat(4, 1fr);
grid-column-gap: 10px;
grid-row-gap: 10px;
}
.item {
border: 1px gray solid;
color: white;
padding: 20px;
text-align: center;
}
.item1 {
grid-area: 1 / 1 / 2 / 2;
background: red;
}
.item2 {
grid-area: 1 / 2 / 2 / 3;
background: green;
}
.item3 {
grid-area: 2 / 1 / 4 / 3;
background: blue;
}
.item4 {
grid-area: 4 / 1 / 5 / 2;
background: brown;
}
<div class="container">
<div class="item item1">
1
</div>
<div class="item item2">
2
</div>
<div class="item item3">
3-6
</div>
<div class="item item4">
7
</div>
</div>
CodePen: https://codepen.io/manaskhandelwal1/pen/qBaMoVm
I would bet there is a more elegant way to do this, but I think I have what you are looking for here by overriding the row-gap with negative margins. I did not do anything to close the column-gap on blocks 3 to 6, but should be able to accomplish the same effect with negative margins.
Is this what you are looking for?
.container {
display: grid;
grid-template-columns: 1fr 1fr;
column-gap: 1.5%;
row-gap: 1.5%;
margin-bottom: 30%;
}
div {padding:5%; border:1px gray solid;}
div.specialDiv {
padding: 0;
margin: 0;
background-color: blue;
color: white;
}
div.specialDiv.bott {
margin-top: -.375%;
margin-bottom: -.75%;
}
div.specialDiv.top {
margin-top: -.75%;
margin-bottom: -.375%;
}
<div class = "container">
<div>
1
</div>
<div>
2
</div>
<div class="specialDiv top">
3
</div>
<div class="specialDiv top">
4
</div>
<div class="specialDiv bott">
5
</div>
<div class="specialDiv bott">
6
</div>
<div>
7
</div>
</div>

Layout combining two CSS Grids

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>