Tillitsdone
down Scroll to discover

CSS grid-row-start Mastering Grid Layouts

Learn how to use the CSS grid-row-start property to control the starting position of grid items.

Explore available options like auto, span, and custom identifiers.
thumbnail

Introduction

The grid-row-start property in CSS allows you to specify the starting position of a grid item within a grid row. This property is part of the CSS Grid Layout Module, which offers advanced control over the layout and alignment of elements within a grid container. Understanding and effectively using grid-row-start can simplify your web development process and make it easier to manage complex layouts, improving the overall user experience.

Specification

The grid-row-start property is defined in the CSS Grid Layout Module Level 2. This module provides a comprehensive system for creating complex grid layouts on web pages. The grid-row-start property allows you to specify the starting position of a grid item within a grid row, contributing to precise and flexible layout designs.

For more detailed information, refer to the official CSS Grid Layout Module Level 2 specification.

Description

The grid-row-start property in CSS is used to specify the starting position of a grid item within a grid row. This property plays a crucial role in defining the layout of grid items by determining the inline start edge of their grid area. By setting grid-row-start, you can precisely control where a grid item begins vertically within the grid container.

This property accepts various values, including keywords, integers, and custom identifiers, which provide flexibility in positioning grid items. For instance, you can use the auto keyword to allow automatic placement, specify a particular row line using an integer, or use a custom identifier to refer to named grid lines.

Additionally, the grid-row-start property supports the span keyword, which allows an item to span multiple rows. This feature is particularly useful for creating complex layouts where items need to occupy more than one row.

Understanding and effectively using the grid-row-start property can significantly enhance the design and functionality of web pages. It enables you to create responsive and adaptive grid layouts that are both visually appealing and user-friendly.

Syntax

The grid-row-start property in CSS is used to define the starting position of a grid item within a grid row. The syntax for this property is straightforward and allows for a variety of values that provide flexibility in positioning grid items. Here is the basic syntax:

grid-row-start: auto | span | <integer> | <custom-ident> | initial | inherit;

Keywords and Values

  1. auto: Indicates that the property contributes nothing to the grid item’s placement, allowing for automatic positioning.
  2. span: Specifies the number of rows the item will span. It is followed by an integer value. For example, span 2 means the item will span two rows.
  3. <integer>: Specifies the row line on which the item starts. Positive integers count from the start, while negative integers count from the end.
  4. <custom-ident>: Refers to a named grid line. If there is a named line with the name <custom-ident>-start, it contributes the first such line to the grid item’s placement. If no such named line exists, it is treated as if the integer 1 had been specified.
  5. initial: Sets the property to its default value, which is auto.
  6. inherit: Allows the grid item to inherit the grid-row-start value from its parent element.

Examples

Here are some examples to illustrate the syntax:

/* Keyword value */
grid-row-start: auto;
/* Custom identifier value */
grid-row-start: header-start;
/* Integer value */
grid-row-start: 2;
/* Integer and custom identifier value */
grid-row-start: 2 header-start;
/* Span value */
grid-row-start: span 3;
/* Global values */
grid-row-start: initial;
grid-row-start: inherit;

Explanation

  • grid-row-start: auto;: The item will be placed automatically according to the grid’s default flow.
  • grid-row-start: header-start;: The item will start at the named line header-start.
  • grid-row-start: 2;: The item will start at the second row line.
  • grid-row-start: 2 header-start;: The item will start at the second named line header-start.
  • grid-row-start: span 3;: The item will span three rows from its starting position.
  • grid-row-start: initial;: The item will use the default value, which is auto.
  • grid-row-start: inherit;: The item will inherit the grid-row-start value from its parent.

Values

The grid-row-start property in CSS accepts a variety of values, each serving a specific purpose in defining the starting position of a grid item within a grid row. These values provide flexibility and precision, allowing you to create complex and adaptive grid layouts. Below are the different types of values that can be used with grid-row-start:

auto

The auto keyword is used to indicate that the property does not contribute to the grid item’s placement. This means the item will be placed automatically according to the grid’s default flow, typically starting at the first available row line.

grid-row-start: auto;

<custom-ident>

The <custom-ident> value refers to a named grid line. If there is a named line with the name <custom-ident>-start, it contributes the first such line to the grid item’s placement. If no such named line exists, it is treated as if the integer 1 had been specified.

grid-row-start: header-start;

Note: Named grid areas automatically generate implicit named lines of this form, so specifying grid-row-start: foo; will choose the start edge of that named grid area (unless another line named foo-start was explicitly specified before it).

<integer> && <custom-ident>?

This value specifies the nth grid line to the grid item’s placement. If a negative integer is given, it instead counts in reverse, starting from the end edge of the explicit grid.

