How to Make a Grid in CSS with Sample Layouts

Author

Reads 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.

Creating a grid in CSS is a fundamental skill for any web developer. To start, you'll want to use the CSS Grid Layout module, which allows you to create a two-dimensional grid system.

The grid container is the parent element that contains all the grid items. To define a grid container, you simply add the display property and set it to grid. For example, in the "Basic Grid Layout" section, we used the following code: `display: grid;`.

Grid items are the elements that make up the grid. In the "Grid Item Placement" section, we learned that grid items can be placed anywhere within the grid container using the grid-column and grid-row properties. By setting these properties, you can control where each grid item is positioned.

In the "Sample Layouts" section, we saw how to create a basic grid layout with two rows and two columns using the grid-template-rows and grid-template-columns properties. For instance, we used `grid-template-rows: 1fr 1fr;` to create two equal rows.

Basic Grid Structure

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

To create a basic grid structure in CSS, you need to define a container element as a grid with the display: grid property. Most browsers shipped native support for CSS Grid as of March 2017, including Chrome, Firefox, Safari, and Opera.

To get started, you'll need to set the column and row sizes with grid-template-columns and grid-template-rows. This will define the grid structure, allowing you to place child elements into the grid with grid-column and grid-row.

The source order of the grid items doesn't matter, which makes it easy to rearrange your grid with media queries. Imagine defining the layout of your entire page and then completely rearranging it to accommodate a different screen width with just a few lines of CSS.

You can specify columns using the grid-template-columns property, passing two or more values to define the column widths. For example, you can use percentages like 25% and 75% to slice the element into two columns.

Here are some common values you can use for grid-template-columns:

  • pixels (e.g., 200px)
  • rems (e.g., 2rem)
  • viewport units (e.g., 20vw)
  • fr unit (e.g., 1fr and 3fr)

The fr unit stands for "fraction" and allows columns to grow and shrink as required, making it a flexible and powerful tool for grid construction.

Grid Properties

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

Grid Properties are what make a grid layout flexible and customizable. You can use the grid-template-columns property to specify the size of the columns, and the grid-template-rows property to specify the size of the rows.

The grid-template-columns property can be used to specify the size of the columns, and the grid-template-rows property can be used to specify the size of the rows. The grid-template-columns property can also be used to specify how many columns are in a grid layout.

Here are some common Grid Properties:

Grid Properties can also be used to position grid items, such as using the grid-column and grid-row properties to specify where to start and end the grid item.

Explore further: Css Grid Properties

Benefits

The benefits of a CSS Grid are numerous, and one of the most significant advantages is its flexibility. It allows web designers to achieve almost any type of layout they need.

A CSS Grid can easily adapt to changes in device, space, and orientation without requiring a semantic change in the content. This makes it a great option for creating responsive designs.

A vibrant grid pattern with pastel colors and mesh overlay, perfect for artistic and abstract design use.
Credit: pexels.com, A vibrant grid pattern with pastel colors and mesh overlay, perfect for artistic and abstract design use.

The grid layout can be rearranged to fit the context without changing the underlying markup, which is a huge time-saver. You can easily fit the layout to the context without messing with the content structure.

Auto-placement is another powerful feature of CSS Grid, allowing you to easily fill up available space without complex calculations. This feature is a game-changer for designers who want to create flexible layouts.

CSS Grid also allows you to overlap items when needed, which is not possible with tables. This adds a whole new level of creativity to your design options.

For more insights, see: Css Grid Auto-fit

All Properties

Grid properties are a crucial aspect of CSS Grid Layout. An HTML element becomes a grid container when its display property is set to grid or inline-grid. All direct children of the grid container automatically become grid items.

The grid container has several properties that can be used to customize its behavior. These properties include 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, and grid.

For more insights, see: Css Grid Vertical Align

Credit: youtube.com, Master CSS Grid – Full Tutorial with All Properties Explained

Here's a breakdown of these properties:

Grid items also have several properties that can be used to customize their behavior. These properties include grid-column-start, grid-column-end, grid-row-start, grid-row-end, grid-column, grid-row, grid-area, justify-self, align-self, and place-self.

The grid-gap property is a shorthand property for the row-gap and the column-gap properties. It can also be used to set both the row gap and the column gap in one value.

Related reading: Html 3 Column Table

Columns

Columns are the vertical lines of grid items, also known as grid tracks. The space between two adjacent grid lines is called a grid track.

You can specify the size of columns using the grid-template-columns property. For example, you can create a grid with 7 columns that are each 1fr wide by using the repeat function. This is equivalent to writing out the column widths for each column line.

The grid-template-columns property can take a variety of values, including lengths, percentages, and fractions of the free space in the grid (using the fr unit).

Intriguing read: Css Grid Space between

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

Here are some examples of how to specify column sizes:

