Explore Topics

Center Div in CSS Horizontally

Last Updated : 18 Apr, 2025 - Asked By Ashok

Iam trying to centre a div Horizontally. I added "text-align: center" to div. But it does not work. Can you please add your suggestions?

css  div alignment 

Answers
2024-01-26 04:55:28

Except in IE6, this is a bug. 

You can fix the width of the block using margin: 0 auto

    #block_div{
        margin: 0 auto;
        width: 200px;
        border: 1px solid red;
    }

    <div id="block_div">center a div using css</div>
2024-01-26 04:57:39

To center div in CSS Horizontally, Just use text-align property with value center property

Suppose this is your HTML ELement

    <div class="container">
      <p>This text needs to center Horizontally</p>
    </div>
    <style>
      .container {
            font-size: 25px;
            width: 450px;
            margin: 35px;
            height: 200px;
            font-family: arial;
            outline: dashed 2px black;
      }
    </style>

         p {
             /* To Center horizontally*/
              text-align: center;
          }

 

2024-01-26 04:58:35

To center an element horizontally with Flexbox, just apply justify-content: center and display: flex  to the parent element:

    <div class="container">
      <div class="child_class"></div>
    </div>

    .container {
          margin: 35px;
          width: 450px;
          font-family: arial;
          font-size: 25px;
          height: 300px;
          outline: dashed 1px black;
          justify-content: center;
          display: flex;
    }

    .child_class {
        height: 50px;
        width: 50px;  
        background-color: green;
    }
2024-01-31 05:07:29

To center a <div> horizontally, you can use the following CSS properties:

.center-div {
  margin: 0 auto; /* This centers the div horizontally */
}

Margin: 0 auto;: This sets the top and bottom margins to 0 and the left and right margins to "auto", which effectively centers the <div> horizontally within its containing element.

Then, apply the center-div class to the <div> you want to center:

<div class="center-div">
  <!-- Content goes here -->
</div>

 

2024-02-14 05:42:22

.parent-div {
  display: flex;
  justify-content: center;
}

2024-02-22 04:52:30

Can do it by

1|   .div{

margin: 0 auto;

2|  .div{

display : flex;

justify-content : center

}

 

 

2024-03-18 17:01:32

.parent-div{

display: flex;

justify-content: center

}

Your Answer



Other Resources

Quiz Image
Quiz

Test your knowledge with interactive quizzes.

Interview Questions Image
Interview Questions

Prepare for interviews with curated question sets.

Q&A Image
Q&A

Ask your coding-related doubts and get answers.

Certification Image
Certification

Earn certifications to enhance your resume.

internships Image
Internships

Hands-on projects to improve your skills.

Quiz Image
Quiz

Test your knowledge with interactive quizzes.

Interview Questions Image
Interview Questions

Prepare for interviews with curated question sets.

blog Image
Blogs

Add your technical blogs and read technical topics.

Certification Image
Certification

Earn certifications to enhance your resume.

Q&A Image
Q&A

Hands-on projects to improve your skills.