Css Grid Lines: A Comprehensive Guide

Author

Posted Nov 14, 2024

Reads 1.1K

View of city skyline framed by a modern metal grid structure, showing architectural innovation.
Credit: pexels.com, View of city skyline framed by a modern metal grid structure, showing architectural innovation.

Css Grid Lines are a fundamental concept in CSS Grid, allowing you to create complex layouts with ease.

Grid lines are the invisible boundaries that define the grid's structure, and they can be named for reference.

These named grid lines can be used to position elements precisely within the grid, making it easier to create responsive and flexible layouts.

You can create grid lines by using the grid-template-columns and grid-template-rows properties, which define the number and size of the grid tracks.

Defining the Structure

You can define the grid template columns and rows using the grid-template-columns and grid-template-rows properties.

These properties accept values like percentages, fixed pixel values, or the fr unit for flexible sizing based on available space.

To create a grid that adapts to different screen sizes, use the fr unit in your grid-template-columns and grid-template-rows properties.

The fr unit allows the grid to distribute the available space evenly among its rows or columns.

Grid Properties

Credit: youtube.com, Learn CSS Grid - A 13 Minute Deep Dive

Grid properties are used to specify how browsers should layout a specified item within the grid box model. To make a block-level grid container, you need to use the display: grid; property.

A grid item's properties can be specified using grid-column and grid-row. These properties determine the layout of a grid item within the grid container.

You can use the grid property shorthand to reset other grid properties to their initial values. This shorthand accepts six properties: grid-template-rows, grid-template-columns, grid-template-areas, grid-auto-rows, grid-auto-columns, and grid-auto-flow.

The grid-template-columns property creates column tracks in a grid layout. Each value in the property creates a column track. For example, grid-template-columns: 200px 1fr 2fr creates a three-column grid layout with a fixed width of 200px for the first column, and 1fr and 2fr for the second and third columns, respectively.

The Display Property

To create a grid container, you need to use the display property. You can set it to grid to make a block-level grid container or inline-grid to make an inline grid container.

Credit: youtube.com, Learn CSS Display Property In 4 Minutes

The display property defines the element as a grid container and establishes a new grid formatting context for its contents. This is how you create a grid system that can be used to arrange your content.

To make a block-level grid container, use the display: grid; property. This is the same property used in the image above.

You can choose between two types of grid containers: grid and inline-grid. Here's a quick rundown of the options:

  • grid – generates a block-level grid
  • inline-grid – generates an inline-level grid

Note that the ability to pass grid parameters down through nested elements has been moved to level 2 of the CSS Grid specification.

Property

Grid properties are used to define the layout and structure of a grid container. You can use the display property to make a block-level grid container or an inline grid container by setting it to grid or grid-inline respectively.

The display property can be set to grid or grid-inline to create a grid container. The image above is set to grid as the display property.

High-altitude aerial photo showing grid layout of Monopoli, Italy's residential district.
Credit: pexels.com, High-altitude aerial photo showing grid layout of Monopoli, Italy's residential district.

Grid columns are defined using the grid-template-columns property, which allows you to specify the number of columns and their widths. In our code above, the grid container is divided into three columns, where the middle column is twice as wide as the other two.

Grid columns are the vertical divisions of your grid. You define them using the grid-template-columns property. This will allow you to specify the number of columns and their widths.

Grid properties can be defined using the grid shorthand, which can be used in exactly the same way as the grid-template shorthand. The full set of grid properties that it accepts to their initial values are:

  • grid-template-rows
  • grid-template-columns
  • grid-template-areas
  • grid-auto-rows
  • grid-auto-columns
  • grid-auto-flow

The repeat function can be used to define multiple columns or rows with the same width. For example, to create 7 columns that are each 1fr wide, you can use the repeat function like this.

Grid item's properties specify how browsers should layout a specified item within the grid box model. There are two grid item's properties: grid-column and grid-row.

Shorthand: Property

