-1

I need the following HTML (jsfiddle):

<div class="main">
    <div class="top">Top</div>
    <div class="bottom">Bottom</div>
</div>

To look something like this:

enter image description here

Not like this:

enter image description here

Jerad Rose
  • 15,235
  • 18
  • 82
  • 153

4 Answers4

2

Can you change your HTML layout a little ?

.main {
    display: table;
    height: 100px;
    border: solid 1px;
}
.inner {
    display:table-cell;
    vertical-align:middle
}
.top, .bottom {background:yellow;}
<div class="main">
    <div class="inner">
        <div class="top">Top</div>
        <div class="bottom">Bottom</div>
    </div>
</div>

Check this below.

Deepak Yadav
  • 6,804
  • 5
  • 31
  • 49
  • This is the better answer, because the parent div should be `display:table` otherwise kinda goofy for child div to use `display:table-cell` ... – Jesse Nickles Oct 06 '22 at 09:08
1

Just set the display of the parent to table-cell:

.main {
    display: table-cell;
    height: 100px;
    border: solid 1px;
    vertical-align: middle;
}
<div class="main">
    <div class="top">Top</div>
    <div class="bottom">Bottom</div>
</div>
j08691
  • 204,283
  • 31
  • 260
  • 272
0
position: relative;
top: 50%;
transform: translateY(-50%);
  • Close, but [not quite](http://jsfiddle.net/q0kLojwx/2/) (I saw [that article](http://zerosixthree.se/vertical-align-anything-with-just-3-lines-of-css/) too). – Jerad Rose Sep 04 '15 at 18:37
0

HTML:

<div class="main">
    <div class="wrapper">
        <div class="top">Top</div>
        <div class="bottom">Bottom</div>
    </div>
</div>

CSS:

.main {
    display: inline-block;
    height: 100px;
    border: solid 1px;
    line-height: 100px;
    vertical-align: middle;
}

.wrapper {
    line-height: 1em;
    display: inline-block;
    vertical-align: middle;
}

DEMO: https://jsfiddle.net/q0kLojwx/5/

Soheil Jadidian
  • 878
  • 7
  • 12