Menu
×
   ❮     
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR GIT POSTGRESQL MONGODB ASP AI R GO KOTLIN SASS VUE GEN AI SCIPY CYBERSECURITY DATA SCIENCE

CSS Tutorial

CSS HOME CSS Introduction CSS Syntax CSS Selectors CSS How To CSS Comments CSS Colors CSS Backgrounds CSS Borders CSS Margins CSS Padding CSS Height/Width CSS Box Model CSS Outline CSS Text CSS Fonts CSS Icons CSS Links CSS Lists CSS Tables CSS Display CSS Max-width CSS Position CSS Z-index CSS Overflow CSS Float CSS Inline-block CSS Align CSS Combinators CSS Pseudo-classes CSS Pseudo-elements CSS Opacity CSS Navigation Bar CSS Dropdowns CSS Image Gallery CSS Image Sprites CSS Attr Selectors CSS Forms CSS Counters CSS Website Layout CSS Units CSS Specificity CSS !important CSS Math Functions

CSS Advanced

CSS Rounded Corners CSS Border Images CSS Backgrounds CSS Colors CSS Color Keywords CSS Gradients CSS Shadows CSS Text Effects CSS Web Fonts CSS 2D Transforms CSS 3D Transforms CSS Transitions CSS Animations CSS Tooltips CSS Image Styling CSS Image Centering CSS Image Filters CSS Image Shapes CSS object-fit CSS object-position CSS Masking CSS Buttons CSS Pagination CSS Multiple Columns CSS User Interface CSS Variables CSS @property CSS Box Sizing CSS Media Queries CSS MQ Examples

CSS Flexbox

Flexbox Intro Flex Container Flex Items Flex Responsive

CSS Grid

Grid Intro Grid Columns/Rows Grid Container Grid Item

CSS Responsive

RWD Intro RWD Viewport RWD Grid View RWD Media Queries RWD Images RWD Videos RWD Frameworks RWD Templates

CSS SASS

SASS Tutorial

CSS Examples

CSS Templates CSS Examples CSS Editor CSS Snippets CSS Quiz CSS Exercises CSS Website CSS Syllabus CSS Study Plan CSS Interview Prep CSS Bootcamp CSS Certificate

CSS References

CSS Reference CSS Selectors CSS Combinators CSS Pseudo-classes CSS Pseudo-elements CSS At-rules CSS Functions CSS Reference Aural CSS Web Safe Fonts CSS Animatable CSS Units CSS PX-EM Converter CSS Colors CSS Color Values CSS Default Values CSS Browser Support

CSS Grid Item


1
2
3
4
5

Try it Yourself »


Grid Items

A grid container contains one or more grid items.

By default, a container has one grid item for each column, in each row, but you can style the grid items so that they will span multiple columns and/or rows.


The grid-column-start and grid-column-end Properties

The grid-column-start property specifies where to start a grid item.

The grid-column-end property specifies where to end a grid item.

Example

Place the first grid item at column-line 1, and let it end on column-line 3:

.item1 {
  grid-column-start: 1;
  grid-column-end: 3;
}

Result:

1
2
3
4
5
6
7
8

Try it Yourself »


The grid-column Property

The grid-column property is a shorthand property for the grid-column-start and the grid-column-end properties.

To place an item, you can refer to line numbers, or use the keyword "span" to define how many columns the item will span.

Example

Place the first grid item at column-line 1, and let it span 2 columns:

.item1 {
  grid-column: 1 / span 2;
}

Result:

1
2
3
4
5
6
7
8

Try it Yourself »

Example

Make "item1" start on column 1 and end before column 4:

.item1 {
  grid-column: 1 / 4;
}

Result:

1
2
3
4
5
6
7
8

Try it Yourself »

Example

Make "item2" start on column 2 and span 2 columns:

.item2 {
  grid-column: 2 / span 2;
}

Result:

1
2
3
4
5
6
7
8

Try it Yourself »


The grid-row-start and grid-row-end Property

The grid-row-start property specifies where to start a grid item.

The grid-row-end property specifies where to end a grid item. 

Example

Place the first grid item at row line 1, and let it end on row line 3:

.item1 {
  grid-row-start: 1;
  grid-row-end: 3;
}

Result:

1
2
3
4
5
6
7
8

Try it Yourself »


The grid-row Property

The grid-row property is a shorthand property for the grid-row-start and the grid-row-end properties.

To place an item, you can refer to line numbers, or use the keyword "span" to define how many rows the item will span:

Example

Place the first grid item at row-line 1, and let it span 2 rows:

.item1 {
  grid-row: 1 / span 2;
}

Result:

1
2
3
4
5
6
7
8

Try it Yourself »

Example

Make "item1" start on row-line 1 and end before row-line 4:

.item1 {
  grid-row: 1 / 4;
}

Result:

1
2
3
4
5
6
7
8

Try it Yourself »



The grid-area Property

