CSS justify-self Property

The justify-self property aligns a grid item within its grid cell in the inline direction, which is typically left to right for English pages. It requires available space around the item in the inline direction to have an alignment effect.

Syntax:

justify-self: stretch | normal | auto | baseline | start | end | center | flex-start | 
flex-end | self-start | self-end | left | right | safe | unsafe;

Property Values:

ValueDescription
stretchDefault value. Content fills the whole width of the cell.
normalBehaves as ‘start’ in block-level layouts. Ignores in table and flex layouts. Acts as ‘stretch’ in other absolutely positioned boxes.
autoDefault value. Used if justify-items property is located in parent element or defaults to normal value.
baselineAligns the alignment baseline of the current box’s first or last baseline with corresponding baseline in shared first or last baseline set of all the boxes.
startAligns content to the left of the cell.
endAligns content to the right of the cell.
centerAligns content to the center of the cell.
flex-startSame as ‘start’ value.
flex-endSame as ‘end’ value.
self-startAligns the item to the left of the aligned container at the beginning of an item.
self-endAligns the item to the right of the aligned container at the end of an item.
leftMakes the item pack flush to the left of the alignment container.
rightMakes the item pack flush to the right of the alignment container.
safeAligns as ‘start’ if item overflows the alignment container.
unsafeAligns as the given value regardless of relative sizes of alignment container and item.

Example 1: In this example, justify-self is not used for the alignment.

HTML
<!DOCTYPE html>
<html>

<head>
    <style>
        article {
            font-family: sans-serif;
            background-color: green;
            display: grid;
            grid-template-columns: 1fr 1fr;
            grid-auto-rows: 70px;
            grid-gap: 30px;
            width: 700px;
            justify-items: stretch;
            border: solid;
            margin: 20%;
        }

        article span {
            background-color: white;
            color: green;
            margin: 2px;
            text-align: center;
            border: solid;
        }

        article,
        span {
            padding: 5px;
            border-radius: 3px;
            border: solid;
        }
    </style>
</head>

<body>
    <article class="container">
        <span>Beginner</span>
        <span>FOR</span>
        <span>Beginner</span>
        <span>ONLINE</span>
        <span>PORTAL</span>
    </article>
</body>

</html>

Output:

Example 2: In this example, a few of the justify-self values are used for the alignment.

HTML
<!DOCTYPE html>
<html>

<head>
    <style>
        article {
            font-family: sans-serif;
            background-color: green;
            display: grid;
            grid-template-columns: 1fr 1fr;
            grid-auto-rows: 70px;
            grid-gap: 30px;
            width: 700px;
            justify-items: stretch;
            border: solid;
            margin: 20%;
        }

        span:nth-child(2) {
            justify-self: start;
        }

        span:nth-child(3) {
            justify-self: center;
        }

        span:nth-child(4) {
            justify-self: end;
        }

        article span {
            background-color: white;
            color: green;
            margin: 2px;
            text-align: center;
            border: solid;
        }

        article,
        span {
            padding: 5px;
            border-radius: 3px;
            border: solid;
        }
    </style>
</head>

<body>
    <article class="container">
        <span>Beginner</span>
        <span>FOR</span>
        <span>Beginner</span>
        <span>ONLINE</span>
        <span>PORTAL</span>
    </article>
</body>

</html>

Output:

Supported Browsers:



Contact Us