简体   繁体   English

我必须设置容器的高度吗?

[英]Do I have to set the height of the container?

I have this code: 我有以下代码:

#container {
    position:relative;
    width:760px;    

    margin-left: auto;
    margin-right: auto;
    margin-top: 10px;

    border: 1px solid #cdcdcd;  
}

#sidebar {
    float:left;
    width: 200px;
    background:red;
}

#wrapper {
    float:left;
    width:460px;
background:blue;

} }

and this HTML CODE: 和这个HTML代码:

<!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>
        <title>Example.com</title>
        <link rel="stylesheet" type="text/css" href="style.css"/>
    </head>
    <body>
        <div id="container">
            <div id="sidebar">
                        LEFT
            </div>

            <div id="wrapper">
                RIGHT
            </div>          
        </div>  
    </body>
</html>

I see that the container has no height.... do I Must set it? 我看到容器没有高度。...我必须设置它吗? I would like to explain the height depending on the height of the inner DIVs, How could i do it? 我想根据内部DIV的高度来说明高度,我该怎么办?

You do not have to set the height, just add: 您不必设置高度,只需添加:

<div style="clear: both;"></div>

directly below any divs that you float. 直接在您浮动的所有div下方。


In your case, you could do this: 就您而言,您可以执行以下操作:

Just add this to your CSS file. 只需将其添加到您的CSS文件即可。

<!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>
        <title>Example.com</title>
        <link rel="stylesheet" type="text/css" href="style.css"/>
    </head>
    <body>
        <div id="container">
            <div id="sidebar">
                        LEFT
            </div>

            <div id="wrapper">
                RIGHT
            </div>    
            <div class="clear"></div>      
        </div>  
    </body>
</html>

And update your HTML file to match 并更新您的HTML文件以使其匹配

 <!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> <title>Example.com</title> <link rel="stylesheet" type="text/css" href="style.css"/> </head> <body> <div id="container"> <div id="sidebar"> LEFT </div> <div id="wrapper"> RIGHT </div> <div class="clear"></div> </div> </body> </html> 

That happens because you used float:left . 发生这种情况是因为您使用了float:left One solution is to add a clear:both div as follow: 一种解决方案是添加一个clear:both如下:

CSS: CSS:

.clear {
    clear: both;
}

HTML: HTML:

<div id="container">
    <div id="sidebar">LEFT</div>
    <div id="wrapper">RIGHT</div>
    <div class="clear"></div>
</div>  

Give the container the additional CSS 给容器附加CSS

overflow: hidden;

This will break the float with no extra markup 这将打破浮动,没有额外的标记
(Will not work with box-shadow inside) (将无法在其中使用盒子阴影)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我必须为BG图片或父容器指定高度吗? - Do I Have To Specify A Height For BG Image Or Parent Container? 将固定容器转换为流体...如何设置高度? - Converting fixed container to fluid… how do I set the height? 我将html,body和container div设置为100%高度,但页面顶部仍然有空隙吗? - I have html,body and container div set to 100% height yet there is still a gap at top of page? 如何使 overflow:scroll div 与其容器具有相同的高度? 里面的细节 - How do I make a overflow:scroll div have the same height of its container? Details inside 如何使TABLE成长为与其容器相同的高度? (或与IE JavaScript速度作斗争) - How do I have a TABLE grow to be the same height as its container? (or, battling IE JavaScript slowness) 设置了固定的td单元格高度后,如何在CSS中设置它,但不能兑现? - How do I set fixed td cell height in CSS when I already have it set, but it is not being honored? 如何更改容器高度以匹配图像高度? - How do I change container height to match image height? 我有一个导航栏。 如何将项目的高度设置为导航栏的 100%? - I have a navbar. How do I set the height of the items to be 100% of the navbar? CSS将高度设置为容器的百分比 - CSS Set height as % of container div 设置为高度 100%,使用 scroll-y: hidden,但是我如何让子 div 具有可滚动性? - Div is set to height 100% with scroll-y: hidden, BUT how do i get a child div to have scrollable?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM