DIV+CSS 让左右结构内容之间有一定距离

时间:2021-04-16

  一般我们在一个外围DIV内针对两个DIV分别使用float 浮动属性(float:left(局左)、float:right(居右))来解决此问题。

  这样的布局一般总的宽度一定,只需左、右内容DIV宽度设置小于总宽度即可实现,

  注意的是一个DIV的宽度计算公式是:自己设置宽度+边框宽度(border)+padding宽度+margin宽度组成。

  左右DIV的距离为:外围DIV宽度-左边DIV宽度-右边DIV宽度

  提示:在DIV CSS制作中很多时候需要计算的如这样的布局。

  代码为:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd>
<html xmlns=http://www.w3.org/1999/xhtml>
<style type="text/css">
<!--页面内容距离浏览器顶端及左边空隙为0-->
html, body {
margin: 0;
padding: 0;
}

#Box{
width:200px;
height:72px;
background-color:#666;
}
<!--Box-a的宽度为50+5(margin-left)+2(左右border宽度和)=57-->
#Box-a{
float:left;
width:50px;
border:1px solid #999;
height:60px;
background-color:#00F;
margin-left:5px;
margin-bottom:5px;
margin-top:5px;
}
<!--Box-b的宽度为120+5(margin-right)+2(左右border宽度和)=127-->
#Box-b{
float:right;
width:120px;
border:1px solid #999;
height:60px;
background-color:#F03;
margin-right:5px;
margin-top:5px;
margin-bottom:5px;
}
</style>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>DIV+CSS 让左右结构内容之间有一定距离</title>
</head>

<body>
<!--外围的DIV-->
<div id="Box">
<div id="Box-a"></div><!--左边的DIV-->
<div id="Box-b"></div><!--右边的DIV-->
</div>
</body>
</html>

  分别计算出Box-a和Box-b的宽度后,可得Box-a与Box-b之间的宽度为200-57-127=16px。

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章