The grid-area property is a shorthand property for the grid-row-start, grid-column-start, grid-row-end and the grid-column-end properties.

The syntax is grid-row-start / grid-column-start / grid-row-end / grid-column-end.

Example

Make "item4" start on row-line 1 and column-line 2, and end on row-line 3 and column line 2:

.item4 {
  grid-area: 1 / 2 / 3 / 2;
}

Result:

1
2
3
4
5
6
7
8

Try it Yourself »

Example

Make "item4" start on row-line 1 and column-line 1, and span 4 rows and 1 column:

.item8 {
  grid-area: 1 / 1 / span 4 / span 1;
}

Result:

1
2
3
4
5
6
7
8

Try it Yourself »


Naming Grid Items with grid-area

The grid-area property can also be used to assign names to grid items.

The named grid items can then be referred to by the grid-template-areas property of the grid container.

Header
Menu
Main
Right
Footer

Example

Item1 gets the name "myArea" and spans all five columns in a five columns grid layout:

.item1 {
  grid-area: myArea;
}

.grid-container {
  grid-template-areas: 'myArea myArea myArea myArea myArea';
}

Result:

1
2
3
4
5
6

Try it Yourself »

Each row is defined by apostrophes (' ').

The named grid items in each row is defined inside the apostrophes, separated by a space.

Example

Let "myArea" span three columns in a five columns grid layout (period signs represent items with no name):

.item1 {
  grid-area: myArea;
}

.grid-container {
  grid-template-areas: 'myArea myArea myArea . .';
}

Result:

1
2
3
4
5
6

Try it Yourself »

Note: A period sign represents a grid item with no name.

To define two rows, define the second row inside another set of apostrophes:

Example

Let "item1" span two columns and two rows:

.item1 {
  grid-area: myArea;
}

.grid-container {
  grid-template-areas:
    'myArea myArea . . .'
    'myArea myArea . . .';
}

Result:

1
2
3
4
5
6

Try it Yourself »

Example

Name all grid items, and make a ready-to-use webpage template:

.item1 { grid-area: header; }
.item2 { grid-area: menu; }
.item3 { grid-area: main; }
.item4 { grid-area: right; }
.item5 { grid-area: footer; }

.grid-container {
  grid-template-areas:
    'header header header header header header'
    'menu main main main main right'
    'menu footer footer footer footer footer';
}

Result:

Header
Menu
Main
Right
Footer

Try it Yourself »


The Order of the Grid Items

The grid-area property can also be used to define the order of the grid items.

The first grid item in the HTML code does not have to appear as the first item in the grid.

Example

Define the order of the grid items:

/* place in row 1 column 3 */
.item1 {grid-area: 1 / 3;}

/* place in row 2 column 3 */
.item2 {grid-area: 2 / 3;}

/* place in row 1 column 1 */
.item3 {grid-area: 1 / 1;}

/* place in row 1 column 2 */
.item4 {grid-area: 1 / 2;}

/* place in row 2 column 1 */
.item5 {grid-area: 2 / 1;}

/* place in row 2 column 2 */
.item6 {grid-area: 2 / 2;}

Result:

1
2
3
4
5
6

Try it Yourself »

You can also re-arrange the order for certain screen sizes, with media queries:

Example

Re-arrange order on small devices:

@media only screen and (max-width: 500px) {
  .item1 {grid-area: 1 / span 3;}
  .item2 {grid-area: 3 / 3;}
  .item3 {grid-area: 2 / 1;}
  .item4 {grid-area: 2 / 2 / span 2;}
  .item5 {grid-area: 3 / 1;}
  .item6 {grid-area: 2 / 3;}
}

Try it Yourself »


The justify-self Property

The justify-self property is used to align the content of a grid item along the row axis.

Example

.item1 {
  justify-self: right;
}

.item6 {
  justify-self: center;
}

Result:

Item 1
Item 2
Item 3
Item 4
Item 5
Item 6

Try it Yourself »


The align-self Property

The align-self property is used to align the content of a grid item along the column axis.

Example

.item1 {
  align-self: start;
}

.item6 {
  align-self: center;
}

Result:

Item 1
Item 2
Item 3
Item 4
Item 5
Item 6

Try it Yourself »



All CSS Grid Item Properties

Property Description
align-self Aligns the content for a specific grid item along the column axis
grid-area A shorthand property for the grid-row-start, grid-column-start, grid-row-end and the grid-column-end properties
grid-column A shorthand property for the grid-column-start and the grid-column-end properties
grid-column-end Specifies where to end the grid item
grid-column-start Specifies where to start the grid item
grid-row A shorthand property for the grid-row-start and the grid-row-end properties
grid-row-end Specifies where to end the grid item
grid-row-start Specifies where to start the grid item
justify-self Aligns the content for a specific grid item along the row axis
place-self A shorthand property for the align-self and the justify-self properties

×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
[email protected]

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
[email protected]

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.

Copyright 1999-2025 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.