为什么我的图像背景在浮动上消失:左?

6 html css menubar background-image

我想在每一端创建一个带有图像的导航栏,以使其成为真实的.

所以,我在下面创建了HTML和CSS,效果很好.我的菜单项目在ul/li列表中.

当我设置列表样式以将项目全部放在一行上时,两端的图像消失.那是怎么回事?我如何解决它?

罪魁祸首是浮动:左; 下面.

--- test.html ---

<html>
<head>
    <link rel="stylesheet" href="test.css" type="text/css">
</head>
<body>
    <div id="container">
        <div id="header-usernav" class="span-6 last large">
            <div id="header-usernav-leftside"><div id="header-usernav-rightside">
                <ul>
                    <li>Register</li>
                    <li>Signin</li>
                    <li>Signout</li>
                </ul>
            </div></div>
        </div>
    </div>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

--- test.CSS ---

#container #header-usernav {
    background: url('http://www.webcredible.co.uk/i/bg-i-resources.gif');
    height: 28px;
    background-repeat: repeat;
}
#container #header-usernav-leftside {
    background: url('http://www.webcredible.co.uk/i/nav-l-h.gif');
    background-position: top left;
    background-repeat: no-repeat;
}
#container #header-usernav-rightside {
    background: url('http://www.webcredible.co.uk/i/nav-r-h.gif');
    background-position: top right;
    background-repeat: no-repeat;
}

#container #header-usernav ul {
    list-style: none;
    padding: 0;
    margin: 0;
    margin-left: 10px;
}

#container #header-usernav li {
    float: left;
    padding: 0;
    margin: 0 0.2em;
}
Run Code Online (Sandbox Code Playgroud)

图像不同,因此可以复制粘贴html/css来测试它.

这是怎么回事?我究竟做错了什么?

Sas*_*sha 7

我建议添加overflow:hidden; (并缩放:1;保持IE6的跨浏览器兼容性)到容器div而不是添加清除div.清除会为您的源代码添加膨胀.

#container #header-usernav ul {
    list-style: none;
    padding: 0;
    margin: 0;
    margin-left: 10px;
    overflow: hidden;
    zoom: 1;
    height: 28px; /* This is needed to ensure that the height of the rounded corners matches up with the rest of the bg.
}
Run Code Online (Sandbox Code Playgroud)


Sam*_*son 5

只有浮动的孩子,你的外部容器将返回0px的高度.因此,您可以在浮动元素之后立即放置以下内容:

<div class="clear"></div>
Run Code Online (Sandbox Code Playgroud)

并添加以下规则:

.clear { clear:both; }
Run Code Online (Sandbox Code Playgroud)

例如:

<div id="container">
    <div id="header-usernav" class="span-6 last large">
        <div id="header-usernav-leftside">
          <div id="header-usernav-rightside">
            <ul>
                <li>Register</li>
                <li>Signin</li>
                <li>Signout</li>
            </ul>
            <div class="clear"></div>
          </div>
        </div>
    </div>
</div>
Run Code Online (Sandbox Code Playgroud)