Credit: youtube.com, CSS Grid Layout Tutorial: Grid Gaps (shorthand property)

The grid shorthand property is a powerful tool that allows you to set multiple grid properties in one go. It can look a bit confusing at first, but once you understand how it works, it can save you a lot of time and effort.

You can use the grid shorthand property to define the grid-template-rows, grid-template-columns, grid-template-areas, grid-auto-rows, grid-auto-columns, and grid-auto-flow properties all at once. This can be especially useful when you need to reset multiple properties to their initial values.

The grid shorthand property can be used in the same way as the grid-template shorthand, and it will reset the other grid properties that it accepts to their initial values. This includes grid-template-rows, grid-template-columns, grid-template-areas, grid-auto-rows, grid-auto-columns, and grid-auto-flow.

Here are the properties that are reset to their initial values when using the grid shorthand property:

  • grid-template-rows
  • grid-template-columns
  • grid-template-areas
  • grid-auto-rows
  • grid-auto-columns
  • grid-auto-flow

You can use the grid shorthand property to define how the implicit grid behaves, for example:

Column-Gap

Credit: youtube.com, CSS Grid - Create a Column Gap Using grid-column-gap - Free Code Camp

Column-Gap is a property that lets you set the size of the grid lines, essentially setting the width of the gutters between the columns. You can think of it like adjusting the space between the columns.

The property is specified as column-gap, and it can be set to a length value, such as 10px or 2em. The gutters are only created between the columns, not on the outer edges.

Note that the grid- prefix will be removed, and the property will be renamed to column-gap. This means that you can already use the unprefixed property in Chrome 68+, Safari 11.2 Release 50+, and Opera 54+.

Here are some examples of valid values for column-gap:

  • 10px
  • 2em

Remember to set the column-gap property to create the desired spacing between your grid columns.

The Minmax() Function

The Minmax() Function is a powerful tool in CSS Grid, allowing you to set a minimum and maximum size for a track. This can be quite useful for distributing remaining space in a grid container.

Credit: youtube.com, CSS Grid for beginners | Using the repeat and minmax function

You can use minmax() to make a track take an equal share of the space in the grid container minus gaps. Simply replace 1fr with minmax(0, 1fr) to make the minimum size of the track 0 and not the min-content size.

Grid looks at the intrinsic size of the content and distributes available space after giving the content enough room, which means you might not get tracks that each have an equal share of all space available in the grid container.

Auto-Fill & Auto-Fit Keywords

The auto-fill and auto-fit keywords are a game-changer for responsive grid layouts. They allow us to create as many tracks as will fit in our container without specifying the exact number.

You can use the repeat() function with auto-fill or auto-fit keywords to achieve this. For example, grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)).

The auto-fill keyword creates as many tracks as will fit, but leaves empty tracks if there's not enough space. The auto-fit keyword, on the other hand, collapses the empty tracks to 0 size, allowing the flexible tracks to grow and consume the space.

Credit: youtube.com, The magic of auto-fit and auto-fill (and the difference between them)

In terms of behavior, auto-fit expands grid items to fill the available space, while auto-fill keeps the available space. The choice of keyword depends on the desired behavior.

Using the auto-fill or auto-fit keyword eliminates the need for media queries in our grid layouts. This means we can create responsive layouts without having to worry about different screen sizes and devices.

Grid Layout

Grid layout is a powerful tool in CSS that allows you to create complex layouts with ease. You can turn any layout element into a grid layout by setting the Display value to grid.

Grid containers are the foundation of the grid, defining the grid area and holding the grid items. You can set a grid container with the code display: grid;. This will create a grid layout that can contain a parent element with one or more child elements.

By default, grid layout places items along the rows, but you can cause items to place into columns using grid-auto-flow: column. You'll also need to define row tracks to avoid items creating intrinsic column tracks and laying out all in one long row.

Container

