Why do my image background disappear on float: left?

Deadly 提交于 2019-12-07 03:05:38

问题


I want to create a navbar with images on each end to make it purty.

So, I created the HTML and CSS below and it worked great. My menu items are in a ul/li list.

When I style the list to put the items all on one line, the images on the ends disappear. What's up with that? How do I fix it?

The culprit is float: left; below.

--- 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>

--- 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;
}

The images are different so the html/css can be copied pasted to test it.

What's the deal? What am I doing wrong?


回答1:


I'd recommend adding overflow:hidden; (and zoom: 1; to maintain cross browser compatability for IE6) to the container div rather than adding a clearing div. Clear adds bloat to your source code.

#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.
}



回答2:


With only floated children, your outter container will return a height of 0px. As such, you can place the following immediately after the floated element(s):

<div class="clear"></div>

And add the following rules:

.clear { clear:both; }

For example:

<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>



回答3:


It's happening because your divs have no height. You can add an empty div with clear: both immediately after your UL. Or you can add these styles to UL

width: 100%;
overflow: auto;

See this for an explanation.




回答4:


Add a height: 28px; to your ul in your css. Jonathan and Chetan have already given a very good explanation of why this works, but to reiterate, parents of floated elements are not affected by the height of their floated children.



来源:https://stackoverflow.com/questions/2062134/why-do-my-image-background-disappear-on-float-left

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