CSS Grid Unused space on left and right [closed] - css

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
I am trying to create CSS grid components but one problem keeps poping up and for the life of me I cant find any solution.
So for a basic example:
.grid {
display: grid;
grid-gap: 1px;
background: black;
grid-template-columns: 1fr 1fr 1fr;
grid-template-rows: 1fr 1fr 1fr;
}
.item {
background: white;
}
<div class="grid">
<div class="item">a</div>
<div class="item">b</div>
<div class="item">c</div>
<div class="item">a</div>
<div class="item">b</div>
<div class="item">c</div>
<div class="item">a</div>
<div class="item">b</div>
<div class="item">c</div>
</div>
What gives the following result:
The question is:
How do you remove the side space, the black one to be the same as the grid gap?
It is a bit simplistic, but a major problem. What am i missing?
I tried margins, and puddings, all sorts of "work-around" but all of them made different problems down the way.
I feel like this had to have an answer already, but for the life of me I can't find a proper solution.

body {margin: 0;} works for me.
body {
margin: 0;
}
.grid {
display: grid;
grid-gap: 1px;
background: black;
grid-template-columns: 1fr 1fr 1fr;
grid-template-rows: 1fr 1fr 1fr;
}
.item {
background: white;
}
<div class="grid">
<div class="item">a</div>
<div class="item">b</div>
<div class="item">c</div>
<div class="item">a</div>
<div class="item">b</div>
<div class="item">c</div>
<div class="item">a</div>
<div class="item">b</div>
<div class="item">c</div>
</div>

Ok so I Figured it out. So I'm not sure whether this is a bug or I'm missing something, but the solution is to wrap the grid in to a wrapper like:
<style>
.grid-container {
border:1px solid red;
}
.grid {
display: grid;
grid-gap: 1px;
background: black;
grid-template-columns: 1fr 1fr 1fr;
grid-template-rows: 1fr 1fr 1fr;
border:1px solid green;
}
.item {
background: white;
}
</style>
<div class="grid-container">
<div class="grid">
<div class="item">a</div>
<div class="item">b</div>
<div class="item">c</div>
<div class="item">a</div>
<div class="item">b</div>
<div class="item">c</div>
<div class="item">a</div>
<div class="item">b</div>
<div class="item">c</div>
</div>
</div>
The wrapper (red border) takes the available space, but the grid then fits properly, without the wrapper the grid would spread out like the wrapper but behave like it was inside the wrapper.
As i mentioned in a comment this is happening with razor components in Blazor Server Side project. Not sure if this is an exclusive problem to me, the project or technology.
P.S.
The wrapper does not need a CSS class or styling, it can be a simple tag.

Related

CSS grid: place items in "at least column 2"

