You are currently viewing CSS: Grid-Row-End – Defining Grid Item End Row

CSS: Grid-Row-End – Defining Grid Item End Row

CSS Grid Layout is a powerful system for creating complex and responsive web layouts. One of its key features is the ability to control the placement of grid items within rows using the grid-row-end property. This property specifies where a grid item should end within the grid container, providing precise control over the layout and allowing for sophisticated designs.

The grid-row-end property is particularly useful when you need to span grid items across multiple rows or position them to end at a specific row. By mastering this property, developers can create well-structured and visually appealing web pages. In this article, we will explore the grid-row-end property in detail, starting with a basic setup and moving on to practical examples demonstrating its usage.

Basic Setup

Before we dive into the details of the grid-row-end property, let’s set up a basic example to demonstrate its functionality. We’ll create a simple HTML structure with some CSS to define our grid container and items.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>CSS Grid-Row-End Example</title>

    <style>

        .grid-container {
            display: grid;
            grid-template-rows: 100px 100px 100px;
            gap: 10px;
            margin: 20px;
            padding: 20px;
            background-color: #f0f0f0;
        }

        .grid-item {
            background-color: #ccc;
            padding: 20px;
            text-align: center;
        }

    </style>

</head>
<body>

    <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 class="grid-item">Item 4</div>
        <div class="grid-item">Item 5</div>
        <div class="grid-item">Item 6</div>
    </div>

</body>
</html>

In this code, we define a .grid-container element with the display property set to grid. The grid-template-rows property creates a basic grid structure with three rows. Each .grid-item has some padding and background color to distinguish it visually. This basic setup provides a foundation for exploring the grid-row-end property.

Understanding the grid-row-end Property

The grid-row-end property in CSS specifies the ending grid line for a grid item within the rows of a grid container. It is used to control where a grid item should end, and it can be combined with the grid-row-start property to define the item’s span across multiple rows. The syntax for grid-row-end is:

element {
    grid-row-end: value;
}

Where value can be:

  • A line number (e.g., 3)
  • A named grid line (e.g., end)
  • The keyword span followed by a number to specify the number of rows to span (e.g., span 2)

By using the grid-row-end property, you can control the exact ending position of grid items within the rows, providing flexibility and precision in your layout design.

Practical Examples of grid-row-end

Let’s explore practical examples of using the grid-row-end property in different scenarios.

Placing Grid Items to End at Specific Rows

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>CSS Grid-Row-End Example</title>

    <style>

        .grid-container {
            display: grid;
            grid-template-rows: 100px 100px 100px;
            gap: 10px;
            margin: 20px;
            padding: 20px;
            background-color: #f0f0f0;
        }

        .grid-item {
            background-color: #ccc;
            padding: 20px;
            text-align: center;
        }

        .item-1 {
            grid-row-end: 2;
        }

        .item-2 {
            grid-row-end: 3;
        }

        .item-3 {
            grid-row-end: 4;
        }

    </style>

</head>
<body>

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

</body>
</html>

In this example, the grid-row-end property is used to place the first three items to end at specific row lines within the grid. The first item ends at row line 2, the second item ends at row line 3, and the third item ends at row line 4. This precise placement allows for a structured and organized layout.

Spanning Grid Items Across Multiple Rows

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>CSS Grid-Row-End Example</title>

    <style>

        .grid-container {
            display: grid;
            grid-template-rows: 100px 100px 100px;
            gap: 10px;
            margin: 20px;
            padding: 20px;
            background-color: #f0f0f0;
        }

        .grid-item {
            background-color: #ccc;
            padding: 20px;
            text-align: center;
        }

        .item-span {
            grid-row-start: 1;
            grid-row-end: span 2;
        }

    </style>

</head>
<body>

    <div class="grid-container">
        <div class="grid-item item-span">Item 1</div>
        <div class="grid-item">Item 2</div>
        <div class="grid-item">Item 3</div>
        <div class="grid-item">Item 4</div>
        <div class="grid-item">Item 5</div>
        <div class="grid-item">Item 6</div>
    </div>

</body>
</html>

In this example, the first item spans two rows using the grid-row-start property with the value 1 and the grid-row-end property with the value span 2. This means that the item will occupy two row tracks, starting from the first row. This approach is useful for creating layouts where certain items need to take up more vertical space than others.

Combining Grid-Row-End with Other Properties

The grid-row-end property can be combined with other CSS Grid properties to create more sophisticated and flexible layouts. Let’s see an example where we combine grid-row-end with grid-template-areas.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>CSS Grid-Row-End Example</title>

    <style>

        .grid-container {
            display: grid;
            grid-template-rows: 100px 100px 100px;
            grid-template-areas:
                "header header header"
                "sidebar main main"
                "footer footer footer";
            gap: 10px;
            margin: 20px;
            padding: 20px;
            background-color: #f0f0f0;
        }

        .header {
            grid-area: header;
            background-color: #ccc;
            padding: 20px;
            text-align: center;
        }

        .sidebar {
            grid-area: sidebar;
            background-color: #bbb;
            padding: 20px;
        }

        .main {
            grid-area: main;
            background-color: #aaa;
            padding: 20px;
        }

        .footer {
            grid-area: footer;
            background-color: #999;
            padding: 20px;
            text-align: center;
        }

        .extra-item {
            grid-row-start: 1;
            grid-row-end: span 2;
            background-color: #777;
            padding: 20px;
            text-align: center;
        }

    </style>

</head>
<body>

    <div class="grid-container">
        <div class="header">Header</div>
        <div class="sidebar">Sidebar</div>
        <div class="main">Main Content</div>
        <div class="footer">Footer</div>
        <div class="extra-item">Extra Item</div>
    </div>

</body>
</html>

In this example, the .grid-container class uses both grid-template-areas and grid-row-end. The grid-template-areas property defines a specific layout for the header, sidebar, main content, and footer. The .extra-item class uses the grid-row-start property to start at the first row and the grid-row-end property to span two rows. This combination allows for a flexible layout that can adapt to extra content dynamically.

Conclusion

The grid-row-end property in CSS Grid is a valuable tool for controlling the placement of grid items within rows. By using this property, developers can specify the ending grid line for grid items, span items across multiple rows, and create precise and organized layouts. The grid-row-end property simplifies the process of managing grid items, making it easier to create responsive and well-structured layouts.

Experimenting with different values for grid-row-end and combining it with other CSS Grid properties allows for the creation of sophisticated and visually engaging webpages. The examples provided in this article serve as a foundation, encouraging further exploration and creativity in using CSS Grid and the grid-row-end property to design responsive and user-friendly webpages.

Leave a Reply