Html Display Grid Layout and Styling Guide

Author

Reads 683

High-altitude view showcasing a structured urban grid layout under daylight.
Credit: pexels.com, High-altitude view showcasing a structured urban grid layout under daylight.

Display grids are a game-changer for arranging content on the web. By using the HTML display grid, you can create a layout that's both flexible and visually appealing.

The grid template columns property allows you to define the number of columns and their widths. For example, grid-template-columns: 1fr 2fr 1fr; creates three equal-width columns.

A grid container can have multiple grid templates, but only one will be applied at a time. This is useful for creating responsive designs that adapt to different screen sizes.

Related reading: Html Css Grid

Grid Container

To create a grid container in HTML, you need to apply the display: grid property to an HTML element. This element becomes the grid container and creates a new grid formatting context for its direct children.

The grid container is the root element of your layout, and to make it fill its parent, you should set the display property to grid on the root element's CSS class, such as home-content.

A unique perspective: Html Class Property

Container

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

The container is the foundation of a CSS Grid layout. It's the HTML element with display: grid applied, which creates a new grid formatting context for its direct children.

To create a CSS Grid container, you simply need to set the display property of a CSS selector to grid. This can be done on the root element of your layout.

The root element should be set to take up the full parent, such as the body of the HTML5 document, by using the body CSS selector. This can be achieved by setting the display property of the home-content class to grid, and the body CSS selector to fill the parent.

If this caught your attention, see: Selector Html

Template

In CSS, you can name areas of the grid and place items onto those named areas, which is a great technique for seeing what your component looks like right there in the CSS.

To start, give the direct children of your grid container a name using the grid-area property. You can use any name you like, except for the keywords auto and span.

Curious to learn more? Check out: Grid Html Layout

Credit: youtube.com, Create Responsive CSS Grid Layouts with GRID WRAPPING

To define which grid cells each item will span, use the grid-template-areas property. Each row is defined within quotes, and you can leave white space on the grid by using a . or multiples with no white space between them.

Here are the rules for using grid-template-areas:

  • The value must be a complete grid with no empty cells.
  • To span tracks, repeat the name.
  • The areas created by repeating the name must be rectangular and cannot be disconnected.

If you break any of these rules, the value is treated as invalid and thrown away. This makes it straightforward to redefine the layout using media queries.

You can also use the grid-template property, which is a shorthand for grid-template-rows, grid-template-columns, and grid-template-areas. The rows are defined first, along with the value of grid-template-areas, and column sizing is added after a /.

For another approach, see: Html Table Space between Rows

Grid Properties

Grid properties are used to define the layout of a grid container. You can use the grid shorthand to reset other grid properties to their initial values.

The grid shorthand can be used to define the grid layout, and it accepts six properties: grid-template-rows, grid-template-columns, grid-template-areas, grid-auto-rows, grid-auto-columns, and grid-auto-flow.

Here are the six properties that the grid shorthand resets to their initial values:

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

Lines

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

Lines are a fundamental part of a grid. They run horizontally and vertically, and their numbering starts from 1, following the writing mode and script direction of the component.

In a grid with four columns, there will be five column lines, including the one after the last column. This is a key concept to understand when working with grids.

Here's a breakdown of the different types of lines in a grid:

  1. Column lines: These are the vertical lines that separate the columns in a grid.
  2. Row lines: These are the horizontal lines that separate the rows in a grid.

Grid lines are numbered starting from 1, which means the first column line is line 1, and the first row line is also line 1. This numbering system helps to keep track of the different lines in a grid.

Here's an interesting read: Html First

Tracks

Tracks are the spaces between two grid lines. They serve as rows and columns, and can be sized using different length units.

A track is the space between two grid lines, and can be a row track or a column track. Row tracks are between two row lines, and column tracks are between two column lines.

Credit: youtube.com, CSS Property : grid-auto-rows explained !

Grid tracks can be sized using different length units, such as pixels or percentages.

Here are some key things to know about grid tracks:

  • Row tracks can be sized using a length unit, or they can be auto-sized.
  • Column tracks can be sized using a length unit, and each track can use a different length unit.
  • Tracks are auto-sized by default, which means they can be as big as the content.

By understanding how to size and use grid tracks, you can create complex and flexible grid layouts for your web pages.

Gaps

Gaps are an essential part of grid layouts, and understanding how they work can make a big difference in your designs.

A gutter or alley between tracks is what we call a gap. For sizing purposes, gaps act like regular tracks.

You can't place content directly into a gap, but you can span grid items across it, which is a useful technique for creating visually appealing layouts.

Gaps can be tricky to work with, but knowing the basics can help you make the most of them.

See what others are reading: Gap Html Code

Intrinsic Sizing Keywords

Intrinsic sizing keywords are a powerful tool in grid layout design. They allow you to define the size of grid tracks using keywords like min-content, max-content, and fit-content().

Credit: youtube.com, min-content, max-content, fit-content - intrinsic sizing with CSS

The min-content keyword makes a track as small as it can be without the track content overflowing. This means that if you have a grid with three column tracks all at min-content size, they will become as narrow as the longest word in the track.

