简体   繁体   中英

vertical align image and text in div

I have a parent div which includes an image and text and they should be vertically aligned: middle, though I do not get it to work. Not sure what I did wrong.

Find below my Fiddle

jsfiddle

HTML

<div class="social-cont">
    <div class="social-cont1">
        <img src="img/contact1.png" alt="contact1" width="21" height="30" />
    </div> <!-- end social-cont1 -->
    <div class="social-cont2">
        <h6 class="social-context">Pastoor de Leijerstraat 29, 5246JA, Hintham</h6>
    </div> <!-- end social-cont1 -->

</div> <!-- end social-cont -->

<div class="social-cont">
    <div class="social-cont1">
        <img src="img/contact3.png" alt="contact3" width="23" height="24" />
    </div> <!-- end social-cont1 -->
    <div class="social-cont2">
        <h6 class="social-context">XXXXXXXXXXXXXX</h6>
    </div> <!-- end social-cont1 -->
</div> <!-- end social-cont -->

CSS

.social-cont {
    height: 100%;
    width: 350px;
    border-bottom: 1px solid #c1c1c1;
    display: table;
    background-color: salmon;
}

.social-cont1 img {
    display: table-cell;
    vertical-align: middle;
    float: left;
}

h6.social-context {
    display: table-cell;
    vertical-align: middle;
    color: #404040;
    float: left;
}

This is what I get when previewing the code in Coda:

这是我在Coda中预览代码时得到的

If you use display: inline-block and use vertical-align: middle on your container divs the content will be vertically aligned correctly.

Fiddle Here

.social-cont2,
.social-cont1{display: inline-block; vertical-align: middle;}

Edit: Fiddle using table-cell instead

EDIT: Fiddle with valid image files

You don't need float: left and in fact you don't need .social-cont1 and .social-cont2 , you can style img and h6

.social-cont1 {
    display: table-cell;
    vertical-align: middle;
}

.social-cont2 {
    display: table-cell;
    vertical-align: middle;
    color: #404040;
}

http://jsfiddle.net/fwshE/3/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM