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 - A Step-by-Step Guide.

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 create a responsive we...

What is Git and GitHub. Difference Between Git and GitHub. Workflow of Git.

Introduction In this blog, we will dive into a Git and GitHub, understand their differences, and go over the typical workflow when using GitHub. Whether you are new to version control or simply curious about how these tools work together, this guide will walk you through the essential concepts and processes you need to know. What is Git? Git is a Distributed Version Control System (DVCS) that efficiently manages code changes across multiple contributors. It helps developers to monitor and manage changes in their code efficiently. Git was created by Linus Torvalds on April 7, 2005. It is widely used in developer community for its collaborative features, allowing developers to work on the same project, whether the project is small or large. You can efficiently monitor code modifications and preserve a detailed history of changes. Git is free and open-source, designed to handle everything from individual projects to complex ones. Developers can track every modification, create branches fo...