If a name is given as a <custom-ident>, only lines with that name are counted. If not enough lines with that name exist, all implicit grid lines are assumed to have that name for the purpose of finding this position.

An <integer> value of 0 is invalid.

grid-row-start: 2;
grid-row-start: header-start 4;

span && [ <integer> || <custom-ident> ]

This value contributes a grid span to the grid item’s placement, such that the row start edge of the grid item’s grid area is n lines from the end edge.

If a name is given as a <custom-ident>, only lines with that name are counted. If not enough lines with that name exist, all implicit grid lines on the side of the explicit grid, corresponding to the search direction, are assumed to have that name for the purpose of counting this span.

If the <integer> is omitted, it defaults to 1. Negative integers or 0 are invalid.

The <custom-ident> cannot take the span value.

grid-row-start: span 3;
grid-row-start: span header-start;
grid-row-start: 5 header-start span;

Global Values

In addition to the specific values, grid-row-start also accepts global CSS values:

  • initial: Sets the property to its default value, which is auto.
  • inherit: The grid item inherits the grid-row-start value from its parent element.
  • revert: Resets the property to its inherited value, if it has one.
  • revert-layer: Resets the property to its value in the previous cascade layer.
  • unset: Resets the property to its inherited value if it inherits, or to its initial value if not.
grid-row-start: initial;
grid-row-start: inherit;
grid-row-start: revert;
grid-row-start: revert-layer;
grid-row-start: unset;

Formal Definition

The grid-row-start property in CSS is formally defined to specify the starting position of a grid item within a grid row. This property is part of the CSS Grid Layout Module, which provides advanced control over the layout and alignment of elements within a grid container.

Initial Value

The initial value of the grid-row-start property is auto. This value indicates that the property does not contribute to the grid item’s placement, allowing the item to be placed automatically according to the grid’s default flow.

Applies To

The grid-row-start property applies to grid items and absolutely-positioned boxes whose containing block is a grid container. This means that it can be used to control the positioning of elements that are part of a grid layout.

Inherited

The grid-row-start property is not inherited. This means that a grid item will not automatically take on the grid-row-start value of its parent element unless explicitly specified using the inherit keyword.

Computed Value

The computed value of the grid-row-start property is as specified. This means that the browser will use the exact value provided by the developer when computing the layout of the grid item.

Animation Type

The animation type for the grid-row-start property is discrete. This means that changes to the grid-row-start value will not be smoothly animated but will instead occur in discrete steps.

Formal Syntax

The formal syntax for the grid-row-start property is as follows:

grid-row-start = <grid-line>
<grid-line> = auto | <custom-ident> | [ <integer> [ <custom-ident> ]? ] | span [ <integer> | <custom-ident> ]

Explanation

  • auto: Indicates that the property contributes nothing to the grid item’s placement, allowing for automatic positioning.
  • <custom-ident>: Refers to a named grid line. If there is a named line with the name <custom-ident>-start, it contributes the first such line to the grid item’s placement. If no such named line exists, it is treated as if the integer 1 had been specified.
  • <integer>: Specifies the row line on which the item starts. Positive integers count from the start, while negative integers count from the end.
  • <integer> && <custom-ident>?: Specifies the nth grid line to the grid item’s placement. If a name is given as a <custom-ident>, only lines with that name are counted. If not enough lines with that name exist, all implicit grid lines are assumed to have that name for the purpose of finding this position.
  • span && [ <integer> || <custom-ident> ]: Contributes a grid span to the grid item’s placement, such that the row start edge of the grid item’s grid area is n lines from the end edge. If a name is given as a <custom-ident>, only lines with that name are counted. If not enough lines with that name exist, all implicit grid lines on the side of the explicit grid, corresponding to the search direction, are assumed to have that name for the purpose of counting this span. If the <integer> is omitted, it defaults to 1. Negative integers or 0 are invalid.

Examples

Setting Row Start for a Grid Item

HTML

<div class="grid-container">
<div class="grid-item">Item 1</div>
<div class="grid-item">Item 2</div>
<div class="grid-item">Item 3</div>
</div>

CSS

.grid-container {
display: grid;
grid-template-rows: repeat(3, 100px);
grid-template-columns: repeat(3, 100px);
}
.grid-item:nth-child(1) {
grid-row-start: 1;
}
.grid-item:nth-child(2) {
grid-row-start: 2;
}
.grid-item:nth-child(3) {
grid-row-start: 3;
}

In this example, we have a grid container with three grid items. Each grid item is positioned in a different row using the grid-row-start property.

Using the auto Keyword

HTML

<div class="grid-container">
<div class="grid-item">Item 1</div>
<div class="grid-item">Item 2</div>
<div class="grid-item">Item 3</div>
</div>

CSS

.grid-container {
display: grid;
grid-template-rows: repeat(3, 100px);
grid-template-columns: repeat(3, 100px);
}
.grid-item {
grid-row-start: auto;
}

