CSS - 将导航栏与图像和文本对齐

时间:2014-01-13 19:35:58

标签: html css menu navigationbar

我有一个菜单(如下图所示),我试图正确地将文本与图像对齐(即[img] [text])以及“拉伸”整个内容以适应所有宽度。

图像: Navigation Menu

HTML:

<div id="menu">
      <ul>                                        
           <li><a class="current" href="home.html" title="">HOME</a></li>
           <li><a href="services.html" title="">TRANSPORT</a></li>
            <li><a href="#" title="">ACCOMODATION</a></li>
            <li><a href="#" title="">DEALS</a></li>
            <li><a href="contact.html" title="">CONTACT</a></li>
      </ul>
</div>

CSS:

#menu{
    width:900px;
    height: 50px;
    clear:both;
    background-color: #f7951e;
    font-family: 'Exo 2', sans-serif;
    text-align: center;

}
#menu ul{
    font-family: 'Exo 2', sans-serif;
    list-style:none;
    margin-top: 5px;
    margin-bottom: 5px;
    display:block;
    float: left;
    width: auto;
}
#menu ul li{
    font-family: 'Exo 2', sans-serif;
    list-style:none;
    display:inline;
    width:auto;
    height:50px;
    line-height:42px;
    font-size:18px;
}
#menu ul li a{
    font-family: 'Exo 2', sans-serif;
    font-size: 18px;
    height: auto;
    width: auto;
    float:next;
    text-decoration: none;
    color:#fff;
    padding-left: 40px;
    padding-right: 10px;
}
#menu ul li a:hover{
    height:auto;
    width: auto;
    text-decoration:none;
    color: #fff;
}
#menu ul li a.current{
    height:auto;
    width: auto;
    text-decoration:none;
    color: #fff;
}

#menu ul li:nth-of-type(1){

    background:url(./images/iconza/house_32x32.png) no-repeat;
    float: start;
    padding-top: 5px;
    padding-bottom: 10px;
}

#menu ul li:nth-of-type(2){

    background:url(./images/iconza/Car.png) no-repeat;
    float: start;
    padding-top: 5px;
    padding-bottom: 10px;

}

#menu ul li:nth-of-type(3){

    background:url(./images/iconza/house.png) no-repeat;
    float: start;
    padding-top: 5px;
    padding-bottom: 10px;
}

#menu ul li:nth-of-type(4){

    background:url(./images/iconza/offer.png) no-repeat;
    float: start;
    padding-top: 5px;
    padding-bottom: 10px;
}
#menu ul li:nth-of-type(5){

    background:url(./images/iconza/mail_32x32.png) no-repeat;
    float: start;
    padding-top: 5px;
    padding-bottom: 10px;
}

1 个答案:

答案 0 :(得分:4)

我建议在display: table-cell;中使用#menu ul li而不是float: left;(我是从一本关于响应式设计的书中学到的),并将背景图像放入html作为imgs:

查看我的 LIVE DEMO

这是修改后的html:

<div id="menu">
  <ul>                                        
    <li><a class="current" href="home.html" title=""><img src=""><span>HOME</span></a</li>
    <li><a href="services.html" title=""><img src=""><span>TRANSPORT</span></a></li>
    <li><a href="#" title=""><img src=""><span>ACCOMODATION</span></a></li>
    <li><a href="#" title=""><img src=""><span>DEALS</span></a></li>
    <li><a href="contact.html" title=""><img src=""><span>CONTACT</span></a></li>
  </ul>
</div>

和css:

#menu{
    width:900px;
    height: 50px;
    background-color: #f7951e;
    font-family: 'Exo 2', sans-serif;
    text-align: center;
    display: table;

}
#menu ul{
    font-family: 'Exo 2', sans-serif;
    display:table-row;

}
#menu ul li{
    font-family: 'Exo 2', sans-serif;
    list-style:none;
    display:table-cell;
    font-size:18px;
    vertical-align: middle;
}
#menu ul li a{
    font-family: 'Exo 2', sans-serif;
    text-decoration: none;
    color:#fff;
}

#menu ul li span {
    vertical-align: middle;
    margin-left: 5px;
}
#menu ul li img{
    vertical-align: middle;
}
#menu ul li a:hover{
    color: #ababab;
}
#menu ul li a.current{
    text-decoration:underline;
}

#menu ul li:first-child {
    text-align: left;
    padding-left: 10px;
}

#menu ul li:last-child {
    text-align: right;
    padding-right: 10px;
}