css - 无法让 div 一直向右浮动

标签 css html

当我将#main div 向右浮动时,为什么右边框与标题div 的右边框不对齐?

* {
     margin: 0;
     padding: 0;
}

html, body {
     height: 100%;
}

#wrapper {
     width: 960px;
     height: 100%;
     margin: 0 auto;
}

#header {
     width: 960px;
     height: 70px;
     border: 1px solid black;         
     margin-bottom: 20px;
     margin-top: 20px;
}

#leftcol {
     width: 250px;
     height: 500px;
     border: 1px solid black; 
     float: left;
     margin-right: 20px;          
 }

 #main {  
    width: 686px;
    height: 500px;
    border: 1px solid black;
    float:right;
 }

HTML

<html>
  <body>
    <div id="wrapper">
      <div id="header">
      </div>
      <div id="leftcol">
      </div>  
      <div id="main">
      </div>
    </div><!--end wrapper-->
  </body>
</html>

最佳答案

正如@alfonso 指出的那样,边框会增加 div 的实际大小。

最好在所有带边框的元素上使用 box-sizing: border-box,这样边框就在里面了。对齐变得更加容易。

关于css - 无法让 div 一直向右浮动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12064829/

相关文章:

html - 无法设置 Ajax 脚本内容的样式(nowrap 不起作用?)

html - CSS::before 伪元素似乎不起作用。我似乎无法找出原因,也无法在线获得解决方案

css - 调整 imagem 与 2 div 固定(页眉和页脚)

html - ipad 上意外的 DIV 边框线

javascript - 无法同时调整静态和可拖动元素的大小

php - PHP从文本 explode URL,可能吗?

css - 从 css 样式中排除组件

css - 使用 `-webkit-text-fill-color: transparent` 和 `color: transparent` 有区别吗?

jquery - 如何在悬停时使用 CSS 中属性的值更改元素的背景颜色

html - 当 P 是相对的儿子时,将绝对父 P 拉伸(stretch)到其子宽度