float-left的对象如何居中

我们在做导航(nav)时,通常会用到float:left;但是这样做的话,让导航文本水平居中确实个麻烦事;

分析如下:  

[float:left]有个伟大之处,它使div(或者其他标签)的宽度自适应其内容,但它却有个弊端:无法居中。
[display:inline-block]也有同样的特性,并且可以居中,但连续几个这样的东东,之间却会出现空格。

为了解决这个问题,我们可以把二者结合起来使用:  

Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/--> 1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>inline-block解决float:left对象无法居中的问题。</title>
        <style type="text/css">
            * { margin:0; padding:0; list-style:none;}
            body { text-align:center;}
            li { float:left; font-size:12px;}
            a { float:left; border:1px solid #000; padding:5px 10px; text-decoration:none; color:#000;}
            ul { display:inline-block; *display:inline; zoom:1;}
        </style>
    </head>
    <body>
        <ul>
            <li><a href="#nogo">首页</a></li>
            <li><a href="#nogo">关于</a></li>
            <li><a href="#nogo">产品</a></li>
            <li><a href="#nogo">联系我们</a></li>
            <li><a href="#nogo">留言</a></li>
        </ul>
    </body>
 </html>
原文链接:http://www.cnblogs.com/hema/archive/2009/12/17/1626497.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值