The max-content keyword has the opposite effect, making the track as wide as it needs to be to fit all the content in one long unbroken string. This can cause overflows if the content is too long.

The fit-content() function is a bit more complex. It acts like max-content at first, but once the track reaches the size you pass into the function, the content starts to wrap. So if you use fit-content(10em), the track will be less than 10em if the max-content size is less than 10em, but never larger than 10em.

Here are the intrinsic sizing keywords in a nutshell:

  • min-content: makes the track as small as possible without overflowing
  • max-content: makes the track as wide as it needs to be to fit all the content
  • fit-content(10em): makes the track less than 10em if the max-content size is less than 10em, but never larger than 10em

Auto-Fill and Auto-Fit

You can combine repeat() and auto-fill or auto-fit keywords to create a useful pattern with grid layout. This allows you to create as many tracks as will fit in the container without specifying the number of column tracks.

Credit: youtube.com, CSS Grid - Unlock the Secrets of AUTO-FILL vs AUTO-FIT

The auto-fill keyword creates as many tracks as will fit, but leaves a gap on the end until there is enough space for another track. You can see this in action in a demo where the grid changes as you change the size of the viewport.

The auto-fit keyword, on the other hand, collapses empty tracks to 0 size, allowing the flexible tracks to grow and consume the space. This means that the flexible tracks now grow to fill the space.

There is a subtle difference between auto-fill and auto-fit, but once the first track is filled, they act in exactly the same way. This means that you can use either keyword to achieve a responsive layout with no need for media queries.

Shorthand Properties

Shorthand properties can look a little confusing at first, but they're actually a convenient way to set many grid properties in one go.

You can use two shorthand properties: grid and grid-template. The grid shorthand can be used in exactly the same way as the grid-template shorthand, and it will reset the other grid properties that it accepts to their initial values.

The grid shorthand accepts a full set of properties, including grid-template-rows, grid-template-columns, grid-template-areas, grid-auto-rows, grid-auto-columns, and grid-auto-flow.

Here's a list of the grid shorthand properties:

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

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

Syntax

Credit: youtube.com, #12 - Extremely Helpful Lost Grid Properties

CSS syntax is the foundation of building a grid system. You can create a grid container by using the display property and setting it to grid.

A grid container can have a specific size for its columns, as seen in the example of making a 4 columns grid container and specifying a size for each column.

To define the number of columns in a grid container, you can use the grid-template-columns property. This property allows you to specify the size of each column.

The grid-template-columns property can take various values, such as repeat(), fr, and auto.

Grid Layout

Grid layout allows us to arrange elements on a page, according to regions created by guides.

To place items in columns, you can use grid-auto-flow: column, but you'll also need to define row tracks, or the items will create intrinsic column tracks and layout out all in one long row.

In the context of writing mode, a row always runs in the direction a sentence runs, which is related to the writing mode of the document or component.

Additional reading: Basic Layout Html

Column Layout

Credit: youtube.com, Breakpoint-Free CSS Grid Layouts

Grid layout allows us to arrange elements on a page, according to regions created by guides.

To place items in columns, you can use grid-auto-flow: column. This will cause the items to place into columns instead of rows.

You need to define row tracks otherwise the items will create intrinsic column tracks, and layout out all in one long row. This is because a row always runs in the direction a sentence runs in the writing mode of the document or component.

You can change the value of grid-auto-flow and the writing-mode property to see the effects in the next demo.

Curious to learn more? Check out: Auto Carousel Html Css

Template Areas

Grid template areas are a powerful feature of CSS Grid Layout. You can name areas of the grid and place items onto those named areas.

To start, give the direct children of your grid container a name using the grid-area property. The name can be anything you like other than the keywords auto and span.

Credit: youtube.com, Easily Structure your Layout with CSS Grid's 'grid-template-areas'

Here are the rules for using grid-template-areas:

  • The value must be a complete grid with no empty cells.
  • To span tracks, repeat the name.
  • The areas created by repeating the name must be rectangular and cannot be disconnected.

If you break any of these rules, the value is treated as invalid and thrown away. To leave white space on the grid, use a . or multiples with no white space between them.

Check this out: Html White Circle

Grid Item Placement

Grid item placement is a crucial aspect of CSS Grid Layout. A grid item is an item that is a direct child of the grid container.

To place items on the grid, you can use the properties grid-column-start, grid-column-end, grid-row-start, and grid-row-end. These properties allow you to place items from one line to another, and they have shorthands like grid-column and grid-row that let you 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

These properties are essential for placing items on the grid, and they work consistently with the writing mode and direction of the component.

Area

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

Grid areas are created by causing an item to span over multiple tracks. This is a powerful way to design and layout your grid items.

Several grid cells together make up a grid area.

Grid items can be made to span over multiple tracks by using the grid-template-areas property in CSS.

Intriguing read: Span vs Div Html

Item

A grid item is an item that's a direct child of the grid container. This is the foundation of CSS Grid Layout.

You can place items on the grid by placing them from one line to another. This is the simplest way, and it's a good starting point.

