-3

I want to center the paragraph in div "box", but vertical is not correctly centered.

.container {
  position: relative;
}
p {
  position: absolute;
  left: 0;
  top: 50%;
  width: 100%;
  text-align: center;
}
.box {
  width: 100%;
  height: 120px;
  background-color: #62a6c9;
}
<div class="container">
  <div class="box"></div>
  <p>center</p>
</div>
j08691
  • 204,283
  • 31
  • 260
  • 272
R. Catalin
  • 25
  • 4
  • `top: 50%;` does not "center" an element, it only aligns it's top edge in the middle. And a `p` element has default margins from the browser stylesheet, that also influence the element position here. – CBroe Nov 28 '16 at 18:13
  • Possible duplicate of [CSS Center text (Horizontal and Vertical) inside a DIV block](http://stackoverflow.com/questions/5703552/css-center-text-horizontal-and-vertical-inside-a-div-block) – jacefarm Nov 28 '16 at 18:55
  • Welcome to StackOverflow. Please be sure to look for existing solutions. This question has been asked and answered numerous times before. Good luck! – jacefarm Nov 28 '16 at 18:57

3 Answers3

0

add transform:translateY(-50%); margin:0; to the p to make it middle vertically

.container {
       position: relative;
}

p {
       position: absolute;
       left: 0;
       top: 50%;
       width: 100%;
       text-align: center;
  transform:translateY(-50%);
  margin:0;
}

.box {
       width: 100%;
       height: 120px;
       background-color: #62a6c9;
}
<div class="container">
        <div class="box"></div>
        <p>center</p>
    </div>
jafarbtech
  • 6,842
  • 1
  • 36
  • 55
0

You should change your approach, the element .box used as a push-down margin is totally unnecessary, the paragraph p that is the element we want to keep in the middle of the .container should have a reset on its margins and the .container should have a fixed height.

Finally, use flex-boxes:

.container {
  height: 200px;
  background: orange;
  
  display: flex;
  align-items: center;
  justify-content: center;
}

.container p {
  margin: 0;
  background: cyan;
}

.container img {
  width: 100px;
  height: auto;
}

.wrapper {
  padding-left: 20px;
}
<div class="container">
  <img src="http://flaviusso.altervista.org/blog/wp-content/uploads/2013/11/html-css.jpg" alt="http://flaviusso.altervista.org/blog/wp-content/uploads/2013/11/html-css.jpg" />
  <p>Hello World</p>
  
  <div class="wrapper">
    
  <img src="http://flaviusso.altervista.org/blog/wp-content/uploads/2013/11/html-css.jpg" alt="http://flaviusso.altervista.org/blog/wp-content/uploads/2013/11/html-css.jpg" />
  <p>Hello World</p>
  </div>
</div>
Hitmands
  • 13,491
  • 4
  • 34
  • 69
0

note that your p is not inside .box currently. You can create a fake table, add display: table to parent, and display: table-cell for child.

.container {
  position: relative;
}
p {
  display: table-cell;
  margin: 0;
  vertical-align: middle;
}
.box {
  background-color: #62a6c9;
  display: table;
  height: 120px;
  text-align: center;
  width: 100%;
}
<div class="container">
  <div class="box">
    <p>center</p>
  </div>
</div>
DigitCart
  • 2,980
  • 2
  • 18
  • 28