Credit: youtube.com, Elementor Tutorial - Flexbox vs Grid Container Layout

A grid container is the foundation of the grid, defining the grid area and holding the grid items. You can set it with display: grid;.

To create a grid container, you can turn any layout element into a CSS grid layout by setting the Display value to grid. This element is your Grid Container.

A grid container can contain a parent element with one or more child elements. Every direct child element of your Grid container is a Grid Item.

The element on which display: grid is applied is the grid container. It’s the direct parent of all the grid items.

The HTML element which has display: grid applied creates a new grid formatting context for the direct children.

Column Layout

You can turn any layout element into a CSS grid layout by setting the Display value to grid. This element is your Grid Container.

Grid column tracks are defined using the grid-template-columns property. Each value of this property creates a column track. You can use the fr unit to create flexible columns that take up a fraction of the available space.

Credit: youtube.com, Simplify your CSS with these 3 grid layout solutions

To create a basic grid, you can define a grid with three column tracks, two row tracks, and a 10 pixel gap between the tracks. The grid-template-columns property can be used to define the width of each column track.

Here's an example of how to create a 12 column track grid with equal columns:

grid-template-columns: 1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr

Grid columns can also be defined using the grid-template-columns property in a more explicit way, using the grid-template-columns property with a list of values. For example:

grid-template-columns: 200px 1fr 2fr

The fr unit is a new flexible unit that takes up a fraction of the available space. To calculate the width of each column, you need to subtract all non-fr values and gaps from the available width.

Grid columns can also be defined using the grid-template-columns property with a list of values, where each value represents the width of a column track.

Grid column tracks can be sized explicitly using the grid-template-columns property, or implicitly using the grid-auto-columns property.

You can use the grid-auto-flow property to place items into columns instead of rows. This is useful when you want to create a column-based layout.

Credit: youtube.com, Build Layouts with CSS Grid #2 - Multi-Column Layout

Grid columns can be aligned using the justify-content property, which affects the horizontal alignment of grid items. You can also use the justify-self property to align individual grid items within their grid cells.

Grid columns can be spanned using the grid-column-end property, which allows you to specify the end line of a grid item. You can use the span keyword to specify the number of lines to span.

Grid column tracks can be sized using the grid-auto-columns property, which allows you to specify the width of implicit grid tracks. You can use the track-size value to specify the size of each track.

Grid column tracks can be sized using the grid-auto-columns property with a list of values, where each value represents the width of a track. For example:

grid-auto-columns: 100px 200px

Grid column tracks can also be sized using the grid-auto-columns property with a single value, which represents the width of all tracks. For example:

grid-auto-columns: 300px

Justify-Content

Credit: youtube.com, The justify-content and align-content properties with CSS Grid layout

Justify-Content is a property that helps you align your grid within its container. It's especially useful when your grid items are sized with non-flexible units like pixels.

If your grid items are all sized with non-flexible units, the total size of your grid might be less than the size of its container. In this case, you can use justify-content to align the grid along the inline (row) axis.

You have several options for how to align your grid, including start, end, center, stretch, space-around, space-between, and space-evenly. Each of these options provides a different way to space out your grid items.

Here are the different options for justify-content:

  • start – aligns the grid to be flush with the start edge of the grid container
  • end – aligns the grid to be flush with the end edge of the grid container
  • center – aligns the grid in the center of the grid container
  • stretch – resizes the grid items to allow the grid to fill the full width of the grid container
  • space-around – places an even amount of space between each grid item, with half-sized spaces on the far ends
  • space-between – places an even amount of space between each grid item, with no space at the far ends
  • space-evenly – places an even amount of space between each grid item, including the far ends

Grid Tracks

Grid tracks are the building blocks of a grid layout, and they can be created in two ways: implicit or explicit. Implicit tracks are created automatically by the browser, while explicit tracks are defined by the developer using the grid-template-columns and grid-template-rows properties.