To specify a grid with 7 columns that are each 1fr wide, you can use the repeat function: grid-template-columns: repeat(7, 1fr). This is equivalent to writing out the column widths for each column line: grid-template-columns: 1fr 1fr 1fr 1fr 1fr 1fr 1fr.

Explore further: Css Grid Two Columns

Grid Layout

A grid layout consists of a parent element, with one or more child elements, as we learned from the grid elements section.

The CSS grid is designed as a flexible solution to be applied to different use cases, and it's easy to achieve layouts like the Holy Grail Layout, which has a header at the top, footer at the bottom, and content area in between.

You can use the fr unit to define each column, which represents a fraction of the available space in the grid container, ensuring that grid tracks grow and shrink according to the available space.

To style the content, you can set the grid-column-start and -end properties to 1 and 4 to span the length of the grid, and set a minimum width for the article element to take up most of the page.

A fresh viewpoint: Css Grid Fit-content

Elements

Credit: youtube.com, Learn EVERY Graphic Design Grid In 8 Minutes!

Grid layout is made up of a few key elements that work together to create a flexible and responsive design.

A grid layout consists of a parent element, with one or more child elements. This parent element is often referred to as the grid container.

The grid container is the direct parent of all the grid items, and it's the element where you apply the display: grid property.

You might enjoy: Grid Container Css

Grid Layout

Grid layout is a powerful tool for creating complex and dynamic layouts on the web. It allows you to divide your content into rows and columns, making it easy to manage and arrange your elements.

You can use grid lines to define the structure of your grid, and there are two types of grid lines: column lines and row lines. Column lines are the lines between columns, and row lines are the lines between rows.

To place a grid item at a specific column line, you can use the grid-column-start property, and to place it at a specific row line, you can use the grid-row-start property. You can also use line numbers to define the placement of your grid items.

Related reading: Css Grid Rows Auto

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

Grid areas are the total space surrounded by four grid lines. A grid area may be composed of any number of grid cells. You can give a grid item a name using the grid-area property, which can be referenced by a template created with the grid-template-areas property.

Here are some common grid area properties:

  • grid-area: gives an item a name so that it can be referenced by a template created with the grid-template-areas property
  • grid-area: can be used as an even shorter shorthand for grid-row-start + grid-column-start + grid-row-end + grid-column-end

The grid-area property is a shorthand property that can save you time by requiring less code. However, it's a bit more difficult to remember the order of property values.

You can also use the grid-template-columns and grid-template-rows properties to define the structure of your grid. These properties allow you to specify the number of columns and rows, as well as the size of each column and row.

Here are some common grid template properties:

  • grid-template-columns: defines the number and size of columns
  • grid-template-rows: defines the number and size of rows

Using grid lines and grid areas, you can create complex and dynamic layouts that are easy to manage and arrange. Whether you're building a simple layout or a complex one, grid layout is a powerful tool that can help you achieve your goals.

Sub

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

The subgrid feature of CSS grid is a game-changer for layout flexibility. It allows grid items to have a grid of their own that inherits grid lines from the parent grid.

With subgrid, you can create complex layouts that would be difficult or impossible to achieve without it. For example, you can create a layout with a header, navigation sidebar, main content, and right sidebar, all with their own grid systems.

Subgrid is not the same as display: contents, although it can be a useful tool in a similar fashion. It's essential to understand the difference between the two to use them effectively.

The CSS grid was designed to be a flexible solution for different use cases, and subgrid is a key part of that flexibility. By using subgrid, you can create layouts that adapt to different screen sizes and devices.

Related reading: Responsive Design Sizes

Grid Units and Flow

Grid units are the building blocks of a grid system, and they're measured in a unit called a "fr" (short for fraction). A single fr is equal to 1/100 of the available space.

The fr unit is flexible and can be used to create a responsive grid that adapts to different screen sizes. For example, if you have a grid with 3 columns, each column can take up an equal amount of space, which is 33.33fr.

A different take: Css Grid Extra Space

Auto-Flow

Credit: youtube.com, CSS GRID: grid-auto-flow Explained — 6 of 25

Auto-Flow is a crucial aspect of grid units, and it's essential to understand how it works. The grid-auto-flow property controls the auto-placement algorithm, which kicks in when you don't explicitly place all your grid items.

You can set grid-auto-flow to three different values: row, column, or 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 mix of large and small items, as it will fill in each row before moving on to the next one. This can be beneficial when you have a lot of content, but it's not ideal for accessibility.

The column value, on the other hand, tells the auto-placement algorithm to fill in each column in turn, adding new columns as necessary. This can be useful when you have a lot of items that need to be placed horizontally.

Expand your knowledge: Css Grid Auto Flow

Credit: youtube.com, CSS GRID: grid-auto-flow dense Block Fitting — 16 of 25

