You are currently viewing CSS: Justify-Self – Aligning Individual Elements in Grid Layouts

CSS: Justify-Self – Aligning Individual Elements in Grid Layouts

The beauty of CSS Grid lies in its versatility. It gives developers the ability to create complex layouts effortlessly, positioning elements precisely where they are needed. While grid items can be aligned as a whole with properties like justify-items, there’s a powerful tool for aligning individual items within their grid cells: justify-self.

The justify-self property provides pinpoint control over how each grid item aligns itself on the inline axis (horizontally). Whether you want a single item to hug the left edge, center itself perfectly, or stretch across its grid cell, justify-self has you covered. This article will guide you through the key aspects of this property, focusing on how it can be used effectively within grid layouts.

Understanding the justify-self Property

The justify-self property in CSS Grid is used to control the alignment of an individual grid item along the horizontal axis inside its grid cell. The main benefit of using justify-self is that it overrides the alignment settings for that specific item, without affecting the rest of the grid.

The syntax for justify-self is straightforward:

grid-item {
    justify-self: value;
}

Where value can be one of the following:

  • start: Aligns the item at the beginning of the grid cell.
  • end: Aligns the item at the end of the grid cell.
  • center: Aligns the item at the center of the grid cell.
  • stretch: Stretches the item to fill the grid cell’s entire width (this is the default behavior).

These values allow for precise control over individual items, helping you build responsive and clean layouts with minimal effort.

Setting Up a Basic Grid Layout

Let’s start by creating a simple grid to showcase how justify-self can affect individual items. The following HTML and CSS create a basic grid layout with three items.

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

    <style>

        .grid-container {
            display: grid;
            grid-template-columns: repeat(3, 1fr);
            grid-gap: 20px;
            padding: 20px;
        }

        .grid-item {
            background-color: lightblue;
            padding: 20px;
            text-align: center;
            border: 2px solid #333;
        }

    </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>

</body>
</html>

In this basic setup, we have a grid with three columns and three items. By default, each grid item is stretched across its grid cell.

Applying justify-self to Individual Items

Now, let’s explore how the justify-self property can change the alignment of individual grid items.

Example 1: Aligning an Item to the Start of the Cell

In this example, we align the second grid item to the start of its grid cell.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Justify-Self: Start Example</title>

    <style>

        .grid-container {
            display: grid;
            grid-template-columns: repeat(3, 1fr);
            grid-gap: 20px;
            padding: 20px;
        }

        .grid-item {
            background-color: lightblue;
            padding: 20px;
            text-align: center;
            border: 2px solid #333;
        }

        .grid-item:nth-child(2) {
            justify-self: start;
        }

    </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>

</body>
</html>

In this example, the second grid item will be aligned to the start of its grid cell. All other items will retain the default stretch alignment.

Example 2: Centering a Grid Item

This example centers the third grid item within its cell.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Justify-Self: Center Example</title>

    <style>

        .grid-container {
            display: grid;
            grid-template-columns: repeat(3, 1fr);
            grid-gap: 20px;
            padding: 20px;
        }

        .grid-item {
            background-color: lightblue;
            padding: 20px;
            text-align: center;
            border: 2px solid #333;
        }

        .grid-item:nth-child(3) {
            justify-self: 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>

</body>
</html>

Here, the third item is centered within its cell, providing a balanced and aesthetically pleasing layout.

Example 3: Aligning an Item to the End of the Cell

This example aligns the first grid item to the end of its grid cell.

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

    <style>

        .grid-container {
            display: grid;
            grid-template-columns: repeat(3, 1fr);
            grid-gap: 20px;
            padding: 20px;
        }

        .grid-item {
            background-color: lightblue;
            padding: 20px;
            text-align: center;
            border: 2px solid #333;
        }

        .grid-item:nth-child(1) {
            justify-self: end;
        }

    </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>

</body>
</html>

This rule aligns the first item to the end (right side) of its grid cell, creating a visually interesting mix of alignments across the grid.

Stretching vs. Controlling Individual Alignments

The stretch value (which is the default for grid items) can sometimes provide a more uniform look to your layout, as it makes each item take up the entire width of its cell. However, when you’re looking for more custom alignments, using justify-self for specific items allows for greater creativity and visual distinction within the grid.

For example, combining different values of justify-self within a single grid can create dynamic and unique layouts that catch the user’s eye.

In this example, different alignment values are used for each item within the grid.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Justify-Self: Mixed Alignments</title>

    <style>

        .grid-container {
            display: grid;
            grid-template-columns: repeat(3, 1fr);
            grid-gap: 20px;
            padding: 20px;
        }

        .grid-item {
            background-color: lightblue;
            padding: 20px;
            text-align: center;
            border: 2px solid #333;
        }

        .grid-item:nth-child(1) {
            justify-self: start;
        }

        .grid-item:nth-child(2) {
            justify-self: center;
        }

        .grid-item:nth-child(3) {
            justify-self: end;
        }

    </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>

</body>
</html>

By applying different alignment values to each item, you create a visually diverse grid where items appear in different positions across their respective cells.

These examples demonstrate how the justify-self property can be applied to individual grid items to achieve different alignments within their respective grid cells. Adjust the CSS rules as needed to see how justify-self affects the layout.

Why Use justify-self?

There are several reasons why you might want to use justify-self in your CSS Grid designs:

  • Granular control: It allows you to control the alignment of a single item without affecting others.
  • Responsive designs: When designing for different screen sizes, it can help reposition elements in a way that maintains readability and aesthetics.
  • Creative layouts: For unique, modern designs, justify-self can help you break away from monotonous layouts by offering varying alignments within the grid.

Conclusion

The justify-self property is a crucial tool in your CSS Grid toolkit. It provides flexibility and control over the alignment of individual grid items, allowing you to customize your layouts beyond what global alignment properties can offer. Whether you’re aiming for a neat, consistent design or a creative, dynamic layout, mastering justify-self will help you craft responsive, user-friendly designs that stand out.

With these examples and a clear understanding of how justify-self works, you’re now ready to experiment with your own grid layouts and discover the powerful possibilities of CSS Grid alignment.

Leave a Reply