Div + CSS:absolute与relative的运用

时间:2008-03-27 13:23:48  来源:  作者:

  Div + CSS 进行网页布局,适当地运用 absolute 与 relative,能给布局带来意想不到的效果和方便,达到事半功倍…本文介绍了关于 absolute 与 relative 的运用。nJy第一天空网络

  详细讲解两者的关系,需要配合例子,请先看例子:nJy第一天空网络

以下是引用片段:nJy第一天空网络
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> nJy第一天空网络
<html xmlns="http://www.w3.org/1999/xhtml"> nJy第一天空网络
<head> nJy第一天空网络
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> nJy第一天空网络
<title>Div + CSS Example, Wayhome's Blog</title> nJy第一天空网络
<style type="text/css"> nJy第一天空网络
<!-- nJy第一天空网络
body,td,th{font-family:Verdana;font-size:9px;} nJy第一天空网络
--> nJy第一天空网络
</style></head> nJy第一天空网络
<body> nJy第一天空网络
<div style="position:absolute; top:5px; right:20px; width:200px; height:180px; background:#00FF00;"> nJy第一天空网络
 position: absolute;<br /> nJy第一天空网络
 top: 5px;<br /> nJy第一天空网络
 right: 20px;<br /> nJy第一天空网络
 <div style="position:absolute; left:20px; bottom:10px; width:100px; height:100px; background:#00FFFF;"> nJy第一天空网络
position: absolute;<br /> nJy第一天空网络
left: 20px;<br /> nJy第一天空网络
bottom: 10px;<br /> nJy第一天空网络
</div> nJy第一天空网络
</div> nJy第一天空网络
<div style="position:absolute; top:5px; left:5px; width:100px; height:100px; background:#00FF00;"> nJy第一天空网络
 position: absolute;<br /> nJy第一天空网络
 top: 5px;<br /> nJy第一天空网络
 left: 5px;<br /> nJy第一天空网络
</div> nJy第一天空网络
<div style="position:relative; left:150px; width:300px; height:50px; background:#FF9933;"> nJy第一天空网络
 position: relative;<br /> nJy第一天空网络
 left: 150px;<br /> nJy第一天空网络
 <br /> nJy第一天空网络
 width: 300px; height: 50px; <br /> nJy第一天空网络
</div> nJy第一天空网络
<div style="text-align:center; background:#ccc;"> nJy第一天空网络
  <div style="margin:0 auto; width:600px; background:#FF66CC; text-align:left;"> nJy第一天空网络
  <p>1</p> nJy第一天空网络
  <p>2</p> nJy第一天空网络
  <p>3</p> nJy第一天空网络
  <p>4</p> nJy第一天空网络
  <p>5</p> nJy第一天空网络
  <div style="padding:20px 0 0 20px; background:#FFFF00;"> nJy第一天空网络
    padding: 20px 0 0 20px; nJy第一天空网络
  <div style="position:absolute; width:100px; height:100px; background:#FF0000;">position: <span style="color:#fff; ">absolute</span>;</div> nJy第一天空网络
  <div style="position:relative; left:200px; width:500px; height:300px; background:#FF9933;"> nJy第一天空网络
    position: <span style="color:blue;">relative</span>;<br /> nJy第一天空网络
   left: 200px;<br /> nJy第一天空网络
   <br /> nJy第一天空网络
   width: 300px;<br /> nJy第一天空网络
   height: 300px;<br /> nJy第一天空网络
   <div style="position:absolute; top:20px; right:20px; width:100px; height:100px; background:#00FFFF;"> nJy第一天空网络
    position: absolute;<br /> nJy第一天空网络
    top: 20px;<br /> nJy第一天空网络
    right: 20px;<br /></div> nJy第一天空网络
   <div style="position:absolute; bottom:20px; left:20px; width:100px; height:100px; background:#00FFFF;"> nJy第一天空网络
    position: absolute;<br /> nJy第一天空网络
  bottom: 20px;<br /> nJy第一天空网络
  left: 20px;<br /> nJy第一天空网络
  </div> nJy第一天空网络
  </div> nJy第一天空网络
  </div> nJy第一天空网络
  nJy第一天空网络
</div> nJy第一天空网络
</div> nJy第一天空网络
</body> nJy第一天空网络
</html> nJy第一天空网络

nJy第一天空网络
  absolute:绝对定位,CSS 写法“ position: absolute; ”,它的定位分两种情况,如下:nJy第一天空网络

  1. 没有设定 Top、Right、Bottom、Left 的情况,默认依据父级的“内容区域原始点”为原始点,上面例子红色部分(父级黄色区域有 Padding 属性,“坐标原始点”和“内容区域原始点”不一样)。nJy第一天空网络

  2. 有设定 Top、Right、Bottom、Left 的情况,这里又分了两种情况如下:nJy第一天空网络

  (1). 父级没 position 属性,浏览器左上角(即 Body)为“坐标原始点”进行定位,位置由 Top、Right、Bottom、Left 属性决定,上面例子绿色部分。nJy第一天空网络

  (2). 父级有 position 属性,父级的“坐标原始点”为原始点,上面例子浅蓝色部分。nJy第一天空网络

  relative:相对定位,CSS 写法“ position: relative; ”,参照父级的“内容区域原始点”为原始点,无父级则以 Body 的“内容区域原始点”为原始点,位置由 Top、Right、Bottom、Left 属性决定,且有“撑开或占据高度”的作用,上面例子橙色部分。nJy第一天空网络

  通过上面的例子和讲解,相信熟练运用 absolute 与 relative 并不是一件很困难的事,我们周围有不少关于 absolute 与 relative 的好例子,比如“网易163免费邮”首页(http://mail.163.com),里面就有大量的运用。nJy第一天空网络

  例子代码在 IE5.5、IE6、FF1.5、Opera9 测试通过。nJy第一天空网络

文章评论

共有 位天空网友发表了评论 查看完整内容

特别推荐
  • 文字广告
  • 文字广告
  • 文字广告
  • 文字广告
站长黑板报