A Guide to CSS Grid: Mastering Layouts With Grid Properties

Trending 1 week ago

Positioning elements connected a webpage tin beryllium very analyzable erstwhile moving pinch analyzable layouts. This is wherever nan CSS grid comes successful handy. It is simply a layout strategy designed to simplify nan process of creating analyzable layouts.

How does it thief you? Unlike accepted layout methods that only let you to position elements successful either rows aliases columns, CSS grid offers you nan champion of some worlds—a 2D attack utilizing rows and columns.

Grid Containers and Items

You tin use CSS grid properties to 2 main types of elements: genitor and children. When you group nan show spot to "grid" for a genitor element, it transforms that constituent into a grid container. Any kid constituent wrong this grid instrumentality becomes a grid item, inheriting nan applied grid properties.

Here’s really it’s represented:

A Grid instrumentality pinch 4 grid items

A grid point tin besides go a grid container. You tin now mention to nan layout arsenic a nested grid—a grid wrong a grid. The main grid instrumentality is now referred to arsenic nan outer grid, while nan item-turned-grid instrumentality becomes an soul grid.

Each of these grids operates independently of nan other, meaning that nan properties group for an soul grid do not impact nan layout of nan outer grid, and vice-versa.

Here’s what it looks like:

Grid instrumentality pinch 4 items, a nested grid instrumentality pinch 3 items.

Mastering nan narration betwixt grid containers and items is basal for building two-dimensional layouts effectively.

Keep successful mind that location are grid properties for grid containers, while others are for grid items.

Grid Lines and Tracks

Before you commencement utilizing nan CSS grid, location are 2 cardinal position you should beryllium acquainted with:

  1. Grid lines: Grid lines mention to nan horizontal and vertical lines that shape nan grid successful a CSS grid layout. They specify nan starting and ending points of rows and columns, helping to shape wherever things spell connected nan grid. These lines are for illustration nan edges of boxes; they person numbers that thief you reference nan elements erstwhile positioning. Here nan reddish dashed statement represents them:
  2. A2X3 Grid instrumentality pinch six grid items and dashed reddish lines moving on columns and rows
    Grid tracks: They are nan gaps betwixt grid lines that specify rows and columns. They are for illustration nan building blocks of nan grid layout. In nan supra image, nan colored area wrong each point represents nan grid track.

Think of grid lines and tracks arsenic nan building blocks of a grid layout, for illustration nan lines connected a expanse of chart paper. When a horizontal grid statement intersects pinch a vertical grid line, it forms a box-shaped cell. These cells enactment arsenic containers wherever you tin spot your grid items.

CSS Grid Container Properties

These are properties you tin use to nan grid instrumentality to shape nan layout and assistance successful positioning elements wrong it. As mentioned earlier, 1 of them is nan show spot group to grid. Here are more:

Grid Template

This spot defines nan size of rows and columns. You tin size these properties utilizing pixels, percentages, aliases nan fractional portion (fr). Also, you tin usage keywords for illustration auto, minmax(), and repeat() to heighten flexibility.

  • grid-template-rows: Sets statement heights.
  • grid-template-columns: Defines file widths.

Here are immoderate examples:

Using Pixels:

