CSS:float的小问题

问题描述

|                                                                                                                       

解决方法

        
#sidebar { 
        display:block; 
        float:left; 
        width:170px; 
        height:auto; 
        min-height:400px;
        background:#f9f9f9;
        border: solid 1px #eaeaea;
        border-radius:10px;
        -moz-border-radius:10px;
}

#main { display:block; float:right; width:766px; height:auto; min-height:400px; background:red; }
使用此CSS。它会工作。     ,        站点栏div的宽度为20%,但两侧的边框均为1px。这2个像素被添加到20%的内部宽度中,因此主div不会留下80%的宽度。 因此,将main的宽度设置为79%,或添加:
margin: 0 -1px 0 -1px;
但是,既然您给容器指定了固定的宽度,那么为什么要按百分比设置样式呢?     ,        我通过将红色容器的宽度设置为
width: 79%
来解决此问题。不知何故,当您设置它时,它会溢出,因此两者都达到100%。 也许最好在这里工作像素完美并放像素值而不是使用百分比,因为容器的宽度始终是固定的。 希望对您有帮助。