I have the following HTML:
<div class="grid">
<div class="special-1"></div>
<div class="special-1"></div>
<div class="special-2"></div>
<div class="special-2"></div>
<div class="special-2"></div>
<div class="special-2"></div>
<div class="special-2"></div>
<div class="special-2"></div>
</div>
.special-1 is always in the first column. The number of .special-2s is variable, generated by user data. So approximately:
.grid {
display: grid;
grid-template-columns: repeat(4, 1fr);
grid-auto-rows: 1fr;
}
.special-1 {
grid-column-start: 1;
}
Now the thing is, I want .special-2 to always be in at least the second column. But it can also show in the third or fourth. Is there anyway I can do this? grid-column-start: 2; doesn't work because it will put all of them in column 2 and none in the other columns. So I basically want grid-column: :not(1) or grid-column: 2 | 3 | 4 if you know what I mean. Any ideas?
You can approximate it like below. However, the order may not be like want.
.grid {
display: grid;
grid-template-columns: repeat(3, 1fr); /* only 3 explicit clumns */
grid-auto-columns: 1fr; /* this will size the implicit one */
grid-auto-flow: dense; /* make sure to fill all the tracks */
grid-auto-rows: 100px;
grid-gap: 5px;
}
.special-2 {
background: red;
}
.special-1 {
/* place the #1 outside the explict column
it will create an implicit column at the start
*/
grid-column-end: -4;
background: blue;
}
/* place all the #2 in the implicit columns
pay attention to the order, they can be scrambled
*/
.special-2:nth-child(3n+1) {grid-column:1}
.special-2:nth-child(3n+2) {grid-column:2}
.special-2:nth-child(3n+3) {grid-column:3}
<div class="grid">
<div class="special-1"></div>
<div class="special-1"></div>
<div class="special-2"></div>
<div class="special-2"></div>
<div class="special-2"></div>
<div class="special-2"></div>
<div class="special-2"></div>
<div class="special-2"></div>
<div class="special-2"></div>
<div class="special-2"></div>
<div class="special-2"></div>
</div>
The above will make only 3 columns if there is no element special-1
.grid {
display: grid;
grid-template-columns: repeat(3, 1fr); /* only 3 explicit clumns */
grid-auto-columns: 1fr; /* this will size the implicit one */
grid-auto-flow: dense; /* make sure to fill all the tracks */
grid-auto-rows: 100px;
grid-gap: 5px;
}
.special-2 {
background: red;
}
.special-1 {
/* place the #1 outside the explict column
it will create an implicit column at the start
*/
grid-column-end: -4;
background: blue;
}
/* place all the #2 in the implicit columns
pay attention to the order, they can be scrambled
*/
.special-2:nth-child(3n+1) {grid-column:1}
.special-2:nth-child(3n+2) {grid-column:2}
.special-2:nth-child(3n+3) {grid-column:3}
<div class="grid">
<div class="special-2"></div>
<div class="special-2"></div>
<div class="special-2"></div>
<div class="special-2"></div>
<div class="special-2"></div>
<div class="special-2"></div>
<div class="special-2"></div>
<div class="special-2"></div>
<div class="special-2"></div>
</div>
Use the below if you will always have element special-1
.grid {
display: grid;
grid-template-columns: repeat(4, 1fr);
grid-auto-flow: dense;
grid-auto-rows: 100px;
grid-gap: 5px;
}
.special-2 {
background: red;
}
.special-1 {
grid-column: 1;
background: blue;
}
.special-2:nth-child(3n+1) {grid-column:2}
.special-2:nth-child(3n+2) {grid-column:3}
.special-2:nth-child(3n+3) {grid-column:4}
<div class="grid">
<div class="special-1"></div>
<div class="special-1"></div>
<div class="special-2"></div>
<div class="special-2"></div>
<div class="special-2"></div>
<div class="special-2"></div>
<div class="special-2"></div>
<div class="special-2"></div>
<div class="special-2"></div>
<div class="special-2"></div>
<div class="special-2"></div>
</div>
The answer by Temani Afif surely works, but isn't very scalable. In my usecase there's some different column counts for different viewport sizes. I eventually decided to implement this differently. Note: if by the time you read this CSS Grid Level 2 has been released, this could trivially be solved using css subgrid: caniuse
Now for my solution: I changed my html as such:
<div class="grid">
<div class="special-1s"
<div class="special-1"></div>
<div class="special-1"></div>
</div>
<div class="special-2s">
<div class="special-2"></div>
<div class="special-2"></div>
<div class="special-2"></div>
<div class="special-2"></div>
<div class="special-2"></div>
<div class="special-2"></div>
</div>
</div>
And my css now looks somewhat like below. I forced the rows to a fixed height, which was okay in my use case and made this solution possible.
.grid {
display: flex;
}
.grid > div {
display: grid;
grid-auto-rows: 100px;
}
.special-1s {
flex: 1;
grid-template-columns: 1fr;
}
.special-2s {
--columns: 2;
flex: var(--columns);
grid-template-columns: repeat(var(--columns), 1fr);
#media (min-width: 1000px) {
--columns: 3;
}
}
By using the flex-property, I can fake consistent column-sizes across the width of .grid. The --columns property allows me to easily define extra media-queries. (I actually have a few more than shown here.)

Use css Grid to divide unknow number of items into two columns, but list in column order

I have a number of items I wish to divide into two even columns.
I have an example here
So I have a number of items..
#parent {
display: grid;
grid-template-columns: 1fr 1fr;
grid-auto-flow: row;
}
.item {
margin: 5px;
height: 40px;
width: 50px;
background: green;
color: white;
}
<div id="parent">
<div class="item">1</div>
<div class="item">2</div>
<div class="item">3</div>
<div class="item">4</div>
<div class="item">5</div>
<div class="item">6</div>
</div>
and this gives me
But what I want is for the items to "flow" down the columns, ie they will be in the order
1 4
2 5
3 6
I thought the property grid-auto-flow would do this, but I can't get it to do what I am after
Is there an easy way I can do this (preferably with css grid)?
It doesn't always have to be the latest layout technique; sometimes there already is an older one that was specifically created to get the job at hand done.
That being said, consider using columns, which does what you want, out of the box:
#parent {
columns: 2;
}
.item {
break-inside: avoid;
margin: 5px;
height: 40px;
width: 50px;
background: green;
color: white;
}
<div id="parent">
<div class="item">1</div>
<div class="item">2</div>
<div class="item">3</div>
<div class="item">4</div>
<div class="item">5</div>
<div class="item">6</div>
<div class="item">7</div>
</div>
Important Side Note: As #Yousaf has pointed out, this can lead to a single item being spread to two columns, because the browsers tries to make sure both columns are about the same height. To avoid that, simply use
break-inside: avoid;
for the column items.

