要想使一个div里面包含的两个子div并排显示,要用绝对定位还是相对定位?

如题所述

第一:你的定位理解不正确。 使用绝对定位,需要有相对定位来支持,如果单纯使用绝对定位,如果显示器尺寸变化就会出问题,因为绝对定位会去寻找一个初始坐标,不设置的话,默认是去寻找body的0,0坐标来做初始定位坐标。

第二:可使用其他方式来实现,两个div并排显示,可以使用浮动即可。

两种方法具体实现代码如下:
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>CSS3 Demo</title>

<style>

.box{
position: relative;
height: 300px;
width: 800px;
margin: 0px auto;
background: #F9F9F9;
}
.box_01,.box_02{
position: absolute;
height: 280px;
width: 380px;
background: #999999;
top: 10px;
}
.box_01{
left: 10px;
}
.box_02{
right: 10px;
}

.box_other{
padding: 10px;
width: 780px;
margin: 0px auto;
background: #F9F9F9;
}
.crl{
clear: both;
height: 0px;
overflow: hidden;
}
.box_other_01,.box_other_02{
width: 380px;
height: 280px;
background: #999999;
}
.box_other_01{
float: left;
}
.box_other_02{
float: right;
}
</style>
</head>
<body>
<div class="box">
<div class="box_01">01</div>
<div class="box_02">02</div>
</div>
<div class="box_other">
<div class="box_other_01">01</div>
<div class="box_other_02">02</div>
<div class="crl"></div>
</div>

</body>
</html>
温馨提示:答案为网友推荐,仅供参考