The dense value attempts to fill in holes earlier in the grid if smaller items come up later. However, this can cause items to appear out of order, which is bad for accessibility.

Here are the three values for grid-auto-flow in a table for easy reference:

It's essential to choose the right value for grid-auto-flow based on your specific needs and the type of content you're working with.

Fr Units

Fr units are a flexible way to define grid column widths. They essentially mean "portion of the remaining space".

You'll likely end up using a lot of fractional units in CSS Grid, like 1fr. Fractional units are much more friendly in combination with other units.

For example, if you have a grid declaration like "grid-template-columns: 25% 75%", it's not as flexible as a fractional unit like "grid-template-columns: 1fr 3fr". The percentage values are much more firm.

Fractional units are also more forgiving when it comes to padding. If you add padding to percentage-based columns, you've broken 100% width. But with fractional units, you can add padding without worrying about it.

Here are some key differences between auto-fill and auto-fit:

Grid Shorthand Properties

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

Grid Shorthand Properties can save you time and make your code look cleaner. By using these properties, you can define multiple grid properties in a single line of code.

The grid-column shorthand property is a great example of this. It allows you to define both the grid-column-start and grid-column-end properties in one line. To use it, simply specify the column line where the grid item starts and the column line where it ends, separated by a slash.

The grid-row shorthand property works similarly, allowing you to define both the grid-row-start and grid-row-end properties in one line. To use it, simply specify the row line where the grid item starts and the row line where it ends, separated by a slash.

The grid-area shorthand property is another powerful shorthand property that can save you even more time. It allows you to define the grid-area property with four values in one line: the row line where the grid item starts, the column line where it starts, the row line where it ends, and the column line where it ends.

Recommended read: Realtime Html Editor

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

Here's a summary of the shorthand grid properties:

  • grid-column: defines both grid-column-start and grid-column-end properties
  • grid-row: defines both grid-row-start and grid-row-end properties
  • grid-area: defines the grid-area property with four values

These shorthand properties can be a huge time-saver and make your code look cleaner. Just remember to use them correctly and you'll be well on your way to creating beautiful and effective CSS grids!

Grid Layout Examples

CSS Grid Layout Examples are a great way to visualize how to make a grid in CSS.

Rachel Andrew, a renowned web designer, has provided some excellent examples that we can learn from.

Some examples are quite simple, while others are more complex, but they all demonstrate the power of CSS Grid.

To create a basic grid layout, you can use the grid-template-columns and grid-template-rows properties, as seen in some of Rachel Andrew's examples.

Example Layouts

Let's take a look at some example layouts you can create with CSS grid. You can use the grid-column and grid-row properties to define the size and position of grid items.

Grid items can overlap each other, and you can use the z-index property to control their stacking order. This can be useful when you need to create a layout with multiple overlapping elements.

Check this out: Css Grid Align Items

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

To set up a grid layout, you need to create a parent div element (the grid container) and one or more child div elements (the grid items). The grid items are the direct children of the grid container.

Here are some common grid layouts you can create:

You can adjust the gap between rows and columns using the grid-row-gap and grid-column-gap properties, or use the grid-gap shorthand property for convenience.

By using the grid-column and grid-row properties, you can create complex layouts with ease. For example, you can use the shorthand properties to define a grid item's size and position in a single line of code.

For your interest: Css Grid Column Width

Holy Grail Layout

The Holy Grail Layout is a classic web page structure that's been around for a while. It consists of a header at the top, a footer at the bottom, and a content area in between, with two columns on either side.

This layout was difficult to achieve before CSS grid, but with it, it's a breeze. The content area is typically the main focus of the page.

Related reading: Css Grid Area

Credit: youtube.com, Build Layouts with CSS Grid #3 - Holy Grail Layout

To create this layout, you'll need to define the header, navigation sidebar, main content, right sidebar, and footer in your HTML. You can replace the navigation sidebar with another right sidebar if you prefer.

The CSS grid makes it easy to create the Holy Grail Layout. You can use the fr unit to define each column, which represents a fraction of the available space in the grid container.

Using different value types to define the track size of the rows is also a good idea. This ensures that grid tracks grow and shrink according to the available space.

To style the content, you can change the background color and padding of the header, nav, article, aside, and footer elements. Setting the grid-column-start and -end properties to 1 and 4 so they span the length of the grid is also a good idea.

Intriguing read: Css Grid Justify Content

Websites Using

The Calendly Blog is a great example of a website using CSS grids to create a grid-based navigation menu. The home page features a creative use of grid cells, where hovering over each item animates a fill change to indicate selection.

Credit: youtube.com, Grid Systems in Web & UI Design

The Calendly Blog is one of five excellent examples of CSS grids in action. The website was created to celebrate the 200th Anniversary of Montreal General Hospital.

The grid layout on the Calendly Blog allows designers to place dynamic elements in grid cells, besides images and text. This is a great way to add interactivity to a website.

