user319854
user319854

Reputation: 4106

Problem with vertical-align: middle;

How does div sub set div center to middle? Text can be different, so margin or line-height in this situation is not a good solution.

CSS:

.center {
    background-color: #336699;
    width: 200px;
    height: 200px;
}

.sub {
    display: inline-block;
    vertical-align: middle;
}

HTML:

<div class="center">
    <span class="sub">
        Some text text...
    </span>
</div>

Upvotes: 1

Views: 4078

Answers (1)

vlad saling
vlad saling

Reputation: 375

A possible solution is:

HTML

<div class="center">
    <span class="sub">
        Some text text...<br />
        An some more text...
    </span>
</div>

CSS

.center
 {
     background-color: #336699;
     width: 200px;
     height: 200px;
     display: table;
 }

.sub
 {
     display: table-cell ;
     vertical-align: middle;
     text-align: center;
 }

A live demo, if needed, are at http://jsfiddle.net/vladsaling/6nTGF/.

Upvotes: 3

Related Questions