Menu

Centering a Div in CSS: A Quick Guide.

While I was first doing CSS in 2016, this is the problem I use to face and search on Google so frequently. Centering a div element is a common task in web development, so common that it may seem straightforward, there are several methods to achieve it. In this short guide, I will guide you to explore a few ways to center a div both horizontally and vertically using CSS.

Method 1: Margin Auto (Horizontal Centering)

The simplest way to horizontally center a div is by using the margin: auto; property. This method works when the width of the div is specified.

                        
                          <style>
                            .center-div {
                              width: 50%;
                              margin: auto;
                              background-color: #f2f2f2;
                              padding: 20px;
                            }
                          </style>
                      
                          <div class="center-div">
                            Content goes here
                          </div>
                        
                      

Method 2: Flexbox (Both Horizontal and Vertical Centering)

Flexbox is a powerful layout model in CSS that simplifies centering both horizontally and vertically. Use the following CSS for a flex container:

                        
                        <style>
                            .flex-container {
                            display: flex;
                            justify-content: center; /* Horizontal centering */
                            align-items: center; /* Vertical centering */
                            height: 100vh; /* Optional: Make the container full height */
                            }
                    
                            .center-div {
                            background-color: #f2f2f2;
                            padding: 20px;
                            }
                        </style>
                    
                        <div class="flex-container">
                            <div class="center-div">
                            Content goes here
                            </div>
                        </div>
                        
                    

Method 3: Grid (Both Horizontal and Vertical Centering)

CSS Grid is another layout model that allows for centering elements. Similar to flexbox, it provides both horizontal and vertical centering.

                        
                        <style>
                            .grid-container {
                            display: grid;
                            place-items: center; /* Center both horizontally and vertically */
                            height: 100vh; /* Optional: Make the container full height */
                            }
                    
                            .center-div {
                            background-color: #f2f2f2;
                            padding: 20px;
                            }
                        </style>
                    
                        <div class="grid-container">
                            <div class="center-div">
                            Content goes here
                            </div>
                        </div>
                        
                    

Choose the method that best fits your layout requirements. Experiment with these techniques to find the one that suits your project and enhances the overall design. This is not going to be the last time you are searching this type of tutorial, you can also bookmark this for future or share with your friend struggling to Center a Div Happy Coding!