The grid is made up of numbered lines, which you can use to place items. You can access these lines at all times.

To place an item, you'll need to set the start and end lines of the grid area where it should be placed. This can be done using the following properties: grid-column-start, grid-column-end, grid-row-start, and grid-row-end.

These properties have shorthands that let you set both start and end lines at once. The shorthands are grid-column and grid-row.

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

And here are the shorthands:

  • grid-column
  • grid-row

Auto Placement

Credit: youtube.com, How CSS GRID AUTO PLACEMENT does all the work for you!

Auto Placement is a great way to get started with grid item placement. You've already seen it in action in the demos, where items are placed one per cell in the order they appear in the source.

For many layouts, this might be all you need, as it provides a quick and easy way to arrange items on the grid. This method is especially useful for simple layouts where items don't need to be arranged in a specific order.

To tweak the auto-placement layout, you can make adjustments as needed.

Take a look at this: Html Padding Order

Placing Items

Placing items on a grid is a straightforward process, but it does require some understanding of how the grid system works. A grid item is an item that is a direct child of the grid container.

To place an item on the grid, you can use the numbered lines that make up the grid. These lines are based on the writing mode and direction of the component. You can place an item from one line to another using the grid-column-start, grid-column-end, grid-row-start, and grid-row-end properties.

Explore further: Start in Html

Credit: youtube.com, 25 — CSS Grid — Positioning Items

The grid-column and grid-row properties are shorthands that allow you to set both start and end lines at once. To place an item, you simply set the start and end lines of the grid area where it should be placed.

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

And here are the shorthands:

  • grid-column
  • grid-row

Chrome DevTools can be a big help in visualizing the grid lines and checking where your item is placed.

Grid Rules

Grid Rules are essential to creating effective layouts with HTML display grid. You can't put just any element in a grid, though - it needs to be a grid container and its children need to be grid items.

Grid containers can have multiple grid columns, but each grid item can only be in one grid column at a time. This means you can't have an element spanning across multiple columns without some extra work.

To make things more predictable, you can use grid templates, which are like blueprints for your grid layout. By defining a grid template, you can ensure that your grid items are placed in the right spot every time.

Filling Gaps

Stone wall background. Texture of an old rustic wall made of mixed stones of different sizes and shapes with vegetation, branches and green leaves growing between the gaps
Credit: pexels.com, Stone wall background. Texture of an old rustic wall made of mixed stones of different sizes and shapes with vegetation, branches and green leaves growing between the gaps

Filling gaps can be a challenge in grid layout, but there's a default behavior that helps. It always progresses forward, placing items according to their order in the source.

If there's not enough space to fit an item, grid will leave a gap and move to the next track. This can result in a disconnected display from the logical order.

The dense packing mode can be enabled by giving grid-auto-flow a value of dense. This allows grid to take items later in the layout and use them to fill gaps.

Readers also liked: Html Sample Layout

Browser Support

Browser Support is crucial for smooth grid functionality. The table below shows the first browser version that fully supports the grid-template-columns property.

Keep in mind that these versions are the minimum required for full support, and it's always a good idea to check the latest browser updates for optimal performance.

Worth a look: Html Tag B

Grid Troubleshooting

Grid Troubleshooting can be a real pain, but don't worry, I've got you covered.

Credit: youtube.com, Fix Common CSS Grid Spacing Issues

First, make sure you've set the grid container's display property to 'grid' or 'inline-grid'.

If your grid items are not displaying as expected, check if you've set the grid-template-rows and grid-template-columns properties correctly.

A common mistake is not setting the grid-gap property, which can cause your grid items to overlap.

To fix this, simply add the grid-gap property to your grid container and set it to the desired value.

If you're still having issues, try using the grid-template-areas property to define specific areas for your grid items.

This can help you visualize and troubleshoot your grid layout more easily.

Grid Examples

Grid Examples are a great way to visualize how HTML display grid works.

You can create a simple grid layout using the grid-template-columns property, as seen in the example where 2fr and 1fr are used to create two columns of different sizes.

The grid-template-rows property can be used to define the number of rows in a grid, like in the example where 3 rows are defined using the grid-template-rows property.

Credit: youtube.com, Learn CSS Grid Layouts the Easy Way

In some cases, you might need to repeat a grid template, which can be achieved using the grid-template-columns or grid-template-rows property with the repeat() function, as demonstrated in the example where the grid-template-columns property is repeated three times.

To make a grid item span across multiple rows or columns, you can use the grid-row and grid-column properties, like in the example where a grid item spans two rows and two columns.

The grid-gap property can be used to add space between grid items, as shown in the example where a 10px gap is added between grid items using the grid-gap property.

Broaden your view: Html Value Property

Judith Lang

Senior Assigning Editor

Judith Lang is a seasoned Assigning Editor with a passion for curating engaging content for readers. With a keen eye for detail, she has successfully managed a wide range of article categories, from technology and software to education and career development. Judith's expertise lies in assigning and editing articles that cater to the needs of modern professionals, providing them with valuable insights and knowledge to stay ahead in their fields.

Love What You Read? Stay Updated!

Join our community for insights, tips, and more.