Credit: youtube.com, Mastering CSS Grid: Understanding Grid Tracks & Named Lines in 5 Minutes

Implicit tracks can be sized using the grid-auto-rows and grid-auto-columns properties, which allow you to set a minimum and maximum size for each track. For example, you can create implicit rows with a minimum size of 10em and a maximum size of auto using grid-auto-rows: 10em auto.

Explicit tracks, on the other hand, are defined using the grid-template-columns and grid-template-rows properties. These properties allow you to create a grid with a specific number of columns and rows, and to define the size of each track. For example, you can create a three-column grid with the first column having a fixed width of 200px, and the second and third columns having a flexible width using grid-template-columns: 200px 1fr 2fr.

Here is a summary of the different types of grid tracks:

  • Implicit tracks: created automatically by the browser
  • Explicit tracks: defined by the developer using grid-template-columns and grid-template-rows
  • Named grid lines: can be used to name specific lines on the grid, making it easier to place items

By understanding how to create and size grid tracks, you can build complex and responsive layouts using CSS Grid.

Lines

Lines are a crucial part of a grid layout, helping you visualize where to position your items. Grid lines are invisible horizontal and vertical lines that divide the grid container into rows and columns.

Credit: youtube.com, Learn CSS Grid - A 13 Minute Deep Dive

You can use grid lines to know where to place your items within the grid container. The properties you could use are grid-row-start and grid-row-end for rows and grid-column-start and grid-column-end for columns.

Named grid lines can make it easier to place items into a layout. You can name any line on your grid by adding a name of your choosing between square brackets.

Here are some examples of how to name grid lines:

  • grid-template-columns: 1fr [main-start] 2fr [main-end]
  • grid-template-columns: [header-start] 1fr [header-end] 1fr [footer-start] 1fr [footer-end]

By naming grid lines, you can use them instead of numbers to position your items. This can make your code more readable and easier to maintain.

Implicit

Implicit grid tracks are created automatically by the grid container when there are more grid items than cells in the grid, or when a grid item is placed outside of the explicit grid.

The size of these implicit tracks can be controlled using the grid-auto-columns and grid-auto-rows properties.

You can specify the size of implicit tracks as a length, a percentage, or a fraction of the free space in the grid using the fr unit.

Credit: youtube.com, CSS GRID: Implicit vs Explicit Tracks — 5 of 25

Implicit tracks get created when you reference grid lines that don't exist, such as when you tell an item to start on a column line that hasn't been defined.

The grid algorithm will spawn new rows as-needed to ensure every child has its own grid cell, which is handy for variable-number-of-items layouts, like photo grids.

Here are some common track sizes for implicit grid tracks:

  • Length: 10em
  • Percentage: 50%
  • Fraction of free space: 1fr

By defining grid-auto-columns and grid-auto-rows, you can specify the widths of these implicit tracks, which can help create a more predictable and maintainable layout.

Implicit grid tracks can also be used to create a repeating pattern of track sizes, such as 100px and 200px wide tracks, which can be useful for creating layouts with a specific rhythm or cadence.

Sub

Subgrid is a powerful feature that allows grid items to have their own grid system, inheriting grid lines from the parent grid. This can be especially useful for creating complex layouts.

Credit: youtube.com, 6. Named Tracks - Summer Nights, a CSS Grid Tutorial

Only Firefox currently supports subgrid, so keep that in mind when planning your projects. But trust me, it's worth waiting for.

You can use display: contents; as a tool in a similar fashion to subgrid, but be aware that it's not the same thing. It's more about removing unnecessary elements from the DOM.

Subgrid works by creating a new grid system within the grid item, which can be useful for creating smaller grids within a larger grid. It's like nesting grids.

Here's an example of how you might use subgrid in a grid template:

```css

.grid-item {

display: grid;

grid-template-columns: repeat(2, 1fr);

grid-template-rows: repeat(2, 1fr);

}

```

