Skip to main content

How to center a div using HTML and CSS.

Introduction

This blog is about how to center a div using CSS. There are various techniques available, and depending on the layout you are working with, some methods may be more suitable than others.

Before exploring it, you have to make sure that you have basic knowledge of HTML tags and CSS property and selectors for better understanding.

We will cover three different methods to center a div.

1. Using the Flex Property

Suppose we have a parent container and a child container and Inside the parent container we want to center a child container. so we can use the flexbox property.

HTML Code :

    <div class="parent-container">
        <div class="child-container">
        </div>
    </div>

CSS Code :

    <style>
        .parent-container{
            width: 600px;
            height: 600px;
            background-color: brown;
            display: flex;
            justify-content: center; /* centers vertically */
            align-items: center; /* centers horizontally */
        }
        .child-container{
            width: 150px;
            height: 150px;
            background-color: azure;
        }
    </style>

2. Using the Transform Property

You can also use the transform property to center an element. This method involves using margin and transform to adjust the position.

HTML Code :

    <div class="parent-container">
        <div class="child-container">
        </div>
    </div>

CSS Code :

    <style>
        .parent-container{
            width: 600px;
            height: 600px;
            background-color: brown;
            position: relative; /* Needed for absolute positioning */
        }
        .child-container{
            width: 150px;
            height: 150px;
            background-color: azure;
            position: absolute;
            margin-top: 50%; /* Pushes the child 50% down */
            margin-left: 50%; /* Pushes the child 50% right */
            transform: translate(-50%, -50%);
            /* Adjusts the child back to center */
        }
    </style>

3. Using the Grid Property

Another efficient way to center a div is by using CSS Grid.

HTML Code :

    <div class="parent-container">
        <div class="child-container">
        </div>
    </div>

CSS Code :

    <style>
        .parent-container{
            width: 600px;
            height: 600px;
            background-color: brown;
            display: grid;
            place-items: center; /* Centers both horizontally and
            vertically */
        }
        .child-container{
            width: 150px;
            height: 150px;
            background-color: azure;
        }
    </style>

Conclusion

Here we explore three different methods for centering a div. It depends on your website's design and layout which one can be useful for your requirement. You just need to understand when and how to apply these methods that will help you efficiently center a div using CSS.

Happy Coding!

Comments

Popular posts from this blog

Leetcode SQL 50 interview questions with solution: A Guide for Coders

Introduction Hi coders, In this blog, we will tackle SQL questions from LeetCode. If you're a coder, a computer science student, or simply interested in coding, you may be familiar with LeetCode. It's a platform where coding enthusiasts practice problems to enhance their critical thinking and problem-solving skills. Most of us practice programming questions on leetcode so here we practice SQL questions which is taken from leetcode. We'll start with basic SQL questions focused on SELECT , joins, and aggregate functions. Once we've covered these foundational topics, we'll transition to more advanced concepts. Before diving into the questions, ensure you have a basic understanding of SQL syntax and statements, including SELECT , various joins (self join, left join, right join, outer join), and basic aggregate functions. Here are some SQL interview questions with solution: Q1 1757. Recyclable and Low Fat Products SELECT product_id FROM Products WHERE low_fats ...

How to Create a Responsive Website Using CSS.

Responsive Website Introduction Creating a responsive website is not an option, It's number one priority for a developer to make a website responsive. A responsive website adjusts according to different screen sizes, providing an optimal user experience on desktops, tablets, and mobile devices. For creating a responsive website, You don't need JavaScript to make your website responsive, HTML and CSS are enough. In this guide, we will walk through the key techniques to build a fully responsive web page from scratch. Why is Responsiveness Important? A responsive website offers several benefits, including: Better User Experience - Ensures your content is readable on all devices. Improved SEO Ranking - Mobile-friendly websites rank higher in search results. Faster Loading Times - Optimized layouts prevent unnecessary resource consumption. Increased Engagement - Users stay longer on a site that adapts well to their screen. Now, let's dive into the essential techniques to cre...

How to create user login and signup page in php and mysql.

Introduction In this blog, we will create a user login and signup page in PHP and MYSQL. Login and signup feature is essential for any website to authenticate users. We create login and signup page for security purpose that protects some member only content or contain some features which is access by a user who is logged in. For instance, In a blog website that contains blogs related to various topics, but some blogs are members only and does not access by a user who is not a member. To excess member only content you just need to signup with your email address and you can easily access members only content. Similarly, if you want to write a blog to publish your own content and share your knowledge. You have to login on that website. After login you will be able to publish your content on that blog website. Learn how to create user login and signup page in PHP and MySQL. Before diving into this blog, you should have some basic knowledge of HTML and Bootstrap for front-end development an...