css - 如何在 HTML 中使用 float 正确定位它们?

标签 css html css-float

我正在尝试使用这段 CSS 代码制作三种类型的 block :

div#menu {
width:200px;
height:400px;
border:2px ridge white;
text-align:center;
float:left;
margin:4px;
}
div#container {
width:850px;
height:850px;
overflow:auto;
margin:auto;
text-align:center;
border:2px ridge green;
}
div#maincontent {
width:608px;
height:700px;
border:2px ridge white;
text-align:center;
float:right;
top:10px;
margin:4px;
}
div#subcontent {
width:200px;
height:100px;
border:2px ridge white;
text-align:center;
float:left;
margin:4px;
clear:both;
}

容器将容纳整个元素/页面。

在容器内,菜单位于左上角,子内容框位于菜单正下方(左下角)。主内容框位于右上角,就在菜单/子内容框的右侧。

我在 HTML 中是这样安排的:

<div id="container">
<div id="menu"></div>
<div id="subcontent"></div>
<div id="maincontent"></div>
</div>

但是,当我运行它时,它会使主内容框的顶部与子内容框的顶部对齐,而不是与菜单对齐。

如何让它 float 到右上角,而不是恰好在右边?

最佳答案

http://jsfiddle.net/7zvSu/

使您的 mainContent float:left(删除 top 属性)。并将您的主要内容 div 放在菜单后面。

CSS:

div#menu {
    width:200px;
    height:400px;
    border:2px ridge white;
    text-align:center;
    float:left;
    margin:4px;
}
div#container {
    width:850px;
    height:850px;
    overflow:auto;
    margin:auto;
    text-align:center;
    border:2px ridge green;
}
div#maincontent {
    width:608px;
    height:700px;
    border:2px ridge white;
    text-align:center;
    float:left;
    margin:4px;
}
div#subcontent {
    width:200px;
    height:100px;
    border:2px ridge white;
    text-align:center;
    float:left;
    margin:4px;
    clear:both;
}

html

<div id="container">
  <div id="menu"></div>
  <div id="maincontent"></div>
  <div id="subcontent"></div>
</div>

关于css - 如何在 HTML 中使用 float 正确定位它们?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6741267/

相关文章:

html - 第一个动态添加的组件没有从类中获取样式

HTML 链接 <a> 文本换行突破其边界

html - 如何在html中将图像放在背景中

html - jsfiddle layout broken : wrong input size. 大小存储在哪里?

html - 当元素 float 时,div 占用的宽度超出了所需的宽度

css - 如何在两个不同的相交 div 上保留 Logo ?

javascript - jQuery onClick 事件有效,但单击文本时除外

python - Django 给出 "GET/static/css/style.css HTTP/1.1"304 0

google-chrome - chrome css 转换问题 : element in front of rotated element doesn't respond well to styling & clicks

c# - 不使用javascript的textarea的maxlength属性