This creates a 2x2 grid within each grid item. You can then use subgrid to create a new grid system within each grid item, like this:

```css

.grid-item {

display: grid;

grid-template-columns: repeat(2, 1fr);

grid-template-rows: repeat(2, 1fr);

grid: "header header" 1fr / "footer footer" 1fr;

}

```

This creates a new grid system within each grid item, with two rows and two columns. The "header" and "footer" areas are named grid lines, which can be used to place items within the subgrid.

Column & Row Tracks Setup

Credit: youtube.com, Learn CSS Grid - A 13 Minute Deep Dive

You can create column tracks using the grid-template-columns property. Each value of this property creates a column track.

To create a three-column grid layout, you can use the following CSS: grid-template-columns: 200px 1fr 2fr.

The fractional unit (fr) takes up x parts of the available space, and its value is calculated by subtracting all non-fr values and gaps. For example, if we have a 900px available width and three fractional units (1fr, 1fr, and 2fr), each fractional unit would be 300px wide.

You can adjust the gap size between column tracks using the column-gap property. To set both row and column gaps together, use the gap property.

To create a basic grid, you can define a grid with three column tracks, two row tracks, and a 10 pixel gap between the tracks. This can be achieved using the following CSS: grid-template-columns: 100px 1fr 2fr; grid-template-rows: 100px 300px; gap: 10px.

Here's a breakdown of the possible values for the gap property: column-gaprow-gapgap

Grid Gaps

Credit: youtube.com, CSS Grid-gap property with CSS Grid layout

Grid gaps are an essential part of CSS grid layout, and understanding how they work can help you create more effective and visually appealing layouts.

Gaps, also known as gutters or alleys, are the spaces between grid tracks. For sizing purposes, they act like regular tracks, but you can't place content into them, only span grid items across them.

The size of grid gaps can be specified using the column-gap, row-gap, grid-column-gap, or grid-row-gap properties. These properties set the width of the gutters between columns or rows.

You can think of grid gaps as the width of the spaces between the columns or rows, and they're only created between the columns or rows, not on the outer edges.

The grid-gap properties can be set to a length value, such as 10px or 20vw. Note that the unprefixed properties, column-gap and row-gap, are already supported in modern browsers like Chrome 68+, Safari 11.2 Release 50+, and Opera 54+.

Here are the CSS properties you can use to specify grid gaps:

  • column-gap
  • row-gap
  • grid-column-gap
  • grid-row-gap

These properties will be renamed in the future to simply column-gap and row-gap, but for now, you can use either the prefixed or unprefixed versions.

Grid Auto-Layout

Credit: youtube.com, Easy One-line Responsive Layouts with CSS Grid, Auto-fit and MinMax 🔥

Grid auto-layout is a powerful feature that allows you to automatically place grid items that aren't explicitly placed on the grid. This is done using the grid-auto-flow property.

There are three values you can use for grid-auto-flow: row, column, and dense. The default value is row, which tells the auto-placement algorithm to fill in each row in turn, adding new rows as necessary.

The row value is useful when you have a lot of grid items and want to fill up the available space. For example, if you define a grid with five columns and two rows, and set grid-auto-flow to row, the items you don't place will flow across the available rows.

The column value is useful when you want to fill up the columns instead of the rows. If you set grid-auto-flow to column, the items you don't place will flow down the columns.

Here are the three values for grid-auto-flow in a nutshell:

  • row: fills in each row in turn, adding new rows as necessary (default)
  • column: fills in each column in turn, adding new columns as necessary
  • dense: attempts to fill in holes earlier in the grid if smaller items come up later

Note that the dense value can cause items to appear out of order, which is bad for accessibility.

Grid Items

Credit: youtube.com, Line-Based Placement of Grid Items with CSS Grid Layout

Grid items are the direct descendants of the grid container, and they're what make up the content of the grid. They can span multiple rows and columns, and their size and location are determined by the grid container.