.grid-container {
  display: grid;
  grid-template-columns: 250px 250px 250px;
  grid-template-rows: 250px 250px;
A 2X3 Grid layout pinch six items each pinch nan aforesaid tallness and width

Using Percentages:

.grid-container {
  grid-template-columns: 25% 50% 25%;
  grid-template-rows: 50% 50%;
A 2X3 Grid layout pinch six items, point 2 and 5 nan aforesaid size while nan remainder has adjacent but different sizes.

Using fr:

.grid-container {
  grid-template-columns: 1fr 2fr 1fr;
  grid-template-rows: 1fr 2fr;
A 2X3 Grid layout six items pinch 2 of each items having adjacent sizes

Using car and minmax() Keywords:

.grid-container {
  grid-template-columns: auto minmax(150px, 1fr) auto;
  grid-template-rows: 100px auto;

Using repeat() for Consistent Sizing:

.grid-container {
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: repeat(2, 150px);

Auto Placement and Grid Template Areas

Auto Placement: Auto placement is for illustration letting nan grid determine wherever to put items. If you don't specify nonstop positions, nan grid will automatically spot items successful nan bid they look successful nan markup. This is adjuvant erstwhile you person galore items and want them to capable nan grid evenly.

Grid Template Areas: Think of grid template areas arsenic creating a layout utilizing named zones. It's for illustration naming rooms connected a level plan. You tin mention to these area names erstwhile positioning grid items. For instance:

.grid-container {
  grid-template-areas:'header header header header'
    'sidebar main main right'
    'sidebar content content right'
    'footer footer footer footer';

This layout is for illustration a grid pinch 3 columns and 4 rows. There are 2 rows for nan main contented area. The branded areas see "header," "sidebar," "content," and "footer." In nan adjacent sections, you'll study really to usage these area labels successful nan properties of each grid item.

Alignment successful CSS Grid

You tin usage alignment properties to power nan positioning of grid items wrong their grid cells. The properties are:

  • justify-items: Controls horizontal alignment of items wrong their grid cell.
    • Values: start, end, center, and stretch.
  • align-items: Controls vertical alignment of items wrong their grid cell.
    • Values: start, end, center, and stretch.
  • justify-content: Aligns nan full grid wrong nan instrumentality on nan horizontal axis.
    • Values: start, end, center, stretch, space-between, space-around, and space-evenly.
  • align-content: Aligns nan full grid wrong nan instrumentality on nan vertical axis.
    • Values: start, end, center, stretch, space-between, space-around, and space-evenly.

Here’s an example:

.grid-container {
  grid-template-columns: 1fr 1fr;
  justify-items: center;
  align-items: center;
  justify-content: space-between;
  align-content: center;
A 3X2 Grid instrumentality pinch six items, spaced betwixt columns and stacked onto each different successful a row

In this example, items will halfway some horizontally and vertically wrong their cells. Space will beryllium distributed evenly betwixt nan columns of nan full grid, and nan grid will halfway vertically successful nan container.

Grid Gap

Grid spread refers to nan abstraction betwixt rows and columns successful a grid layout. It helps create ocular separation and adds immoderate room betwixt grid items.

The grid-gap spot allows you to group nan spread betwixt rows and columns. You tin usage various units to specify it, specified arsenic pixels (px), percentages (%), em units (em), and more.

.grid-container {
  grid-gap: 20px;
A 2X3 grid layout of six items pinch gaps betwixt nan rows and columns

In this example, nan grid instrumentality has 2 columns pinch a 20-pixel spread betwixt them. This spacing visually separates nan columns and enhances nan layout.

CSS Grid Item Properties

Here are immoderate cardinal properties that power nan behaviour of individual grid items wrong a CSS grid layout, on pinch examples:

grid-row-start and grid-row-end:

  • Defines nan starting and ending statement lines for an item.
  • Values tin beryllium statement numbers, "span n," aliases "auto".
.grid-item-1 {
  grid-row-start: 2;
  grid-row-end: 4;

This codification places Grid Item 1 from nan second-row statement to nan fourth-row line.

A 4X2 grid layout of six items pinch point 1 occupying nan astir space

grid-column-start and grid-column-end:

  • Defines nan starting and ending file lines for an item.
  • Values tin beryllium statement numbers, "span n" aliases "auto".
.grid-item-2 {
  grid-column-start: 1;
  grid-column-end: 3;

This codification places Grid Item 2 from nan first file statement to nan 3rd file line.

A 4X2 grid layout of six items pinch point 1 and 2 occupying much abstraction than nan rest


  • Specifies nan size and position of a grid point wrong nan grid by referencing nan named grid lines successful grid-template-areas.
  • As mentioned earlier, predefined area names are already successful usage pinch nan grid-template-areas property. Here's an illustration of really to usage it together pinch grid-area.
.header {
  grid-area: header;
.sidebar {
  grid-area: sidebar;
.main {
  grid-area: main;
.content {
  grid-area: content;
.right {
  grid-area: right;
.footer {
  grid-area: footer;

Here’s nan result:

 header, sidebar, main, content, correct and footer


  • Aligns individual items horizontally wrong its cell.
  • Values tin beryllium start, end, center, and stretch.
.grid-item-5 {
  justify-self: center;

This codification horizontally centers nan Grid Item 5 wrong its cell.

A 2X3 grid layout of six items pinch point 5 centered successful it's compartment otherwise from others


  • Align individual items vertically wrong its cell.
  • Values tin beryllium start, end, center, and stretch.
.grid-item-1 {
  align-self: end;

This codification aligns Grid Item 1 to nan bottommost of its cell.

A 2X3 grid layout of six items pinch point 1 otherwise position from nan others

Feel free to harvester and customize these properties to create nan layout and look you want for each grid point successful your CSS Grid.

Creating Responsive Layouts Using CSS Grids

Using CSS grids for creating responsive layouts is important to guarantee your webpage adapts seamlessly to various surface sizes and devices. You tin use these methods:

  • Media Queries: You tin usage media queries to use different grid layouts depending connected surface sizes. For instance, you whitethorn request to rearrange grid items aliases set file widths for smaller screens.
  • Flexible Units: Use comparative units specified arsenic percentages and fr to alteration grid items and columns to set proportionally to disposable space.
  • minmax(): Use nan minmax() usability to specify a scope of sizes for grid columns aliases rows. It ensures that items will not look excessively mini aliases excessively ample connected different screens.

Remember to set columns and different elements for illustration gaps betwixt grid items, font sizes, and margins. It ensures a accordant and well-designed layout that useful good connected various devices.

Explore nan Possibilities of CSS Grid Layout

Embracing nan elasticity and powerfulness of CSS grid will let you to trade layouts that not only look awesome but besides accommodate seamlessly to nan demands of modern web design. So, dive into nan world of grids, research nan possibilities, and elevate your web improvement skills.

As you delve into layout systems, you whitethorn want to comparison different layout methods pinch CSS grids. You tin do truthful pinch nan CSS Flexbox module. This will thief you study to determine erstwhile moving connected a project.

Source Tutorials