I have a CSS grid layout where I have the top row spanning the entire grid using the grid-column property.
Does anyone know how I can set this row to be 100px high and have all the subsequent rows set to a grid-auto-rows height of 200px?
I know I can input individual values with grid-template-rows for all the specific rows, but there are going to be a lot of divs on the page and don't want to input 100px and then a load of 200px values using this grid-template-rows property.
I'm thinking there must be a way to set individual pixel values on certain rows and then have everything else as grid-auto-rows?
I can't seem to find how to do this in the docs.
Any help would be awesome!
https://codepen.io/emilychews/pen/dZPJGQ
.gridwrapper {
display: grid;
grid-template-columns: repeat(2, 2fr);
grid-auto-rows: 200px;
grid-template-rows: 100px 200px;
grid-gap: 10px;
}
nav {
background: yellow;
}
.gridwrapper div {
padding: 1em;
background: red;
color: white;
box-sizing: border-box;
}
.gridwrapper div:nth-child(odd) {
background: blue;
}
nav {
grid-column: 1 / -1;
}
/*MAKE DIVS 1FR ON MOBILE*/
#media only screen and (max-width: 736px) {
.gridwrapper {
grid-template-columns: 1fr;
}
}
<div class="gridwrapper">
<nav class="grid">1</nav>
<div class="grid">2</div>
<div class="grid">3</div>
<div class="grid">4</div>
<div class="grid">5</div>
<div class="grid">6</div>
</div>
Does anyone know how I can set this row to be 100px high and have all the subsequent rows set to a grid-auto-rows height of 200px?
Yes. Grid can do this cleanly and easily.
First, you don't need to set a height value on the grid item itself. That overrides one of the great benefits of CSS Grid: the ability to control the dimensions of grid items at the container level.
You know that your grid will always have at least one row: the top row. That's your explicit grid.
You don't know how many additional rows there will be. That number is variable and unpredictable. That's your implicit grid.
The grid-template-rows and grid-template-columns properties set track sizes in the explicit grid.
The grid-auto-rows and grid-auto-columns properties set track sizes in the implicit grid.
Therefore, this is probably what you're looking for:
.gridwrapper{
display: grid;
grid-template-rows: 100px; /* top row is 100px in height */
grid-auto-rows: 200px; /* any new rows created are 200px in height */
}
revised codepen
You can use grid-template-rows property.
I guess you are looking for something like this:
https://codepen.io/harora/pen/EbaQxr
Here is a nice reference to the Grid model:
Grid Model Explained
Related
I am creating a calendar layout in Ruby on Rails and each day box should have equal height regardless of how many events there are per day. This is the CSS I currently have. How can I fix the height of each box to a certain size?
.calendar-container {
display: grid;
grid-template-columns: repeat(7, 200px);
}
.day-calendar-item {
background-color: #f4fcff;
border-width: 2px;
}
I believe you need the grid-template-rows property. Try to add grid-template-rows: repeat(x, 200px) to the .calendar-container class where x is the number of rows you want and 200px is the size of each row (although you can change this to your preference).
TL;DR: Is there anything like table-layout: fixed for CSS grids?
I tried to create a year-view calendar with a big 4x3 grid for the months and therein nested 7x6 grids for the days.
The calendar should fill the page, so the year grid container gets a width and height of 100% each.
.year-grid {
width: 100%;
height: 100%;
display: grid;
grid-template: repeat(3, 1fr) / repeat(4, 1fr);
}
.month-grid {
display: grid;
grid-template: repeat(6, 1fr) / repeat(7, 1fr);
}
Here's a working example: https://codepen.io/loilo/full/ryXLpO/
For simplicity, every month in that pen there has 31 days and starts on a Monday.
I also chose a ridiculously small font size to demonstrate the problem:
Grid items (= day cells) are pretty condensed as there are several hundreds of them on the page. And as soon as the day number labels become too large (feel free to play around with the font size in the pen using the buttons on the upper left) the grid will just grow in size and exceed the page's body size.
Is there any way to prevent this behaviour?
I initially declared my year grid to be 100% in width and height so that's probably the point to start at, but I couldn't find any grid-related CSS properties that would've fitted that need.
Disclaimer: I'm aware that there are pretty easy ways to style that calendar just without using CSS Grid Layout. However, this question is more about the general knowledge on the topic than solving the concrete example.
By default, a grid item cannot be smaller than the size of its content.
Grid items have an initial size of min-width: auto and min-height: auto.
You can override this behavior by setting grid items to min-width: 0, min-height: 0 or overflow with any value other than visible.
From the spec:
6.6. Automatic Minimum Size of Grid
Items
To provide a more reasonable default minimum size for grid items, this
specification defines that the auto value of min-width / min-height also applies an automatic minimum size in the specified axis to grid items whose overflow is visible. (The effect is analogous to the automatic minimum size imposed on flex items.)
Here's a more detailed explanation covering flex items, but it applies to grid items, as well:
Why don't flex items shrink past content size?
This post also covers potential problems with nested containers and known rendering differences among major browsers.
To fix your layout, make these adjustments to your code:
.month-grid {
display: grid;
grid-template: repeat(6, 1fr) / repeat(7, 1fr);
background: #fff;
grid-gap: 2px;
min-height: 0; /* NEW */
min-width: 0; /* NEW; needed for Firefox */
}
.day-item {
padding: 10px;
background: #DFE7E7;
overflow: hidden; /* NEW */
min-width: 0; /* NEW; needed for Firefox */
}
jsFiddle demo
1fr vs minmax(0, 1fr)
The solution above operates at the grid item level. For a container level solution, see this post:
Who does minmax(0, 1fr) work for long elements while 1fr doesn't?
The previous answer is pretty good, but I also wanted to mention that there is a fixed layout equivalent for grids, you just need to write minmax(0, 1fr) instead of 1fr as your track size.
The existing answers solve most cases. However, I ran into a case where I needed the content of the grid-cell to be overflow: visible. I solved it by absolutely positioning within a wrapper (not ideal, but the best I know), like this:
.month-grid {
display: grid;
grid-template: repeat(6, 1fr) / repeat(7, 1fr);
background: #fff;
grid-gap: 2px;
}
.day-item-wrapper {
position: relative;
}
.day-item {
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
padding: 10px;
background: rgba(0,0,0,0.1);
}
https://codepen.io/bjnsn/pen/vYYVPZv
I have two full width rows. The first one has top and bottom padding so that it has a height. The other must be full height minus the height of the first one.
I used CSS grid layout (even if I have only one column, I don't want to make use of the flexbox layout, for personal reasons):
.main {
display: grid;
grid-template-rows: auto 1fr;
grid-template-columns: 100%;
grid-template-areas:
'menu'
'section_1';
}
.menu {
grid-area: menu;
}
.section_1 {
grid-area: section_1;
}
I would want to replace 1fr by something that would allow my second row, named section_1, to be full height minus menu's height without using calc(100vh - <menu_height>). Is it possible? I've tried 1fr but it doesn't work for this purpose of course.
to complete comment untill clarification:
fr means fraction , so you need to set the ratio of fractions of both elements, js will help you here ... but if you don't set any row template it will do without. Can you clarify the use with a snippet with enough code to demonstrate your issue . If you set an height on .main, it will also do the job
are you looking for this :
body {
margin: 0;
}
main {
height: 100vh;/* missing in your code */
display: grid;
grid-template-rows: auto 1fr;
grid-template-columns: 100%;
grid-template-areas: 'menu' 'section_1';
}
nav {
grid-area: menu;
}
section {
background: #bee;
grid-area: section_1;
}
<main>
<nav> a nav</nav>
<section> a section </section>
</main>
I have a CSS grid layout where I have the top row spanning the entire grid using the grid-column property.
Does anyone know how I can set this row to be 100px high and have all the subsequent rows set to a grid-auto-rows height of 200px?
I know I can input individual values with grid-template-rows for all the specific rows, but there are going to be a lot of divs on the page and don't want to input 100px and then a load of 200px values using this grid-template-rows property.
I'm thinking there must be a way to set individual pixel values on certain rows and then have everything else as grid-auto-rows?
I can't seem to find how to do this in the docs.
Any help would be awesome!
https://codepen.io/emilychews/pen/dZPJGQ
.gridwrapper {
display: grid;
grid-template-columns: repeat(2, 2fr);
grid-auto-rows: 200px;
grid-template-rows: 100px 200px;
grid-gap: 10px;
}
nav {
background: yellow;
}
.gridwrapper div {
padding: 1em;
background: red;
color: white;
box-sizing: border-box;
}
.gridwrapper div:nth-child(odd) {
background: blue;
}
nav {
grid-column: 1 / -1;
}
/*MAKE DIVS 1FR ON MOBILE*/
#media only screen and (max-width: 736px) {
.gridwrapper {
grid-template-columns: 1fr;
}
}
<div class="gridwrapper">
<nav class="grid">1</nav>
<div class="grid">2</div>
<div class="grid">3</div>
<div class="grid">4</div>
<div class="grid">5</div>
<div class="grid">6</div>
</div>
Does anyone know how I can set this row to be 100px high and have all the subsequent rows set to a grid-auto-rows height of 200px?
Yes. Grid can do this cleanly and easily.
First, you don't need to set a height value on the grid item itself. That overrides one of the great benefits of CSS Grid: the ability to control the dimensions of grid items at the container level.
You know that your grid will always have at least one row: the top row. That's your explicit grid.
You don't know how many additional rows there will be. That number is variable and unpredictable. That's your implicit grid.
The grid-template-rows and grid-template-columns properties set track sizes in the explicit grid.
The grid-auto-rows and grid-auto-columns properties set track sizes in the implicit grid.
Therefore, this is probably what you're looking for:
.gridwrapper{
display: grid;
grid-template-rows: 100px; /* top row is 100px in height */
grid-auto-rows: 200px; /* any new rows created are 200px in height */
}
revised codepen
You can use grid-template-rows property.
I guess you are looking for something like this:
https://codepen.io/harora/pen/EbaQxr
Here is a nice reference to the Grid model:
Grid Model Explained
Take the following grid:
.grid {
display: grid;
grid-template-columns: [one] minmax(auto, 1fr) [two] minmax(auto, 1fr);
}
.item {
grid-column: one;
min-width: 3rem;
}
<div class="grid">
<div class="item">Item</div>
</div>
The item behaves as expected: both columns fill the grid, but as the parent gets smaller, the first column does not get any smaller than the min-width of the item, 3rem, while the second column continues to collapse to 0. All good.
Now consider
.item {
grid-column: one / span 2;
min-width: 3rem;
}
Now all columns collapse to 0. Effectively, 'auto' means 0 in this case (which is not clear from the spec, where it talks about auto as a maximum but not as a minimum).
Here is a codepen that illustrates this behaviour. See how the grey box escapes it's container at smaller screen sizes!
https://codepen.io/anon/pen/RJbMVB?editors=1100
Is there any way to get the grid to respect the min-width of an item spanning multiple columns, whilst still allowing them to expand with the available free space when the grid is larger? Or is the current behaviour to spec (and if so, where does it say so)?
Ok, well it appears the answer is: No.
Did you try
minmax(min-content, 1fr)
// OR
minmax(max-content, 1fr)