Grid display columns have not equal width [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 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>

Related

Auto rows based on content in column [duplicate]

This question already has answers here:
CSS-only masonry layout
(4 answers)
Closed 1 year ago.
Hey,
i am trying to achieve this kind of layout with css grid.
I have set the container div of these items to
display: gridand grid-template-columns: repeat(3, 1fr). Then i layed out the items with grid-column and grid row.
This gives me an 3x3 grid. But i need a grid with 2 rows in the first column, 3 rows in the middle column and two rows in the third column. Based on the size of the item. Is there any way of doing this with css grid, i am stuck at the moment. Thanks in advance
You can take up to 2 spaces with grid-row: span 2;
.grid {
display: grid;
grid-gap: 5px;
grid-template-columns: repeat(3, 1fr);
grid-auto-rows: 50px;
}
.gr2 {
grid-row: span 2;
}
.box {
background-color: red;
}
<div class="grid">
<div class="box gr2">1</div>
<div class="box">2</div>
<div class="box">3</div>
<div class="box">4</div>
<div class="box gr2">5</div>
<div class="box">6</div>
<div class="box">7</div>
</div>

Having a "display: inline-block" as a parent to a "display: grid" element without having text overlap?

I'm trying to arrange a set of statistics such that:
they are displayed on a single horizontal line
the enclosing element is no wider than it needs to be to contain the content
there should be a fixed gap between statistics
I tried implementing this using display: grid. Here is my approach:
.outer {
background-color: yellow;
display: inline-block;
}
.stats {
display: grid;
grid-gap: 20px;
grid-template-columns: repeat(auto-fit, minmax(0, 1fr));
}
<div class="outer">
<div class="stats">
<div class="stat">
<strong>Value:</strong> 1,234,568
</div>
<div class="stat">
<strong>Another value:</strong> 98,765
</div>
<div class="stat">
<strong>Test value:</strong> 83,263
</div>
</div>
</div>
Unfortunately, this results in some strange overlapping. Despite there being plenty of room for the .outer element to expand, the statistics are wrapped over several lines and the text runs into other columns:
How can I avoid this problem? I tried adding:
.stat {
white-space: nowrap;
}
...but the text still "runs" together. What am I missing?
The main problem stems from this declaration:
grid-template-columns: repeat(auto-fit, minmax(0, 1fr))
You're setting the columns to shrink to zero width.
Also, 1fr does nothing here, because there is no free space in the container. (You have the primary container set to inline-block, i.e., min-width. This means no extra space.)
At a minimum, these commands appear to be confusing the inline-block algorithm.
Consider leaving the columns at auto width (a default setting).
And, perhaps, setting them to appear in the first row. (I used the grid-auto-flow: column technique.)
.outer {
background-color: yellow;
display: inline-block;
}
.stats {
display: grid;
grid-gap: 20px;
grid-auto-flow: column;
}
<div class="outer">
<div class="stats">
<div class="stat">
<strong>Value:</strong> 1,234,568
</div>
<div class="stat">
<strong>Another value:</strong> 98,765
</div>
<div class="stat">
<strong>Test value:</strong> 83,263
</div>
</div>
</div>

CSS Grid Unused space on left and right [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 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.

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>

How to repeat grid-template-rows for all rows

I'm trying create template for rows in my grid block:
grid-template-rows: repeat(3, 150px);
I know, this template should be work for first 3 rows.
However, from 4 row this template is not work.
Can i make template for all rows?
P.S.
This template work only for 1st row.
grid-template-rows: 150px;
Use grid-auto-rows (automatically generated rows) instead of grid-template-rows (manually generated rows). In current case grid-auto-rows: 150px will do the trick. Demo:
.grid {
display: grid;
grid-auto-rows: 150px;
/* space between columns for demo */
grid-gap: 10px;
}
/* just styles for demo */
.grid__item {
background-color: tomato;
color: white;
}
<div class="grid">
<div class="grid__item">One</div>
<div class="grid__item">Two</div>
<div class="grid__item">Three</div>
<div class="grid__item">Four</div>
<div class="grid__item">Five</div>
<div class="grid__item">Six</div>
<div class="grid__item">Seven</div>
<div class="grid__item">Eight</div>
<div class="grid__item">Nine</div>
</div>

Resources