Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vertical align div inside another div without flex

Tags:

html

css

How to vertically align div inside another div using property vertical-align:middle.

Here is my code.

.hello {
  height: 100px;
  width: 100px;
  background-color: black;
  vertical-align: middle;
  display: inline-block;
  color: white;
}
.parent {
  height: 400px;
  width: 400px;
  border: 1px solid red;
  display: table-cell;
}
<div class ="parent ">
  <div class="hello">
    hello
  </div>
</div>    

I referred and found giving parent table-cell property and child inline-block works but still not.

Html

like image 771
Rahul Avatar asked Oct 24 '17 04:10

Rahul


People also ask

How do I vertically align a div inside another div?

Vertically centering div items inside another div Just set the container to display:table and then the inner items to display:table-cell . Set a height on the container, and then set vertical-align:middle on the inner items.

How do I vertically center a div with Flex?

Centering Vertically By default flex items will fill vertical space of their parent element. To vertically center our div we can add a single CSS property. By using align-items: center we can vertically center all flex items to the parent container along the cross axis of the flex container.


1 Answers

Here you go.

Code Snippet:

.hello {
  height: 100px;
  width: 100px;
  background-color: black;
  vertical-align: middle;
  display: inline-block;
  color: white;
}

.parent {
  height: 400px;
  width: 400px;
  border: 1px solid red;
  display: table-cell;
  vertical-align: middle;
  text-align: center;
}
<div class="parent ">
  <div class="hello">
    hello
  </div>
</div>
like image 113
vivekkupadhyay Avatar answered Sep 28 '22 01:09

vivekkupadhyay