CSS Grid Layout is a powerful tool for creating complex and dynamic layouts in web design. It's a two-dimensional system that allows you to place elements on a grid, making it easy to manage multiple columns and rows.
Grid containers are the foundation of CSS Grid Layout, and they can be defined using the grid-template-columns and grid-template-rows properties. This allows you to specify the number of columns and rows you want to create.
To create a grid, you need to define a grid container and its items. Grid items can be any HTML element, and they can be placed on the grid using the grid-column and grid-row properties. This gives you a lot of flexibility in how you arrange your content.
A grid can have multiple tracks, which are the spaces between grid lines. Tracks can be specified using the grid-template-columns and grid-template-rows properties, and they can be defined as a specific width or a repeat pattern.
Readers also liked: Css Grid Template Columns
CSS Grid Basics
Most browsers now support CSS Grid natively, including Chrome, Firefox, Safari, and Opera, making it a great time to start building with grid.
To get started with CSS Grid, you need to define a container element as a grid with `display: grid`. This is the foundation of your grid layout.
Setting the column and row sizes is essential, and you can do this with `grid-template-columns` and `grid-template-rows`. These properties allow you to define the size of your grid tracks.
You can place your grid items in any order you want, and they will be automatically rearranged according to your CSS rules. This is a game-changer for responsive design, as you can easily rearrange your grid with media queries.
Here are the basic steps to define a grid:
- A grid can be defined with rows and columns.
- You can choose how to size these row and column tracks or they can react to the size of the content.
- Direct children of the grid container will be automatically placed onto this grid.
- Or, you can place the items in the precise location that you want.
- Lines and areas on the grid can be named to make placement easier.
- Spare space in the grid container can be distributed between the tracks.
- Grid items can be aligned within their area.
Grid Structure
Grid Structure is all about creating a framework for your content. You can explicitly define your grid's column and row tracks using the Grid template columns (grid-template-columns) and Grid template rows (grid-template-rows) settings.
A different take: Basic Html Template with Css
Each value in grid-template-columns creates a column track, and you can mix fixed widths with flexible units like fr, which takes up a fraction of the available space. For example, grid-template-columns: 200px 1fr 2fr creates a three-column grid layout.
The flexible unit fr is calculated by subtracting non-fr values and gaps, then allocating the remaining space among the fractional units. In this example, 1fr equals 300px, and 2fr equals 600px.
Grid-template-rows works similarly, creating row tracks with explicit heights. For instance, grid-template-rows: 100px 300px defines the first two rows, with the height of any row after row 2 determined by its content by default.
Readers also liked: Different Units for Sizing Text Css
Parent Properties
The parent properties of a grid container are a crucial part of creating a grid structure. These properties determine the overall layout and design of the grid.
The display property is used to specify the display type of an element, and in the context of grids, it's used to create a grid container. The grid-template-columns property is used to define the size and number of columns in the grid, and the grid-template-rows property is used to define the size and number of rows.
For more insights, see: Add Css Property to a Predefined Class Javascript
The grid-template-areas property is used to define the areas of the grid, and the grid-template property is a shorthand for the grid-template-columns and grid-template-rows properties. The grid-column-gap and grid-row-gap properties are used to specify the gap between columns and rows, and the grid-gap property is a shorthand for these two properties.
The justify-items, align-items, and place-items properties are used to align the items within the grid, and the justify-content and align-content properties are used to align the grid itself. The place-content property is a shorthand for these two properties.
Here's a list of the parent properties:
- display
- grid-template-columns
- grid-template-rows
- grid-template-areas
- grid-template
- grid-column-gap
- grid-row-gap
- grid-gap
- justify-items
- align-items
- place-items
- justify-content
- align-content
- place-content
- grid-auto-columns
- grid-auto-rows
- grid-auto-flow
- grid
These properties work together to create a robust and flexible grid structure that can be used to design a wide range of layouts.
Template-Columns
You can define the columns of a grid using the grid-template-columns property. This property takes a space-separated list of values that represent the track size, and the space between them represents the grid line.
A value can be a length, a percentage, or a fraction of the free space in the grid using the fr unit. The fr unit allows you to set the size of a track as a fraction of the free space of the grid container.
For example, grid-template-columns: 200px 1fr 2fr creates a three-column grid layout. Column 1 has a fixed width of 200px, and columns 2 and 3 are 1fr and 2fr wide, respectively.
The fr unit is calculated by subtracting all non-fr values and gaps from the available space. So, if we have a remaining width of 900px, and three fractional units in total, 1fr would equal 300px.
Here's a breakdown of the grid-template-columns property:
- 200px: a fixed width of 200px
- 1fr: a fraction of the free space equal to 300px
- 2fr: a fraction of the free space equal to 600px
This means that column 2 is 300px wide, and column 3 is 600px wide.
Grid Layout
Grid layout is a powerful tool in CSS that allows you to create complex and responsive layouts with ease. To create a grid layout, you can turn any layout element (such as a section, container, or div) into a CSS grid layout by setting the Display value to grid.
Discover more: Create Css Selector from Webpage
The element that has display: grid applied is called the grid container and establishes a new grid formatting context for its contents. This grid container can be a block-level or inline-level grid, depending on the value of the display property.
You can define the grid column and row tracks explicitly using the grid-template-columns and grid-template-rows properties. 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 flexible widths for the second and third columns.
Container
The container is the foundation of a grid layout. It's the element on which you apply display: grid, and it's the direct parent of all the grid items.
The container is also known as the grid container, and it's the element that establishes a new grid formatting context for its contents. This means that the container's children will be laid out according to the grid rules you define.
To turn any layout element into a grid container, simply set its Display value to grid. This will create a new grid formatting context for its contents.
On a similar theme: Tailwindcss Cards
Justify-Items
Justify-Items is a property that aligns grid items along the inline (row) axis, as opposed to align-items which aligns along the block (column) axis.
It applies to all grid items inside the container and can be set to one of four values: start, end, center, or stretch. The default value is stretch, which fills the whole width of the cell.
You can use justify-items for super quick multi-directional centering, as it sets both align-items and justify-items in a single declaration.
Here are the possible values for justify-items:
- start – aligns items to be flush with the start edge of their cell
- end – aligns items to be flush with the end edge of their cell
- center – aligns items in the center of their cell
- stretch – fills the whole width of the cell
This behavior can also be set on individual grid items via the justify-self property, making it a powerful tool for controlling the layout of your grid.
Grid Gaps and Spacing
Grid gaps are the spaces between grid tracks, and they can be thought-provoking. By default, if no row-gap is specified, it's set to the same value as column-gap.
You can specify the size of these gaps using the gap property, which is a shorthand for row-gap and column-gap. This means you can set both gaps at once, making your code more concise.
The gap property is supported in modern browsers like Chrome 68+, Safari 11.2 Release 50+, and Opera 54+, and it's already supported without the grid- prefix.
Grid gaps act like regular tracks for sizing purposes, but you can't place content into them. However, you can span grid items across gaps, which can be useful for creating visually appealing layouts.
Here are the properties you can use to specify the size of grid gaps:
- gap
- row-gap
- column-gap
- grid-row-gap
- grid-column-gap
These properties take a length value, which determines the size of the gap.
Gaps
Gaps are a crucial aspect of grid layout, acting like a regular track for sizing purposes. You can't place content into a gap, but you can span grid items across it.
A gap is essentially a gutter or alley between tracks. You can think of it like a space between the columns or rows of your grid.
You can specify the size of the gap using the gap property, which is a shorthand for row-gap and column-gap. If no row-gap is specified, it's set to the same value as column-gap.
Here's a quick rundown of the properties you can use to specify the size of the gap:
- gap: a shorthand for row-gap and column-gap
- row-gap: specifies the size of the gap between rows
- column-gap: specifies the size of the gap between columns
Note that the grid- prefix is deprecated, but the unprefixed properties are already supported in Chrome 68+, Safari 11.2 Release 50+, and Opera 54+.
Implicit
The implicit grid is a game-changer for responsive design. It automatically generates additional tracks for grid items that fall outside of your explicitly defined grid.
You can define the column & row sizes of this implicit grid via the Grid auto columns (grid-auto-columns) and Grid auto rows (grid-auto-rows) settings of your grid container. This is especially useful for creating a pattern of track sizes, like 100px and 200px wide tracks.
To create all implicit rows at a minimum size of 10em and a maximum size of auto, use the grid-auto-rows property. This ensures your rows will adapt to different screen sizes while maintaining a minimum height.
Implicit tracks can also be used to create a repeating pattern of track sizes, like the 100px, 200px, 100px, and so on pattern mentioned earlier.
Discover more: Css Text Size
Grid Placement
Grid Placement is an essential aspect of CSS Grid, and it's surprisingly straightforward once you get the hang of it. The grid is based on a grid of numbered lines, making it easy to position items from one line to another.
To place items on the grid, you can use the grid-column-start, grid-column-end, grid-row-start, and grid-row-end properties. These properties have shorthands, grid-column and grid-row, which allow you to set both start and end lines at once.
For example, to place an item from column line 2 to column line 4 and from row line 1 to row line 3, you would set the Grid column to "2 / 4" and the Grid row to "1 / 3". This is the syntax for these properties: the first value specifies the starting line number, followed by a forward slash, and then the second value, which specifies the end line number.
You can also use the "span" keyword to tell the grid layout how many columns or rows the item should span. For instance, setting the grid column & Grid row setting to "span 2" would achieve the same layout as setting the Grid column to "2 / 4" and the Grid row to "1 / 3".
Expand your knowledge: Css Selector the Last 2 Child Elements
Place Items
Place items is a crucial aspect of CSS Grid Layout, and it's often used to quickly center items in both the horizontal and vertical directions. This can be achieved by using the place-items property.
The place-items property sets both the align-items and justify-items properties in a single declaration. If the second value is omitted, the first value is assigned to both properties.
You can use the place-items property to center items by setting it to "center / center".
Grid auto-placement is another useful feature that allows you to place items on the grid one per cell in the order they appear in the source. This is often enough for many layouts, but if you need more control, you can tweak the auto-placement layout.
To place items on the grid, you can use the grid-column-start, grid-column-end, grid-row-start, and grid-row-end properties. These properties allow you to specify the start and end lines of the grid area where the item should be placed.
You might like: How Do I Center Text in Css
Here are the properties you can use to place items by line number:
- grid-column-start
- grid-column-end
- grid-row-start
- grid-row-end
These properties have shorthands that allow you to set both start and end lines at once: grid-column and grid-row.
You can use the syntax "grid-column: start-line / end-line" to place an item on the grid. For example, "grid-column: 2 / 4" would place the item on the grid from column line 2 to column line 4.
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.
The grid-column and grid-row settings are available for all grid items (direct children of the grid container). The syntax is "grid-column: start-line / end-line" and "grid-row: start-line / end-line".
You can also use the "span" keyword to tell the grid layout how many columns or rows the item should span. For example, "grid-column: span 2" would place the item on the grid from column line 2 to column line 4.
Explore further: Css 2 Tabs Box
Place Content
You can set both the align-content and justify-content properties in a single declaration using the place-content shorthand property. This property is supported by all major browsers except Edge.
To place items on a grid, you can use the numbered lines that make up the grid. You can place items from one line to another, and you always have access to those numbered lines.
The properties grid-column-start, grid-column-end, grid-row-start, and grid-row-end can be used to place items by line number. These properties have shorthands, grid-column and grid-row, which allow you to set both start and end lines at once.
Here are the properties you can use to place items by line number:
- grid-column-start
- grid-column-end
- grid-row-start
- grid-row-end
You can also use the shorthands grid-column and grid-row to set both start and end lines at once. To place an item, set the start and end lines of the grid area that it should be placed into.
The line numbering follows the writing mode and direction of the component, so changing the writing mode or direction will affect the placement of items.
Frequently Asked Questions
What is the grid in CSS?
CSS grid is a two-dimensional layout system that organizes content into rows and columns, simplifying complex web layouts. It's a powerful tool for creating flexible and responsive designs.
Is CSS Grid better than flex?
CSS Grid is ideal for complex, two-dimensional layouts, while Flexbox is better suited for simpler, one-dimensional arrangements. Choose the right tool for your project's needs to achieve a more efficient and effective design.
Is CSS Grid obsolete?
No, CSS Grid is not obsolete, as it has been supported by all major browsers since 2017 and covers 97.8% of users. Its widespread adoption and excellent browser support make it a reliable choice for modern web development.
How do I view a grid in CSS?
To view a grid in CSS, navigate to the Page Inspector's Layout tab and click on the Layout view. From there, you can access the Grid section for more options.
How do I make the grid visible in CSS?
To make the grid visible, add the CSS property `grid-template-columns` to your grid container and define the column layout. This will allow the grid to be displayed on the page.
Sources
- www.w3.org/TR/css-grid-1/ (w3.org)
- "CSS Grid Layout: The Fr Unit" (digitalocean.com)
- "Grid Column Start / End - Tailwind CSS" (tailwindcss.com)
- ""grid" | Can I use... Support tables for HTML5, CSS3, etc" (caniuse.com)
- "CSS Grid Layout" (caniuse.com)
- "Chrome 57 arrives with CSS Grid Layout and API improvements | VentureBeat" (venturebeat.com)
- "The Story of CSS Grid, from Its Creators" (alistapart.com)
- "CSS Grid Layout (level 1) | Can I use... Support tables for HTML5, CSS3, etc" (caniuse.com)
- "CSS Grid – Table layout is back. Be there and be square" (google.com)
- "CSS Grid Positioning Module Level 3" (w3.org)
- "Grid Layout" (w3.org)
- "CSS Grid Layout Module Level 2" (csswg.org)
- "CSS Grid Layout Module Level 1 Publication History - W3C" (w3.org)
- CSS Grid vs Flexbox (codecoda.com)
- level 2 of the CSS Grid specification. (w3.org)
- repeat() notation (digitalocean.com)
- this article by DigitalOcean (digitalocean.com)
- The spec has an official way (csswg.org)
- needs (meyerweb.com)
- only supported in Firefox (mozilla.org)
- Caniuse (caniuse.com)
- CSS Grid: Holy Grail Layout (digitalocean.com)
- CSS Grid Layout: Introduction (digitalocean.com)
- Learning CSS Grid Layout (rachelandrew.co.uk)
- Grid by Example (gridbyexample.com)
- CSS Grid Layout Module Level 1 (w3.org)
- grid overlay in Chrome DevTools (chrome.com)
- demo (codepen.io)
- min-content (mozilla.org)
- max-content (mozilla.org)
- fit-content() (mozilla.org)
- Source (mozilla.org)
- minmax() (mozilla.org)
- repeat() (mozilla.org)
- grid-row-end (mozilla.org)
- grid-row-start (mozilla.org)
- grid-column-end (mozilla.org)
- grid-column-start (mozilla.org)
- grid-row (mozilla.org)
- grid-column (mozilla.org)
- grid-auto-columns (mozilla.org)
- grid-auto-rows (mozilla.org)
- grid-area (mozilla.org)
- grid-template-areas (mozilla.org)
- grid-template (mozilla.org)
- grid (mozilla.org)
- align-items (mozilla.org)
- justify-items (mozilla.org)
- align-self (mozilla.org)
- justify-self (mozilla.org)
- align-content (mozilla.org)
- justify-content (mozilla.org)
- A comprehensive collection of grid learning material (gridbyexample.com)
- Creating a Grid Container (smashingmagazine.com)
- MDN CSS Grid Layout (mozilla.org)
- grid cells (mozilla.org)
- tracks (mozilla.org)
- grid-template-rows (mozilla.org)
- grid-template-columns (mozilla.org)
- minmax (mozilla.org)
- repeat (mozilla.org)
- Grid lines (mozilla.org)
- grid-row (mozilla.org)
- grid-column (mozilla.org)
- grid-template-areas (mozilla.org)
- cssgrid.io (cssgrid.io)
- learncssgrid.com (learncssgrid.com)
- CSS for JavaScript Developers(opens in new tab) (css-for-js.dev)
- wide browser support(opens in new tab) (caniuse.com)
Featured Images: pexels.com