CSS中相对定位

来源:互联网 发布:软件源码提取 编辑:程序博客网 时间:2024/05/03 19:16

    相对定位是按照元素自身所在的位置,使用边偏移属性重新定义元素的显示位置,使用相对定位的元素依然是文档中的元素,元素的显示位置和

元素所在文档中其他元素相互关联。

   在确定相对定位元素位置的时候,首先要确定元素的原始位置,即元素在文档中的位置,然后根据偏移属性中定义的偏移值,确定元素的最终位置。

   

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html> <head>  <title> New Document </title>  <meta name="Generator" content="EditPlus">  <meta name="Author" content="">  <meta name="Keywords" content="">  <meta name="Description" content="">  <style>    .div1 {  background: #333333;  width: 200px;  height: 100px;}.relative {  position: relative;  top: 50px;  left: 100px;  width: 400px;  height: 100px;  background: #666666;}  </style> </head> <body>  <div class="div1"></div>  <div class="relative">这是相对定位到元素</div> </body></html>



   从上图可以看出,使用相对定位的元素按照自身所在的位置进行偏移。

   在使用相对定位到时候,相对定位元素保留原来所占有的空间,同时自身按照边偏移属性中定义的属性值进行偏移(有可能覆盖其他元素),

与相对定位元素相邻的元素会将相对定位元素进行排列。

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html> <head>  <title> New Document </title>  <meta name="Generator" content="EditPlus">  <meta name="Author" content="">  <meta name="Keywords" content="">  <meta name="Description" content="">  <style>    .div1 {  background: #333333;  width: 200px;  height: 100px;}.relative {  position: relative;  top: 50px;  left: 100px;  width: 400px;  height: 100px;  background: #666666;}    .div2 { background: #999999; width: 200px; height: 100px;}  </style> </head> <body>  <div class="div1"></div>  <div class="relative">这是相对定位到元素</div>  <div class="div2"></div> </body></html>


    从上图看出,与相对定位元素相邻的元素的显示方式,会保留相对元素原来占有的空间,使用相对定义的元素,由于其显示级别高于

普通元素,所以相对定位元素覆盖了普通页面元素。











原创粉丝点击