The grid-area property allows you to specify a grid item's size and location within the grid by combining grid-row-start, grid-column-start, grid-row-end, and grid-column-end. This property is useful for creating complex grid layouts.

Grid items can be placed using the grid-column-start, grid-column-end, grid-row-start, and grid-row-end properties. These properties allow you to specify the starting and ending lines of a grid item's grid area.

You can also use the grid-column and grid-row shorthands to set both the start and end lines of a grid item's grid area at once. This can make it easier to create grid layouts with multiple rows and columns.

Here are the properties you can use to place a grid item by line number:

  • grid-column-start
  • grid-column-end
  • grid-row-start
  • grid-row-end

The line numbering of a grid follows the writing mode and direction of the component. This means that the line numbers will change depending on whether the text flows from left to right or right to left.

Grid lines mark the start or end of a column or row track, and the count starts at 1. You can use these line numbers to place a grid item onto the grid.

Frequently Asked Questions

What is the difference between grid lines and column lines?

Grid lines are the overall layout lines, while column lines are the vertical lines that define the grid's columns. Understanding the difference between these lines is key to accurately placing grid items in your layout

Sources

  1. CSS Grid: The Complete Guide (kodaschool.com)
  2. level 2 of the CSS Grid specification. (w3.org)
  3. repeat() notation (digitalocean.com)
  4. this article by DigitalOcean (digitalocean.com)
  5. The spec has an official way (csswg.org)
  6. only supported in Firefox (mozilla.org)
  7. Caniuse (caniuse.com)
  8. How To Use CSS Grid Properties to Justify and Align Content and Items (digitalocean.com)
  9. CSS Grid Layout: Introduction (digitalocean.com)
  10. Learning CSS Grid Layout (rachelandrew.co.uk)
  11. Grid by Example (gridbyexample.com)
  12. CSS Grid Layout Module Level 1 (w3.org)
  13. grid overlay in Chrome DevTools (chrome.com)
  14. demo (codepen.io)
  15. min-content (mozilla.org)
  16. max-content (mozilla.org)
  17. grid-row-end (mozilla.org)
  18. grid-row-start (mozilla.org)
  19. grid-column-end (mozilla.org)
  20. grid-column-start (mozilla.org)
  21. grid-row (mozilla.org)
  22. grid-column (mozilla.org)
  23. grid-auto-columns (mozilla.org)
  24. grid-auto-rows (mozilla.org)
  25. grid-area (mozilla.org)
  26. grid-template-areas (mozilla.org)
  27. grid-template (mozilla.org)
  28. grid (mozilla.org)
  29. align-items (mozilla.org)
  30. justify-items (mozilla.org)
  31. align-self (mozilla.org)
  32. justify-self (mozilla.org)
  33. justify-content (mozilla.org)
  34. A comprehensive collection of grid learning material (gridbyexample.com)
  35. Creating a Grid Container (smashingmagazine.com)
  36. MDN CSS Grid Layout (mozilla.org)
  37. grid cells (mozilla.org)
  38. tracks (mozilla.org)
  39. grid-template-rows (mozilla.org)
  40. grid-template-columns (mozilla.org)
  41. Grid lines (mozilla.org)
  42. grid-row (mozilla.org)
  43. grid-column (mozilla.org)
  44. grid-template-areas (mozilla.org)
  45. cssgrid.io (cssgrid.io)
  46. learncssgrid.com (learncssgrid.com)
  47. CSS <length-percentage> value(opens in new tab) (mozilla.org)
  48. CSS for JavaScript Developers(opens in new tab) (css-for-js.dev)

Ann Predovic

Lead Writer

Ann Predovic is a seasoned writer with a passion for crafting informative and engaging content. With a keen eye for detail and a knack for research, she has established herself as a go-to expert in various fields, including technology and software. Her writing career has taken her down a path of exploring complex topics, making them accessible to a broad audience.

Love What You Read? Stay Updated!

Join our community for insights, tips, and more.