How can I vertically center an image with an anchor inside a div?

我是研究僧i 提交于 2019-12-19 10:20:13

问题


I have a row of DIVs for my navigation and I'm trying to vertically center an image with an anchor inside them using only CSS. I tried vertical-align: middle; but that isn't working. I'm sure it has to do with the float:left since all the images are pushed to the left side but when I take it out the divs go crazy.

HTML

<div class="linkgroup">
        <img src="images/homeIcon.png">
        <li class="navItem"><a href="somelink.com" class="navLink">HOME</a></li>
    </div>
</ul>

CSS

.linkgroup{
    float: left;
    height:5px;
    }

回答1:


Don't know why you are using li to hold the anchor element. From the question there is no mention for the li. So I assume you just need one image and one anchor. Check this out:

http://jsfiddle.net/U64Tg/2/

Basically you just need to have the helper to have 100% height of the container div. And you need to use

vertical-align: middle;
display: inline-block;

on all child elements in the container div to make them vertically aligned.



来源:https://stackoverflow.com/questions/16766004/how-can-i-vertically-center-an-image-with-an-anchor-inside-a-div

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!