Open In App

CSS Grid and place-items together

In this article, we will see how can we use the CSS grid and place-items property together to arrange elements on the web page.

place-items is not specifically for the CSS grid it can be used with other properties too. 



Steps:

Example: Here is an example of a CSS grid and place-item property.






<!DOCTYPE html>
<html>
<head>
    <title>CSS grid and palce-item property</title>
</head>
 
<body>
    <h1>Place-items in start</h1>
    <!-- Container to be placed from starting -->
    <div class="container initial">
        <div class="box">Item 1</div>
        <div class="box">Item 2</div>
        <div class="box">Item 3</div>
    </div>
    <h1>Place-items in middle</h1>
    <!-- Container to be placed in middle -->
    <div class="container middle">
        <div class="box">Item 1</div>
        <div class="box">Item 2</div>
        <div class="box">Item 3</div>
    </div>
    <h1>Place-items in end</h1>
    <!-- Container to be placed in end -->
    <div class="container end">
        <div class="box">Item 1</div>
        <div class="box">Item 2</div>
        <div class="box">Item 3</div>
    </div>
</body>
</html>

Example: CSS 




h1 {
    text-align: center;
    margin-top: 10px;
}
 
/* arrange boxes of container in three column form*/
.container {
    display: grid;
    grid-template-columns: 1fr 1fr 1fr;
}
 
/* starting position for first container*/
.initial {
    place-items: start;
}
 
/* middle position for second container*/
.middle {
    place-items: center;
}
 
/* ending position for third container*/
.end {
    place-items: end;
}
 
.box {
    width: 150px;
    height: 150px;
    display: flex;
    align-items: center;
    justify-content: center;
    background-color: #829099;
}

Complete Code: 




<!DOCTYPE html>
<html>
<head>
    <style>
        h1 {
            text-align: center;
            margin-top: 10px;
        }
 
        /* arrange boxes of container
          in three column form*/
        .container {
            display: grid;
            grid-template-columns: 1fr 1fr 1fr;
        }
 
        /* starting position for first container*/
        .initial {
            place-items: start;
        }
 
        /* middle position for second container*/
        .middle {
            place-items: center;
        }
 
        /* ending position for third container*/
        .end {
            place-items: end;
        }
 
        .box {
            width: 150px;
            height: 150px;
            display: flex;
            align-items: center;
            justify-content: center;
            background-color: #829099;
        }
    </style>
</head>
 
<body>
    <h1>Place-items in start</h1>
    <!-- Container to be placed from starting -->
    <div class="container initial">
        <div class="box">Item 1</div>
        <div class="box">Item 2</div>
        <div class="box">Item 3</div>
    </div>
    <h1>Place-items in middle</h1>
    <!-- Container to be placed in middle -->
    <div class="container middle">
        <div class="box">Item 1</div>
        <div class="box">Item 2</div>
        <div class="box">Item 3</div>
    </div>
    <h1>Place-items in end</h1>
    <!-- Container to be placed in end -->
    <div class="container end">
        <div class="box">Item 1</div>
        <div class="box">Item 2</div>
        <div class="box">Item 3</div>
    </div>
</body>
</html>

Output:


Article Tags :