In this example, all grid items are positioned automatically using the auto keyword.

Using the span Keyword

HTML

<div class="grid-container">
<div class="grid-item">Item 1</div>
<div class="grid-item">Item 2</div>
<div class="grid-item">Item 3</div>
</div>

CSS

.grid-container {
display: grid;
grid-template-rows: repeat(3, 100px);
grid-template-columns: repeat(3, 100px);
}
.grid-item:nth-child(1) {
grid-row-start: span 2;
}
.grid-item:nth-child(2) {
grid-row-start: span 3;
}

In this example, the first grid item spans two rows, and the second grid item spans three rows using the span keyword.

Using Custom Identifiers

HTML

<div class="grid-container">
<div class="grid-item">Item 1</div>
<div class="grid-item">Item 2</div>
<div class="grid-item">Item 3</div>
</div>

CSS

.grid-container {
display: grid;
grid-template-rows: [header-start] 100px [main-start] 100px [footer-start] 100px;
grid-template-columns: repeat(3, 100px);
}
.grid-item:nth-child(1) {
grid-row-start: header-start;
}
.grid-item:nth-child(2) {
grid-row-start: main-start;
}
.grid-item:nth-child(3) {
grid-row-start: footer-start;
}

In this example, we use custom identifiers to name the grid lines. Each grid item is positioned at a specific named line using the grid-row-start property.

Using the initial Keyword

HTML

<div class="grid-container">
<div class="grid-item">Item 1</div>
<div class="grid-item">Item 2</div>
<div class="grid-item">Item 3</div>
</div>

CSS

.grid-container {
display: grid;
grid-template-rows: repeat(3, 100px);
grid-template-columns: repeat(3, 100px);
}
.grid-item:nth-child(3) {
grid-row-start: initial;
}

In this example, the third grid item uses grid-row-start: initial; to reset the property to its default value, which is auto.

Using the inherit Keyword

HTML

<div class="grid-container">
<div class="grid-item">Item 1</div>
<div class="grid-item">Item 2</div>
<div class="grid-item">Item 3</div>
</div>

CSS

.grid-container {
display: grid;
grid-template-rows: repeat(3, 100px);
grid-template-columns: repeat(3, 100px);
}
.grid-item:nth-child(3) {
grid-row-start: inherit;
}

In this example, the third grid item uses grid-row-start: inherit; to inherit the grid-row-start value from its parent element.

Additional Example

HTML

<div class="wrapper">
<div class="box1">One</div>
<div class="box2">Two</div>
<div class="box3">Three</div>
<div class="box4">Four</div>
<div class="box5">Five</div>
</div>

CSS

.wrapper {
display: grid;
grid-template-columns: repeat(3, 1fr);
grid-auto-rows: 100px;
}
.box1 {
grid-column-start: 1;
grid-column-end: 4;
grid-row-start: 1;
grid-row-end: 3;
}
.box2 {
grid-column-start: 1;
grid-row-start: 3;
grid-row-end: 5;
}

Additional Styling

* {
box-sizing: border-box;
}
.wrapper {
border: 2px solid #f76707;
border-radius: 5px;
background-color: #fff4e6;
}
.wrapper > div {
border: 2px solid #ffa94d;
border-radius: 5px;
background-color: #ffd8a8;
padding: 1em;
color: #d9480f;
}
.nested {
border: 2px solid #ffec99;
border-radius: 5px;
background-color: #fff9db;
padding: 1em;
}

In this example, .box1 spans from row 1 to row 3, and .box2 starts at row 3 and ends at row 5. This shows how grid-row-start can control the vertical positioning of grid items.

Browser Compatibility

The grid-row-start property is widely supported across modern web browsers. Here is an overview of the compatibility:

  • Chrome: Supported since version 57.
  • Firefox: Supported since version 52.
  • Safari: Supported since version 10.1.
  • Edge: Supported since version 16.
  • Opera: Supported since version 44.

For detailed and up-to-date compatibility information, refer to the Can I use website.

By ensuring that your use of the grid-row-start property is compatible with the browsers your users are likely to use, you can create consistent and reliable grid layouts that enhance the overall user experience.

Related Properties

In addition to the grid-row-start property, there are several related properties that you can use to control the layout and alignment of grid items within a grid container. These properties include:

  • grid-row-end: Specifies the ending position of a grid item within a grid row.
  • grid-column-start: Specifies the starting position of a grid item within a grid column.
  • grid-column-end: Specifies the ending position of a grid item within a grid column.
  • grid-row: A shorthand property for setting both grid-row-start and grid-row-end.
  • grid-column: A shorthand property for setting both grid-column-start and grid-column-end.
  • grid-area: A shorthand property for setting all four grid line properties (grid-row-start, grid-row-end, grid-column-start, and grid-column-end).

