CSS定位

来源:互联网 发布:日语红宝书软件下载 编辑:程序博客网 时间:2024/05/21 22:51

2013-01-09 14:30289人阅读评论(0)收藏举报

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

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

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

[html]view plaincopyprint?

<!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> 

10 .div1 {

11 background: #333333;

12 width: 200px;

13 height: 100px;

14 }

15 

16 .relative {

17 position: relative;

18 top: 50px;

19 left: 100px;   此属性设置,只是相对于他原先的位置,并不是以父元素的位置未起点

20 width: 400px;

21 height: 100px;

22 background: #666666;

23 }

24 

25 </style> 

26 </head> 

27 

28 <body> 

29 <div class="div1"></div> 

30 <div class="relative">这是相对定位到元素</div> 

31 </body> 

32 </html> 



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

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

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

[html]view plaincopyprint?

33 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 

34 <html> 

35 <head> 

36 <title> New Document</title> 

37 <meta name="Generator" content="EditPlus"> 

38 <meta name="Author" content=""> 

39 <meta name="Keywords" content=""> 

40 <meta name="Description" content=""> 

41 <style> 

42 .div1 {

43 background: #333333;

44 width: 200px;

45 height: 100px;

46 }

47 

48 .relative {

49 position: relative;

50 top: 50px;

51 left: 100px;

52 width: 400px;

53 height: 100px;

54 background: #666666;

55 }

56 

57 .div2 {

58 background: #999999;

59 width: 200px;

60 height: 100px;

61 }

62 

63 </style> 

64 </head> 

65 

66 <body> 

67 <div class="div1"></div> 

68 <div class="relative">这是相对定位到元素</div> 

69 <div class="div2"></div> 

70 </body> 

71 </html> 


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

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

0 0
原创粉丝点击