The Calendly Blog is a great inspiration for designers looking to use CSS grids in their next project.

You might like: Blog Page Web Design

Grid Advanced Topics

Grid is a powerful layout system that can be used to create complex and dynamic layouts.

The grid-template-columns property is used to define the number of columns and their widths in a grid container. This property is essential for creating a grid layout.

A grid container can have multiple grid tracks, which are the areas between grid lines. To create a grid with multiple tracks, you can use the grid-template-columns property with multiple values.

Credit: youtube.com, Become A Master Grid CSS In 13 Minutes

Grid items can be positioned within a grid using the grid-column and grid-row properties. These properties allow you to specify the start and end grid lines for an item.

To create a grid with a specific number of rows, you can use the grid-template-rows property. This property is used in combination with the grid-template-columns property to create a grid with both rows and columns.

Grid areas are the spaces between grid items and can be used to create a more complex layout. To create a grid area, you need to specify the grid area name and the grid lines that define it.

The grid-gap property is used to add space between grid tracks. This property can be used to create a more visually appealing grid layout.

A unique perspective: Css Grid Lines

Grid Alignment and Placement

Grid alignment and placement are crucial aspects of creating a well-designed grid in CSS. There are several properties that can be used to align grid items, including align-items, justify-items, and align-self.

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

The align-items property aligns grid items along the block (column) axis, while justify-items aligns them along the inline (row) axis. This means that align-items is used for vertical alignment, and justify-items is used for horizontal alignment.

To align grid items, you can use the following values: start, end, center, and stretch. For example, align-items: center will center-align grid items vertically, while justify-items: start will align them to the left horizontally.

The place-items property sets both align-items and justify-items in a single declaration, making it a convenient option for quick multi-directional centering. For example, place-items: center center will center-align both vertically and horizontally.

Here is a summary of the align-items and justify-items properties:

By using these properties and values, you can create a well-aligned grid that looks great and is easy to navigate.

Lines

Lines are the backbone of a CSS grid, and understanding them is key to creating effective layouts.

A line is a dividing line that makes up the structure of the grid, and it can be either vertical (column grid line) or horizontal (row grid line).

Credit: youtube.com, Design principles: Grid systems & alignment — The Freelancer's Journey (Part 14 of 43)

Grid lines reside on either side of a row or column, and they're used to position grid items.

You can place a grid item at a specific column line, such as column line 1, and let it end on another column line, like column line 3.

Similarly, you can place a grid item at a specific row line, such as row line 1, and let it end on another row line, like row line 3.

Column lines are used to define the boundaries between columns, while row lines define the boundaries between rows.

The grid line names can be used instead of line numbers, which can make the code more readable and maintainable.

In an example, grid-template-columns and grid-template-rows properties are used to define the grid with line names, where each column is named "col1-start", "col2-start", and "col3-start", and set to 100px.

Grid lines can be used to position grid items that span multiple grid tracks, making it easier to create complex layouts.

By understanding lines, you can create more effective and flexible layouts that adapt to different screen sizes and devices.

You might enjoy: Css Grid 3 Columns

Place Content

Credit: youtube.com, Place-content vs. place-items | What's the difference to vertically & horizontally center?

Place Content is a shorthand property that sets both the align-content and justify-content properties in a single declaration. It's a convenient way to align the grid along both the block (column) and inline (row) axes.

The syntax for Place Content is straightforward: you specify two values, the first for align-content and the second for justify-content. If you only specify one value, it will be assigned to both properties. This property is supported in all major browsers except Edge.

Here's a breakdown of the possible values for Place Content:

  • align-content value / justify-content value

By using Place Content, you can simplify your CSS code and make it more efficient. For example, you can use Place Content to center the grid along both the block and inline axes with a single declaration: place-content: center center.

A unique perspective: Center Grid Css

Browser Support

Browser support is crucial for a CSS grid to work seamlessly.

Chrome supports CSS Grid from version 57 and up, which is great news for developers.

Credit: youtube.com, Internet Explorer + CSS Grid????

Firefox has been on the scene since version 52, making it a reliable choice.

IE is a bit of an outlier, with support starting from version 11, but only marked as experimental.

Edge has been supporting CSS Grid since version 16, which is a significant milestone.

Safari has been on board since version 10.1, making it a solid option for web development.

Here's a quick rundown of the browser support for CSS Grid:

Melba Kovacek

Writer

Melba Kovacek is a seasoned writer with a passion for shedding light on the complexities of modern technology. Her writing career spans a diverse range of topics, with a focus on exploring the intricacies of cloud services and their impact on users. With a keen eye for detail and a knack for simplifying complex concepts, Melba has established herself as a trusted voice in the tech journalism community.

Love What You Read? Stay Updated!

Join our community for insights, tips, and more.