Grid display columns have not equal width [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
With
grid-template-columns:1fr 1fr;
the 2 columns have different width in small(narrow) screen. I can not understand.
Thank you for any help.
In the ideal case, the two cells will have the same size.
But every cell has a minimum size! It is defined as the minimum content width. For example: the larger word, the larger button, or the larget image that it contains.
To avoid this, you should use minmax(0, 1fr) for each column definition. It allows you to say that 1fr, one fraction, is the maximum width of the column.
To sum up, use this code:
.grid {
display: grid;
grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
}
Here is a live demo of the problem and solution.
.grid {
display: grid;
grid-template-columns: 1fr 1fr;
}
.grid--fix {
grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
}
/* Demo styles */
.grid {
grid-gap: 20px;
max-width: 400px;
}
.cell {
background: hotpink;
text-align: center;
padding: 1em;
}
<p>Exact same cells</p>
<div class="grid">
<div class="cell">Cell 1</div>
<div class="cell">Cell 2</div>
</div>
<p>The cells adapt their size to contain their content</p>
<div class="grid">
<div class="cell">Cell 1</div>
<div class="cell">Cell 2 is biiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiig</div>
</div>
<p>This behaviour could be disabled with <code>minmax(0, 1fr)</code></p>
<div class="grid grid--fix">
<div class="cell">Cell 1</div>
<div class="cell">Cell 2 is biiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiig</div>
</div>

How to make css grid items always center [duplicate]

This question already has answers here:
Aligning grid items across the entire row/column (like flex items can)
(3 answers)
What is difference between justify-self, justify-items and justify-content in CSS grid?
(4 answers)
Centering in CSS Grid
(9 answers)
Closed 4 years ago.
I am using CSS grid for some box items. This looks good and fine but however when I make the screen smaller when there are only 2 or 1 boxes in the container its to the left how would I go about making them always in the center?
body {
background-color: #8268EE;
}
.item-container {
display: grid;
grid-gap: 20px;
grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
justify-content: center;
align-items: center;
}
.item {
background-color: #BDD3FB;
width: 200px;
height: 200px;
}
<div class='item-container'>
<div class='item'></div>
<div class='item'></div>
<div class='item'></div>
<div class='item'></div>
<div class='item'></div>
<div class='item'></div>
<div class='item'></div>
<div class='item'></div>
<div class='item'></div>
</div>
Almost there, use the justify-items property instead of justify-content:
body {
background-color: #8268EE;
}
.item-container {
display: grid;
grid-gap: 20px;
grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
align-items: center;
justify-items: center;
}
.item {
background-color: #BDD3FB;
width: 200px;
height: 200px;
}
<div class='item-container'>
<div class='item'></div>
<div class='item'></div>
<div class='item'></div>
<div class='item'></div>
<div class='item'></div>
<div class='item'></div>
<div class='item'></div>
<div class='item'></div>
<div class='item'></div>
</div>
Actually silly me I've figured it out. All it took was justify-self: center; on the child item and it worked! I had thought when the documentation was talking about it that it was referring to the flex item inside. Then I though oh wait? You can't justify-self a flex parent that not in a flex!

auto-fit and minmax() don't work inside nested grid

I have two nested grids - one for the layout and one for the part of my site (let's say it's a list of goods in the shop). My layout grid creates a container for the whole site, including navbar, sidebar, content, etc. And nested grid is responsible for the list of goods only. The problem is that auto-fit and minmax functions don't work in the nested grid. You can check this pen to see the case.
At first, try to change width of the content, you'll see that items are changing its position according to the auto-fit algorithm. But as soon as you uncomment display: grid; for the outer grid, it responsiveness gets broken. Could you please explain why this is happening and how I can fix it?
.outer-grid {
/* display: grid; */
grid-template-columns: 1fr 700px 1fr;
}
.inner-grid {
grid-column: 2;
display: grid;
grid-template-columns: repeat(auto-fit, minmax(100px, 1fr));
}
.item {
background: red;
border: 1px solid black;
}
<div class="outer-grid">
<div class="inner-grid">
<div class="item">Item</div>
<div class="item">Item</div>
<div class="item">Item</div>
<div class="item">Item</div>
<div class="item">Item</div>
</div>
</div>
The problem is that auto-fit and minmax functions don't work in the nested grid.
I think they do work. The problem appears to be something else.
Your nested grid exists in a column with a fixed width (700px). The primary container sees no reason to shrink that column, which would trigger the auto-fit function in the nested grid.
Here's something you may want to consider:
revised codepen
.outer-grid {
display: grid;
grid-template-columns: 1fr repeat(1, minmax(100px, 700px)) 1fr;
}
.inner-grid {
grid-column: 2;
display: grid;
grid-template-columns: repeat(auto-fit, minmax(100px, 1fr));
}
.item {
background: red;
border: 1px solid black;
}
<div class="outer-grid">
<div class="inner-grid">
<div class="item">Item</div>
<div class="item">Item</div>
<div class="item">Item</div>
<div class="item">Item</div>
<div class="item">Item</div>
</div>
</div>

Resources