dense<\/code><\/h4>\n\n\nIf specified, the auto-placement algorithm attempts to fill available space in the grid with smaller items, even if they are out of order in the markup.<\/p>\n\n\n\n
To understand better the dense<\/code> keyword value, let\u2019s establish an 8×8 grid of 50px<\/code> squares as an example:<\/p>\n\n\n\n.grid-container {\n display: grid;\n grid-template-columns: repeat(8, 50px);\n grid-template-rows: repeat(8, 50px);\n}<\/code><\/pre>\n\n\n\nThen we expand the size of some of them:<\/p>\n\n\n\n
.grid-item:is(:nth-child(3), :nth-child(5), :nth-child(8), :nth-child(12), :nth-child(17) {\n grid-column: span 3;\n grid-row: span 2;\n}<\/code><\/pre>\n\n\n\nYou can see the result in the following image:<\/p>\n\n\n\n
The grid has the default value of auto-placement which has caused some holes to appear among grid items.<\/figcaption><\/figure>\n\n\n\nAs you can see, by default, grid lays out items in the same order as they are in the HTML order. And when it comes to a space where it can\u2019t fit an item, it skips that space, leaving \u201choles\u201d in our layout.<\/p>\n\n\n\n
The dense<\/code> keyword changes this behavior by allowing the grid to ignore the HTML in a way that, when it finds an item that fits a hole, it will take the item \u2014 regardless of whether it\u2019s next in the HTML or not \u2014 and places it into that hole.<\/p>\n\n\n\n.grid-container {\n display: grid;\n grid-template-columns: repeat(8, 50px);\n grid-template-rows: repeat(8, 50px);\n grid-auto-flow: dense;\n}<\/code><\/pre>\n\n\n\n
The dense<\/code> keyword makes auto-placement algorithm to reorder items in order to back-fill holes in the layout.<\/figcaption><\/figure>\n\n\n\nTake a look at the sixth grid item, now it moved in order to fill the first hole that it can fit in.<\/p>\n\n\n\n
So by default, grid progresses forward and never looks back to check if it can fit an item in previous empty spaces. But when we declare dense<\/code>, the algorithm attempts to fill as many holes as it can, regardless of the source order.<\/p>\n\n\nrow dense<\/code><\/h4>\n\n\nSince row<\/code> is the default value, using row dense<\/code> is the same as using dense<\/code> and it has the same effect.<\/p>\n\n\ncolumn dense<\/code><\/h4>\n\n\nGrid items will be laid out by column while filling holes.<\/p>\n\n\n\n
If we change the value of grid-auto-flow<\/code> property to column dense<\/code> in the previous example, we will have the following result:<\/p>\n\n\n\n.grid-container {\n display: grid;\n grid-template-columns: repeat(8, 50px);\n grid-template-rows: repeat(8, 50px);\n grid-auto-flow: column dense;\n}<\/code><\/pre>\n\n\n\n
<\/figure>\n\n\nAccessibility concern<\/h3>\n\n\n
One thing to note when using the grid-auto-flow<\/code> property is the issue caused by dense<\/code> algorithm.<\/p>\n\n\n\nThe dense<\/code> keyword only changes the visual order of the grid items and that order might not be the same as the original document order which may cause a very bad experience for someone tabbing through the document on a keyboard or listening to a screen reader that\u2019s reads content in the same order as the HTML.<\/p>\n\n\n\n\n