CSS如何使用position属性

2023-05-23,

这篇文章主要介绍了CSS如何使用position属性的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇CSS如何使用position属性文章都会有所收获,下面我们一起来看看吧。

position属性有4个值:
默认是static,无特殊定位,对象遵循HTML定位规则(一般使用margin、padding)
fixed未支持。对象定位遵从绝对(absolute)方式。但是要遵守一些规范
absolute绝对定位(相对于最近的父元素,假如父元素都是默认的static,那么将相对body进行定位,如果父元素为relative定位,那么就相对该父元素进行定位)
relative相对定位(相对于自己,在原来位置上移动。与static相比,top、bottom、left、rigth生效,而static不生效)
relative不脱离文档流,而absolute脱离文档流。
例子:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="content-type" content="text/html; charset=utf-8"> 
<title>position</title> 
<style type="text/css"> 
<!-- 
body{ 
font-size:12px; 
margin:0 auto; 
} 
div#demo{ 
position:relative; 
border:1px solid #000; 
margin:50px; 
top:-50px; 
line-height:18px; 
overflow:hidden; 
clear:both; 
height:1%; 
} 
div#sub{ 
position:absolute; 
right:10px; 
top:10px; 
} 
div.relative{ 
position:relative; 
left:400px; 
top:-20px; 
} 
div.static,div.fixed,div.absolute,div.relative{ 
width:300px; 
} 
div.static{ 
background-color:#bbb; 
position:static; 
} 
div.fixed{ 
background-color:#ffc0cb; 
} 
div.absolute{ 
background-color:#b0c4de; 
} 
div.relative{ 
background-color:#ffe4e1; 
} 
--> 
</style> 
</head> 
<body> 
<div id="demo"> 
<div class="static">static: 默认值。无特殊定位,对象遵循HTML定位规则 </div> 
<div id="sub" class="absolute">absolute: 将对象从文档流中拖出,使用left,right,top,bottom 等属性相对于其最接近的一个最有定位设置的父对象进行绝对定位。如果不存在这样的父对象,则依据body对象。而其层叠通过z-index属性定义 </div> 
<div class="fixed">fixed:未支持。对象定位遵从绝对(absolute)方式。但是要遵守一些规范 </div> 
<div class="relative">relative:对象不可层叠,但将依据 left,right,top,bottom 等属性在正常文档流中偏移位置 </div> 
</div> 
</body> 
</html>

css的选择器有哪些

css的选择器可以分为三大类,即id选择器、class选择器、标签选择器。它们之间可以有多种组合,有后代选择器、子选择器、伪类选择器、通用选择器、群组选择器等等

关于“CSS如何使用position属性”这篇文章的内容就介绍到这里,感谢各位的阅读!相信大家对“CSS如何使用position属性”知识都有一定的了解,大家如果还想学习更多知识,欢迎关注本站行业资讯频道。

《CSS如何使用position属性.doc》

下载本文的Word格式文档,以方便收藏与打印。