How to do third column in the center in css grid? [duplicate] - css

This question already has answers here:
How to center elements on the last row in CSS Grid?
(8 answers)
Closed 3 years ago.
I have three divs. how to do two div as columns but the thrid need to be center and one column without grid-area?
Like in the picture.
I have try:
display:grid;
grid-template-columns:1fr 1fr;
grid-template-row:1fr 1fr;
But its create four columns.. and not what I need..

Create a grid with 4 columns, each div should span 2 columns, and the last child should start at the 2nd column.
.grid {
display: grid;
grid-template-columns: repeat(4, 1fr);
grid-template-rows: 1fr 1fr;
grid-gap: 2px;
}
.grid > div {
height: 20vmin;
width: 20vmin: 20vmin;
grid-column-end: span 2;
background: red;
}
.grid > div:last-child {
grid-column-start: 2;
}
<div class="grid">
<div></div>
<div></div>
<div></div>
</div>

Related

How to give all `grid-template-rows` same height dynamically? [duplicate]

This question already has answers here:
Equal height rows in CSS Grid Layout
(2 answers)
Closed 24 days ago.
This is grid-template-rows. As you can see last rows has more height. I wanna make all of the row's height same whenever a row get more height than other's.
This must be with display: grid; property.
Just set grid-auto-rows to 1fr as the example below:
More info on MDN and CSS tricks
.container {
display: grid;
grid-auto-rows: 1fr; /* make all rows the same height */
grid-template-columns: 1fr 1fr;
gap: 0.5rem;
}
.container > div {
border: 1px solid gray;
padding: 0.5rem;
}
<div class='container'>
<div>A</div>
<div>B</div>
<div>C</div>
<div>D</div>
<div>E<br>E<br>E</div>
</div>

How to make grid column width the smaller of two values?

I want to make the column width of a 3-column grid to be the minimum of 1fr or 100px.
Here's what I tried, but dev tools say it's not a valid value.
.my-grid {
display: grid;
grid-template-columns: repeat(3, minmax(0, min(100px, 1fr)));
}
Why isn't this correct?
Use percentage and not 1fr
.my-grid {
display: grid;
/* (100% - (N-1)*Gap)/N */
grid-template-columns: repeat(3, min(100px, calc(100% - 2*5px)/3));
justify-content: center;
gap: 5px;
}
.my-grid > div {
height: 50px;
background: red;
}
<div class="my-grid">
<div></div>
<div></div>
<div></div>
</div>
hey in the minmax you don't need to pass the min(), in minmax the first parameter is the min value and second is max value, you can set it accordingly.
like minmax(1fr,33%)

Grid layout, need 3 divs to occupy 2 columns and a row

I have the following markup:
<div class="wrapper">
<div class="one">One</div>
<div class="two">Two</div>
<div class="three">Three</div>
</div>
I want the layout to accomplish a layout in which the first two child divs are arranged next to each other as two columns while the 3rd sits on a row of its occupying full width.
The first child column will be a fixed width (30px), while the second should occupy the remaining space.
I have tried this, but it doesn't accomplish what I need:
.wrapper {
display: grid;
grid-template-columns: 20px auto 100%;
border:1px solid white;
}
Maybe something like this
.wrapper {
display: grid;
grid-template-columns: 30px 1fr;
grid-gap: 1rem;
}
.child {
padding: 1rem;
border: 1px solid red;
display: flex;
align-items: center;
justify-content: center;
}
.three {
grid-column: 1 / -1; /* 👈 It makes this element take all available columns (the whole row)
}
<div class="wrapper">
<div class="child one">One</div>
<div class="child two">Two</div>
<div class="child three">Three</div>
</div>
That's the method I prefer.
First, you fraction in four with grid-template: 1fr 1fr / 1fr 1fr. This is column sizes / row sizes
After that, imagine you have two squares together in the first row (first 1fr 1fr)
Now imagine the three lines (The first line of the left square draws one vertical line, the two squares touching draw a second line, and the 2nd square draws the third one).
|â–¢|â–¢|
So, you say to #red, start in line 1 and finish in line 2 grid-column: 1 / 2, to the #green, start in the two and finish in the 3, and so on.
The result looks like this:
.grid
{
display: grid;
grid-auto-flow: row dense;
/* This is column column / row row */
grid-template: 1fr 1fr / 1fr 1fr;
height: 300px;
}
#red {
background-color: red;
grid-column: 1 / 2;
grid-row: 1 / 2;
}
#green {
background-color: green;
grid-column: 2 / 3;
grid-row: 1 / 2;
}
#blue {
background-color: blue;
grid-column: 1 / 3;
grid-row: 2 / 3;
}
/* To make the first row 30px you can use this instead */
.grid
{
grid-template: 1fr 1fr / 30px 1fr;
}
<div class="grid">
<div id="blue">Blue</div>
<div id="red">Red</div>
<div id="green">Green</div>
</div>

