Skip to main content

Center Div in CSS Horizontally

Published: Asked By 6 Answers Answered

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?

Share:

7 Answers

K
K Savad Community Contributor Answered on

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>
F
Femina Community Contributor Answered on

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;
          }

 

J
jangojango Community Contributor Answered on

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;
    }
L
LAZIO LEO Community Contributor Answered on

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>

 

A
Alex TW Community Contributor Answered on

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

A
Ameen Community Contributor Answered on

Can do it by

1|   .div{

margin: 0 auto;

2|  .div{

display : flex;

justify-content : center

}

 

 

S
Sahadha Community Contributor Answered on

.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
Certifications

Earn certifications to enhance your resume.