<html>
<head>
<title>position属性</title>
<styletype="text/css">
<!--
body{
margin:10px;
font-family:Arial;
font-size:13px;
}
#father{
background-color:#a0c8ff;
border:1pxdashed#000000;
width:100%;
height:100%;
}
#block{
background-color:#fff0ac;
border:1pxdashed#000000;
padding:10px;
position:absolute; /* absolute绝对定位 */
left:20px; /* 块的左边框离页面左边界20px */
top:40px; /* 块的上边框离页面上边界40px */
}
-->
</style>
</head>
<body>
<div id="father">
<div id="block">absolute</div>
</div>
</body>
</html>
绝对定位 百分比:
<html>
<head>
<title>position属性</title>
<styletype="text/css">
<!--
body{
margin:10px;
font-family:Arial;
font-size:13px;
}
#father{
background-color:#a0c8ff;
border:1pxdashed#000000;
width:100%;
height:100%;
}
#block{
background-color:#fff0ac;
border:1pxdashed#000000;
padding:10px;
position:absolute; /* absolute绝对定位 */
right:75%; /* 块的右边框离页面右边界70% */
bottom:10%; /* 块的下边框离页面下边界10% */
}
-->
</style>
</head>
<body>
<div id="father">
<div id="block">absolute</div>
</div>
</body>
</html>
同时设置四个位置
<html>
<head>
<title>position属性</title>
<styletype="text/css">
<!--
body{
margin:10px;
font-family:Arial;
font-size:13px;
}
#father{
background-color:#a0c8ff;
border:1pxdashed#000000;
width:100%;
height:100%;
}
#block{
background-color:#fff0ac;
border:1pxdashed#000000;
padding:10px;
position:absolute; /* absolute绝对定位 */
right:20px; left:30px; /* 同时设置4个位置 */
bottom:10%; top:10%;
}
-->
</style>
</head>
<body>
<div id="father">
<div id="block">absolute</div>
</div>
</body>
</html>
绝对定位,即使是有父div,absolute永远只是相对body绝对定位
<html>
<head>
<title>position属性</title>
<styletype="text/css">
<!--
body{
margin:10px;
font-family:Arial;
font-size:13px;
}
#father{
background-color:#a0c8ff;
border:1pxdashed#000000;
width:100%;
height:100%;
padding:5px;
}
#block1{
background-color:#fff0ac;
border:1pxdashed#000000;
padding:10px;
position:absolute; /* absolute绝对定位 */
left:30px;
top:35px;
}
#block2{
background-color:#ffbd76;
border:1pxdashed#000000;
padding:10px;
}
-->
</style>
</head>
<body>
<div id="father">
<div id="block1">absolute</div>
<div id="block2">block2</div>
</div>
</body>
</html>
相对定位:
一个元素
<html>
<head>
<title>position属性</title>
<styletype="text/css">
<!--
body{
margin:10px;
font-family:Arial;
font-size:13px;
}
#father{
background-color:#a0c8ff;
border:1pxdashed#000000;
width:100%; height:100%;
padding:5px;
}
#block1{
background-color:#fff0ac;
border:1pxdashed#000000;
padding:10px;
position:relative; /* relative相对定位 */
left:15px; /* 子块的左边框距离它原来的位置15px */
top:10%;
}
-->
</style>
</head>
<body>
<div id="father">
<div id="block1">relative</div>
</div>
</body>
</html>
相对定位 :两个元素
先按照去除相对定位显示之后,加了相对定位的元素再按照left top等属性移动位置:
不加relative
加了 relative后:
block2还是不变
<html>
<head>
<title>position属性</title>
<styletype="text/css">
<!--
body{
margin:10px;
font-family:Arial;
font-size:13px;
}
#father{
background-color:#a0c8ff;
border:1pxdashed#000000;
width:100%; height:100%;
padding:5px;
}
#block1{
background-color:#fff0ac;
border:1pxdashed#000000;
padding:10px;
position:relative; /* relative相对定位 */
left:15px; /* 子块的左边框距离它原来的位置15px */
top:10%;
}
#block2{
background-color:#ffc24c;
border:1pxdashed#000000;
padding:10px;
}
-->
</style>
</head>
<body>
<div id="father">
<div id="block1">relative</div>
<div id="block2">block2</div>
</div>
</body>
</html>
如果两个元素都是相对布局,各自按照自身移动,两者互不影响:
<html>
<head>
<title>position属性</title>
<styletype="text/css">
<!--
body{
margin:10px;
font-family:Arial;
font-size:13px;
}
#father{
background-color:#a0c8ff;
border:1pxdashed#000000;
width:100%; height:100%;
padding:5px;
}
#block1{
background-color:#fff0ac;
border:1pxdashed#000000;
padding:10px;
position:relative; /* relative相对定位 */
left:15px; /* 子块1的左边框距离它原来的位置15px */
top:30px; /* 子块1的左边框距离它原来的位置30px */
}
#block2{
background-color:#ffc24c;
border:1pxdashed#000000;
padding:10px;
position:relative; /* relative相对定位 */
left:10px; /* 子块2的左边框距离它原来的位置10px */
top:20px; /* 子块2的上边框距离它原来的位置15px */
}
-->
</style>
</head>
<body>
<div id="father">
<div id="block1">relative</div>
<div id="block2">block2</div>
</div>
</body>
</html>