This solution is a follow-up post on my last year’s article “Using CSS Grid where appropriate”. The goal is to find a solution for navigation with an unknown number of items.
Creating navigation with CSS Grid is arguably not the best solution. However, if one wants to use CSS Grid, two options were suggested:
grid-auto-flow: row;
and placing each item in the grid, like this:.nav__item:nth-child(1) {
grid-area: 1 / 1 / 2 / 2;
}
.nav {
display: grid;
grid-auto-flow: row;
}
@media screen and (min-width: 320px) {
.nav {
grid-template-columns: repeat(4, auto);
grid-template-rows: repeat(2, auto);
}
}
In both examples, we are defining a strict grid — a number of columns in a row are strictly defined.
Solution 2017
I have been using CSS Grid for more than a year now, and I learned how to use its features properly along the way:
[minmax()](https://www.w3.org/TR/css-grid-1/#valdef-grid-template-columns-minmax)
function,[auto-fit](https://www.w3.org/TR/css-grid-1/#valdef-repeat-auto-fit)
keyword,[grid-auto-flow](https://www.w3.org/TR/css-grid-1/#propdef-grid-auto-flow)
property, andI have forked the previous solution and updated it with the features mentioned above. Here’s the final solution.
.nav — grid2 {
display: grid;
grid-auto-flow: dense;
grid-template-columns: repeat(auto-fit, minmax(60px, auto));
justify-content: center;}
Solution 2018
Let’s break down this piece of code.
minmax()
function defines a size as a range between minimum and maximum value. It allows defining a dynamic size of columns and rows.
We could use this property to define a minimum and a maximum width of navigation item. In our example, we are using the following minmax definition:
minmax(60px, auto)
We are saying that column should be at least 60px wide, and it should be as wide as the maximum content width. See [auto](https://www.w3.org/TR/css-grid-1/#valdef-grid-template-columns-auto)
keyword for more details.
auto-fit
should be used as a repetition number—a number used in [repeat()](https://www.w3.org/TR/css-grid-1/#funcdef-repeat)
function. It says that the grid should place as many items as possible like when items are empty (I think 🤔).
grid-auto-flow
is a property that controls how the grid algorithm for placing items works. In our example, we are using [dense](https://www.w3.org/TR/css-grid-1/#valdef-grid-auto-flow-dense)
keyword. It says that the grid should fill holes that could be left when larger grid items occur.
[justify-content](https://www.w3.org/TR/css-align-3/#propdef-justify-content)
property aligns the content of the box. We are using justify-content: center
to align the content of the items to the center.
As you could see, we haven’t used media queries. Media queries are useful and without them, and there wouldn’t be a responsive web design, but it feels so satisfying when we able to build responsive behavior without using one.
CSS Grid still may not be the best approach for navigation element, but it works. Always try using CSS Grid where appropriate, even if it solves your problem. If you are a rebel, ignore this thought and use it nevertheless — there are no rules when building web solutions as long as your users are happy. 😎
Originally published at www.silvestarbistrovic.from.hr.