为什么我的div没有离开?

时间:2016-03-27 00:39:37

标签: html css css-float

我正在尝试使用<div id="sidebar">将嵌套的float: left;置于左侧,但它无效。相反,它只是停留在页面的中间。

   <!DOCTYPE html>
<html lange="en">

<head>
    <meta charset="utf-8" />
    <title>Enhanced</title>
    <style>
    body {
        background : #b3d9ff;
        margin : 0;
        padding: 0;
        font-family : Futura;
    }
    #wrapper {
        width: 960px;
        height: auto;
        background: #cce5ff;
        border-left: 5px solid #737373;
        border-right: 5px solid #737373;
        overflow : auto;
        margin : 0 auto;
        padding: 10px;
    }
    #header {
        width:100%;
        height:100px;
        border-bottom: 3px solid #000;
    }
    #logo {
        float: left;
        margin: 15px 0px 0px 80px;
    }
    #social {
        float: right;
        margin: 20px 30px 0px 0px;
    }
    #social ul li {
        float: left;
        list-style: none;
        padding-right: 5px;
    }
    #sidebar {
        float: left;
        width: 275px;
        height: 100%;
    }
    #menu {
        float: left;
        height: auto;
        width: 200px;
    }
    #menu ul li {
        list-style : none;
        padding: 0px;
        text-align: center;
    }
    #menu ul li a {
        color: #666666;
        text-decoration: none;
        display: block;
    }
    #menu ul li a:visited{
        color:purple;
    }
    #menu ul li a:hover {
        color:black;
    }
    #content {
        float: left;
        width: 655px;
        height: 100%;
        padding-left: 15px;
        letter-spacing : 1;
        border-left: 3px solid black;
    }
    </style>
</head>

<body>

<div id="wrapper">
    <div id="header">
        <a name="top"></a>
        <div id="logo">
                <img src="https://janikvonrotz.ch/wp-content/uploads/2015/10/Python-Logo.png" width="232" height="101" alt="Logo" title="python">
        </div> <!-- End of logo -->
        <div id="social">
            <ul>
                <li><a href="https://github.com/GallardoBrayan" target="_blank"><img src="http://icons.iconarchive.com/icons/limav/flat-gradient-social/512/Github-icon.png" width="55" height="55" alt="github" title="github"></a></li>
            </ul>
        </div> <!-- End of social -->
    </div> <!-- End of header -->
    <div id="sidebar">
        <div id="menu">
            <ul>
                <li><h4><a href="#home">Home</a></h4></li>
            </ul>
        </div> <!-- End of menu -->
    </div> <!-- End of sidebar -->
    <div id="content">
    </div> <!-- End of content -->
</div> <!-- End of wrapper -->

</body>

</html>

2 个答案:

答案 0 :(得分:3)

使用float从正常文档流中删除元素。在包装元素中有多个元素浮动,因此您需要在侧边栏向左浮动之前清除它们。但是,我建议完全删除徽标周围的div,因为它并不是必需的(唯一的是你的img,你可以直接设置)。

删除包装div并将其选择器更改为#header > img {},您也不需要浮动:

&#13;
&#13;
body {
    background: #b3d9ff;
    margin: 0;
    padding: 0;
    font-family: Futura;
}
#wrapper {
    width: 960px;
    height: auto;
    background: #cce5ff;
    border-left: 5px solid #737373;
    border-right: 5px solid #737373;
    overflow: auto;
    margin: 0 auto;
    padding: 10px;
}
#header {
    width: 100%;
    height: 100px;
    border-bottom: 3px solid #000;
    clear: right;
}
#header > img {
    margin: 15px 0px 0px 80px;
}
#social {
    float: right;
    margin: 20px 30px 0px 0px;
}
#social ul li {
    float: left;
    list-style: none;
    padding-right: 5px;
}
#sidebar {
    float: left;
    width: 275px;
    height: 100%;
}
#menu {
    float: left;
    height: auto;
    width: 200px;
}
#menu ul li {
    list-style: none;
    padding: 0px;
    text-align: center;
}
#menu ul li a {
    color: #666666;
    text-decoration: none;
    display: block;
}
#menu ul li a:visited {
    color: purple;
}
#menu ul li a:hover {
    color: black;
}
#content {
    float: left;
    width: 655px;
    height: 100%;
    padding-left: 15px;
    letter-spacing: 1;
    border-left: 3px solid black;
}
&#13;
<div id="wrapper">
    <div id="header">
        <a name="top"></a>
        <img src="https://janikvonrotz.ch/wp-content/uploads/2015/10/Python-Logo.png" width="232" height="101" alt="Logo" title="python">
<!-- End of logo -->
        <div id="social">
            <ul>
                <li><a href="https://github.com/GallardoBrayan" target="_blank"><img src="http://icons.iconarchive.com/icons/limav/flat-gradient-social/512/Github-icon.png" width="55" height="55" alt="github" title="github"></a></li>
            </ul>
        </div> <!-- End of social -->
    </div> <!-- End of header -->
    <div id="sidebar">
        <div id="menu">
            <ul>
                <li><h4><a href="#home">Home</a></h4></li>
            </ul>
        </div> <!-- End of menu -->
    </div> <!-- End of sidebar -->
    <div id="content">
    </div> <!-- End of content -->
</div> <!-- End of wrapper -->
&#13;
&#13;
&#13;

答案 1 :(得分:0)

如果我理解你的话;你在主div包装器中有2个div“header”和“sidebar”。最好的办法是在CSS文件中设置标题样式以设置清除:在#header

的CSS中