CSS grid behaviour different in Chrome and Firefox [duplicate]

This question already has answers here:
Why does Chrome 80 cause this grid-template-rows: auto problem
(2 answers)
Closed 3 years ago.
I have a css grid with 2 columns and 3 rows. The third row content is not always present because of an angular ngif clause. An image is present in the first column that spans the 3 rows. With all that said, I get 2 different results in Chrome (80.0.3987.87, which is the latest stable as I post) and Firefox.
Is there a way to control the rows so that it behaves like in Firefox, i.e. the third row grows when the image height is bigger than the 2 first row heights combined.
.grid {
width:400px;
display:grid;
grid-template-columns: 40px 1fr;
grid-template-rows: minmax(0, auto) minmax(0, auto) minmax(0, 1fr);
}
.image {
width:40px;
height:100px;
grid-column:1;
grid-row:1 / span 3;
background-color: #f00;
}
.text1 {
grid-column:2;
grid-row:1;
}
.text2 {
grid-column:2;
grid-row:2;
}
<div class="grid">
<div class="image"></div>
<div class="text1">Text 1</div>
<div class="text2">Text 2</div>
</div>
Here is the codepen that you can check in both Chrome and Firefox.
And here is an image to show the difference:
As you can see, Chrome increases equally the height of rows 1 and 2 (row 3 stays 0). Firefox increases only the height of row2, i.e. the last visible row with content.
Note that I tried setting grid-template-rows as "minmax(0, auto) minmax(0, auto) 1fr", but then row 3 will have a weird height that makes the grid height bigger than its content.
Update: Ah! Was able to try it on a previous Chrome version (79) and I get the same result as in Firefox this time. So it seems 80 changed it. Can some people here confirm?
You can update your code like below. It seems to work the same in Chrome 80 and firefox. Worth to note that 1fr is equal to minmax(auto, 1fr) which is indeed different from minmax(0,1fr) but still don't know exactly what makes both behave differently.
.grid {
width:400px;
display:grid;
grid-template-columns: 40px 1fr;
grid-template-rows: auto auto 1fr;
}
.image {
width:40px;
height:100px;
grid-column:1;
grid-row:1 / span 3;
background-color: #f00;
}
.text1 {
grid-column:2;
grid-row:1;
}
.text2 {
grid-column:2;
grid-row:2;
}
<div class="grid">
<div class="image"></div>
<div class="text1">Text 1</div>
<div class="text2">Text 2</div>
</div>
The way you have mentioned the grid-template-rows for the container is invalid in chrome. I guess that's the issue
Try this css and see;
<style>
.grid {
display: grid;
grid-template-columns: repeat(2, 1fr);
}
.image {
height: 100px;
grid-row: 1/4;
background-color: #f00;
}
.text1 {
grid-column: 2;
grid-row: 1;
}
.text2 {
grid-column: 2;
grid-row: 2;
}
</style>
Hope that helps!!

How do you place an element on the bottom right corner of a CSS Grid? [duplicate]

This question already has answers here:
Make a grid item span to the last row / column in implicit grid
(5 answers)
Closed 5 years ago.
I have a CSS grid such as this:
.grid-wrapper
display: grid
margin: 0 auto
grid-template-columns: repeat(auto-fill, minmax(120px, 1fr))
grid-auto-rows: minmax(100px, auto)
grid-gap: 10px
And I want to position a main element in the beginning:
.main-item
grid-column-end: span 2
grid-row-end: span 2
A series of intermediate elements following that one, and in the last possible position of the grid -not following the other elements, but literally on the right bottom corner of the grid-.
Right now I can place it on the last column with
.last item
grid-column-end: -1
But I can't find a way of placing it on the last row -given that the number of rows is not defined-.
Any ideas?
https://codepen.io/rtyx/pen/XVQMJQ
.wrapper {
border: 2px solid #f76707;
background-color: #fff4e6;
}
.wrapper>div {
border: 2px solid #ffa94d;
background-color: #ffd8a8;
color: #d9480f;
}
.wrapper {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(120px, 1fr));
grid-auto-rows: minmax(100px, auto);
grid-gap: 10px;
width: 1000px;
}
.main-item {
grid-column-end: span 2;
grid-row-end: span 2;
}
.last-item {
grid-column-end: -1;
grid-row-end: -1;
}
<div class="wrapper">
<div class="main-item">Main item</div>
<div class="item1">Item 2</div>
<div class="item2">Item 3</div>
<div class="item3">Item 4</div>
<div class="last-item">last-item</div>
</div>
Based on your codepen. In class .last-item set grid-row-end:3
For same layout use this...
.last-item {
grid-column-end: -1;
grid-row-end: 4;
}

Resources