Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS: Vertical-align

How to vertical-align without using display table/table-cell or position absolute ?

#parent {
  border: 1px solid red;
  height: 100vh;
}

.child {
  border: 1px solid blue;
}
<div id="parent">
  <div class="child">
    <p>I want to be vertical aligned</p>
  </div>
</div>
like image 322
George Adrian Echim Avatar asked Dec 19 '25 20:12

George Adrian Echim


2 Answers

Here is an another option using "Flex" property.

<div id="parent">
  <div class="child">
    <p>I want to be vertical aligned</p>
  </div>
</div>

#parent {
  border: 1px solid red;
  display: flex;
  align-items: center;
  height: 100vh;
}

.child {
  border: 1px solid blue;
  flex-grow: 1;
}

Codepen demo link

like image 144
Satheesh Kumar Avatar answered Dec 21 '25 13:12

Satheesh Kumar


You can use position relative, with top of 50% and a translation of -50%.

#parent {
  border: 1px solid red;
  height: 100vh;
}

.child {
  position: relative;
  top: 50%;
  transform: translate(0,-50%);
  border: 1px solid blue;
}
<div id="parent">
  <div class="child">
    <p>I want to be vertical aligned</p>
  </div>
</div>
like image 35
dvr Avatar answered Dec 21 '25 13:12

dvr