1

I want to center align #container following code:

<div id="container">
   <p>new</p>
</div>

css-

#container {
   position:relative;
   width:100%;
   background:red;
   padding:10px;
   float:left;
   margin-left:30%;/*for centering*/
}

p {
    text-align: center; 
}

It does not align center properly with respect to content,please help me.

Bun
  • 3,037
  • 3
  • 19
  • 29
  • Possible duplicate of [How to vertically center a div for all browsers?](http://stackoverflow.com/questions/396145/how-to-vertically-center-a-div-for-all-browsers) – easwee Nov 18 '15 at 15:38

2 Answers2

1

You want to have a div with 100% and a children with a specific width and margin 0 auto

#body  {
  width: 100%;
  }

#container {
  width:100px;
  background:red;
  padding:10px;
  margin:0 auto;/*for centering*/
}

p {
    text-align: center; 
}
<div id="body">
  <div id="container">
    <p>new</p>
  </div>
</div>
Paulo Menezes
  • 539
  • 3
  • 15
0

Try This-

#container {
   position:relative;
    width:100%;
    background:red;
    padding:10px;
    display: flex;
    flex-direction: column; 
    justify-content: center; 
    align-items: center; 
    }

    p {
        text-align: center; 
    }
Shantanu Verma
  • 260
  • 2
  • 12