By using these related properties in combination with grid-row-start, you can achieve even more control over the positioning and alignment of grid items, creating complex and adaptive grid layouts that enhance the design and functionality of your web pages.

Conclusion

The grid-row-start property in CSS is a powerful tool for web developers and designers. It allows you to specify the starting position of a grid item within a grid row, enabling the creation of flexible and responsive grid layouts. By understanding the syntax, values, and examples of the grid-row-start property, you can effectively use it to enhance the design and functionality of your web pages.

With wide browser compatibility and a range of related properties, the grid-row-start property offers advanced control over the layout and alignment of grid items within a grid container. Whether you are creating simple or complex grid layouts, the grid-row-start property provides the precision and flexibility needed to achieve visually appealing and user-friendly designs.

By mastering the use of the grid-row-start property, you can streamline your web development processes and create grid layouts that are both functional and visually appealing. So, go ahead and start experimenting with grid-row-start in your next web project!

See Also

To further enhance your understanding and usage of the grid-row-start property, you might find the following related properties and resources helpful:

Related CSS Properties

  1. grid-row-end: Specifies the end position of a grid item within a grid row.
  2. grid-row: Shorthand property to set both start and end positions of a grid item within a grid row.
  3. grid-column-start: Specifies the starting position of a grid item within a grid column.
  4. grid-column-end: Specifies the end position of a grid item within a grid column.
  5. grid-column: Shorthand property to set both start and end positions of a grid item within a grid column.
  6. grid-template-rows: Defines the line names and track sizing functions of the grid rows.

Additional Resources

  1. CSS Grid Layout Module: Official specification for CSS Grid Layout. CSS Grid Layout Module Level 2
  2. MDN Web Docs: Comprehensive documentation on CSS Grid Layout. MDN CSS Grid Layout
  3. Can I Use: Tool to check browser compatibility. Can I Use grid-row-start
  4. Grid by Example: Collection of examples and tutorials on CSS Grid Layout. Grid by Example
  5. CSS Tricks: Articles and guides on CSS Grid Layout. CSS Tricks Grid Guide

By exploring these related properties and resources, you can gain a deeper understanding of CSS Grid Layout and how to effectively use the grid-row-start property to create complex and responsive grid layouts in your web projects.

FAQs

What does the grid-row-start property do in CSS?

The grid-row-start property tells a grid item where to start vertically in a grid container.

How do I position a grid item at a specific row line?

You can use grid-row-start to place an item at a specific row line, like this: grid-row-start: 2;.

What’s the difference between grid-row-start and grid-row-end?

grid-row-start sets where the item begins, while grid-row-end sets where it stops. Together, they determine how many rows the item spans.

Can grid-row-start be used with named grid lines?

Yes, you can use grid-row-start with named grid lines. For example, if you’ve defined named grid lines using grid-template-rows, you can position an item like this: grid-row-start: header-start;.

How does grid-row-start work with grid-template-areas?

When using grid-template-areas, grid-row-start is less important because the areas already define the item’s placement. However, it can still be used for fine-tuning within those areas.

By addressing these frequently asked questions, you can better understand and utilize the grid-row-start property to create sophisticated and responsive grid layouts in your web development projects.

icons/logo-tid.svg

Talk with CEO

Ready to bring your web/app to life or boost your team with expert Thai developers?
Contact us today to discuss your needs, and let’s create tailored solutions to achieve your goals. We’re here to help at every step!
🖐️ Contact us
Let's keep in Touch
Thank you for your interest in Tillitsdone! Whether you have a question about our services, want to discuss a potential project, or simply want to say hello, we're here and ready to assist you.
We'll be right here with you every step of the way.
Contact Information
rick@tillitsdone.com+66824564755
Find All the Ways to Get in Touch with Tillitsdone - We're Just a Click, Call, or Message Away. We'll Be Right Here, Ready to Respond and Start a Conversation About Your Needs.
Address
9 Phahonyothin Rd, Khlong Nueng, Khlong Luang District, Pathum Thani, Bangkok Thailand
Visit Tillitsdone at Our Physical Location - We'd Love to Welcome You to Our Creative Space. We'll Be Right Here, Ready to Show You Around and Discuss Your Ideas in Person.
Social media
Connect with Tillitsdone on Various Social Platforms - Stay Updated and Engage with Our Latest Projects and Insights. We'll Be Right Here, Sharing Our Journey and Ready to Interact with You.
We anticipate your communication and look forward to discussing how we can contribute to your business's success.
We'll be here, prepared to commence this promising collaboration.
Frequently Asked Questions
Explore frequently asked questions about our products and services.
Whether you're curious about features, warranties, or shopping policies, we provide comprehensive answers to assist you.