a元素里面的图片与文字垂直和水平居中

废话不多说,直接上效果如下:
a元素里面的图片与文字垂直和水平居中_第1张图片
代码:

<meta charset="utf-8">
<style type="text/css">
*{
    padding: 0;
    border:0;
    margin: 0;
}
ul li{
    list-style-type: none;
}
a{text-decoration:none; }

.inlineHalk{
    font-size:0;
    -webkit-text-size-adjust:none;
}
.aling-top{
    vertical-align:top;
}
#nav-box{
    width: 1000px;
    height: 45px;
    background-color: #eee;
    margin: 0 auto;
}
.nav-list{
    display: inline-block;
    width: 120px;
    height: 45px;
    font-size: 16px;
    margin-left: 10px;
}
.nav-list:first-child{
    margin-left: 0px;
}
.nav-list a{
    color: #000;
    display: block;
    width: 100%;
    height: 45px;
    line-height: 45px;
    background-color: #DBE8F5
}
.nav-list a:hover{
    background-color: #93BCEA;
    color: #0F0;
}
.nav-list a img{
    vertical-align: middle;
}
style>
<ul id="nav-box" class="inlineHalk">
    <li class = "nav-list aling-top">
        <a href = "annocement.html">
            <img src = "images/horn.png">  公告
        a>
    li>
    <li class = "nav-list aling-top">
        <a href = "my.html">
            <img src = "images/house.png">  我的工作坊
        a>
    li>
    <li class = "nav-list aling-top">
        <a href = "javascript:;">
            <img src = "images/exit.png">  退出
        a>
    li>
ul>

备注

.inlineHalk是去除inline-block元素间间距,方式有很多,具体可以参考:http://blog.csdn.net/dq_pp/article/details/58300264

display:inline-block元素的父元素定义 font-size:0 ,可去掉元素水平4px的空白,子元素若要显示文字可重新定义font-size属性即可
display:inline-block元素本身定义 vertical-align 属性可去掉元素垂直方向的多余空白

你可能感兴趣的:(css+html相关)