How To Center a Div Using CSS Grid? Last Updated : 25 Oct, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report To center a <div> using CSS Grid, you can use a combination of grid container properties to place the <div> at the center of its parent container. The easiest way to achieve this is by setting the parent element to use display: grid and then using properties like place-items or a combination of justify-content and align-content to center the child <div>. Using place-items PropertyThe place-items property is a shorthand for setting both align-items and justify-items in one line. It can be used to center items in both directions easily. 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-columns: repeat(3, 1fr); grid-template-rows: 200px; gap: 10px; place-items: center; /* Centers items horizontally and vertically */ background-color: #f0f4f8; height: 100vh; } .grid-item { background-color: #d1e8e2; padding: 20px; border-radius: 5px; font-size: 18px; } OutputHow To Center a Div Using CSS GridAlternative Approaches to Center a DivTable of ContentUsing justify-content and align-contentUsing Grid LinesUsing justify-content and align-contentYou can also center a <div> using a combination of justify-content and align-content set to center on the grid container. HTML <style> .container { display: grid; /* Centers horizontally */ justify-content: center; /* Centers vertically */ align-content: center; height: 100vh; } .centered-div { background-color: #d1e8e2; padding: 20px; } </style> <div class="container"> <div class="centered-div">I am centered!</div> </div> OutputHow To Center a Div Using CSS GridUsing Grid LinesAnother approach is to use grid lines to explicitly place the <div> in the center by setting the grid template areas and positioning the <div> accordingly. HTML <div class="container"> <div class="left-item">Left Item</div> <div class="center-item">Center Item</div> <div class="right-item">Right Item</div> </div> CSS .container { display: grid; grid-template-columns: 1fr auto 1fr; height: 100vh; align-items: center; } .left-item { grid-column: 1; /* Align to the left */ justify-self: start; background-color: lightcoral; padding: 20px; border: 2px solid red; } .center-item { grid-column: 2; /* Center horizontally */ justify-self: center; background-color: lightgreen; padding: 20px; border: 2px solid green; } .right-item { grid-column: 3; /* Align to the right */ justify-self: end; background-color: lightblue; padding: 20px; border: 2px solid blue; } OutputCenter a Div Using CSS GridWhich Approach Is Better in Different Cases?Using place-items: center: This is the simplest and most efficient way when you need to center a single item both horizontally and vertically. It's ideal for most cases.Using justify-content and align-content: These properties offer more flexibility if you need to align multiple items or customize alignment differently on each axis.Using Grid Lines: This approach gives you explicit control over grid placement, making it suitable for complex layouts where specific grid line positioning is necessary. Comment More infoAdvertise with us Next Article How to center a using CSS grid Property ? P pankaj2025 Follow Improve Article Tags : CSS Similar Reads How to Center a Div using CSS? Here are three methods to center a div horizontally, vertically, and both horizontally and vertically using CSS.Center a div HorizontallyHere are three methods to center a div horizontally:1. Using margin PropertyThe margin: auto; property automatically centers a block-level element within its conta 4 min read How to horizontally center a div using CSS ? To make an HTML div center horizontally using CSS is quite easy. We can horizontally center any element using the below-mentioned approaches. Before centering a div horizontally we need to create a fixed-size div. We have given a border to the div so the centering of that div can be visible. Set the 2 min read How to center a <div> using CSS grid Property ? In this article, we will learn to center an HTML <div> element using the CSS grid property. We can center any element using the below-mentioned approaches. Here we will learn to center a div in 5 different ways. Table of Content Centering div - CSS Grid and place-selfCentering div - CSS Grid a 5 min read How to Create Dynamic Grid Layout using CSS? A dynamic grid layout using CSS can help us to build responsive and flexible web designs. In this article, we are going to learn about different approaches to achieving dynamic grid layouts, with their syntax, and example code for each method. A dynamic grid layout adjusts based on the content and s 3 min read How to Center an Image using Tailwind CSS ? Here are the methods to center an image using Tailwind CSS:Method 1. Using Flexbox ClassesThis method centers the image both horizontally and vertically using Tailwind's flex, justify-center, and items-center classes.HTML<html> <head> <script src="https://cdn.tailwindcss.com"></ 2 min read How to Create a Responsive Image Grid using CSS? A responsive image grid is a layout technique used to display images in a grid-like structure that adjusts dynamically based on the screen size to ensure that the grid looks good and functions well across various devices and screen resolutions. Below are the approaches to creat a responsive image gr 3 min read Like