如何让一个div在上,两个div在下
时间: 2023-05-31 11:03:54 浏览: 73
您可以使用 CSS 的布局属性来实现这个效果。以下是一种可能的方法:
HTML 代码:
```
<div class="top">这是上面的div</div>
<div class="bottom">
<div class="left">这是下面的左边div</div>
<div class="right">这是下面的右边div</div>
</div>
```
CSS 代码:
```
.top {
position: absolute;
top: 0;
left: 0;
right: 0;
height: 100px;
background-color: #ccc;
}
.bottom {
position: absolute;
bottom: 0;
left: 0;
right: 0;
height: 200px;
}
.left {
float: left;
width: 50%;
height: 100%;
background-color: #eee;
}
.right {
float: right;
width: 50%;
height: 100%;
background-color: #ddd;
}
```
解释:
- `position: absolute;` 和 `top: 0; left: 0; right: 0;` 把 `.top` 元素定位到页面的顶部,宽度为整个页面宽度,高度为 100px。
- `.bottom` 元素也是绝对定位,但是把它定位到页面底部。
- `.left` 和 `.right` 元素使用浮动布局,分别占据 `.bottom` 元素的左半部分和右半部分,高度为整个 `.bottom` 元素的高度。因为浮动元素脱离了正常文档流,所以需要给 `.bottom` 元素设置 `height: 200px;` 来撑开它的高度,以便让里面的浮动元素正常排列。