其实话说一直以来也没真正去理解好position:relative的用法的真实意义。

  我想很多人实实在在用的多都是position:relative和position:absolute结合起来一起用的。

  position属性是用四种定位。默认的是static。

  position:absolute(绝对定位) ——是脱离文档流,相对于父级元素(包含这个position:relative)定位,当然如果没有,那就是相对于body定位的。

  position:relative(相对定位) ——单独使用,我不知道很多人是不是也跟我一样忽略过它,relative 也是不脱离文档流,“这个元素会偏移某个距离。但是该元素仍保持其未定位前的形状,它原本所占的空间仍保留。”,它是相对于自己来定位的,例如:#main{position:relative;top:-50px;},这时#main会在相对于它原来的位置上移50px。 

  position:fixed(固定定位)  —— 跟绝对定位有点类似,只是它的父级元素永远都是视窗本身。

  

<!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: auto;
} div#demo{
position:relative;
border:1px solid #;
margin:50px;
top:-50px;
line-height:18px;
overflow:hidden;
clear:both;
height:%;
} 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中position:relative的真正理解的更多相关文章

  1. CSS中"position:relative"属性与文档流的关系

    前言 近期遇到一个问题--"position:relative"到底会不会导致元素脱离文档流?主流观点是不会,但都给不出一个有说服力的论据.最后我自己佐证了一番,总算有了个结果:& ...

  2. 深入理解css中position属性及z-index属性

    深入理解css中position属性及z-index属性 在网页设计中,position属性的使用是非常重要的.有时如果不能认识清楚这个属性,将会给我们带来很多意想不到的困难. position属性共 ...

  3. 深入理解css中position属性及z-index属性 https://www.cnblogs.com/zhuzhenwei918/p/6112034.html

    深入理解css中position属性及z-index属性 请看出处:https://www.cnblogs.com/zhuzhenwei918/p/6112034.html 在网页设计中,positi ...

  4. CSS中Position属性static、absolute、fixed、relative

    在html中网页可以看成一个立体的空间,一个完整的页面是由很多个页面堆积形成的,如下图所示   CSS中Position属性有四个可选值,它们分别是:static.absolute.fixed.rel ...

  5. CSS中Position属性

    也许你看到这个标题觉得很简单,确实这是一篇关于CSS中Position属性基础知识的文章,但是关于Position的一些细节也许你不了解. 1.简介 position有五个属性: static | r ...

  6. CSS中position和header和overflow和background

    <!DOCTYPE html> <!--CSS中position属性--> <html lang="en"> <head> < ...

  7. css中position属性(absolute|relative|static|fixed)概述及应用

    position属性的相关定义: static:无特殊定位,对象遵循正常文档流; relative:对象遵循正常文档流; absolute:对象脱离正常文档流 fixed:对象脱离正常文档流 我们先来 ...

  8. 【转】CSS中position属性( absolute | relative | static | fixed )详解

    我们先来看看CSS3 Api中对position属性的相关定义: static:无特殊定位,对象遵循正常文档流.top,right,bottom,left等属性不会被应用. relative:对象遵循 ...

  9. CSS中position属性( absolute | relative | static | fixed )详解

    我们先来看看CSS3 Api中对position属性的相关定义: static:无特殊定位,对象遵循正常文档流.top,right,bottom,left等属性不会被应用. relative:对象遵循 ...

随机推荐

  1. Apache代理和反向代理

    服务器上安装了多个服务,包括apache的80端口,以及tomcat的8080和8090,为了访问使用方便,尝试了代理和反向代理.下面是部分配置以备参考: NameVirtualHost *:80 & ...

  2. <%@page include%>、<%@include%>、<jsp:include>三者之间的本质区别

    <%@page include%>.<%@include%>.<jsp:include>三者之间的本质区别 先从它的几个内置对象说起. application和se ...

  3. 官方 Animator 例子解析 Animator.MatchTarget

    一.官方的解释 Animator.MatchTargetSwitch to Manual ); Parameters matchPosition The position we want the bo ...

  4. Palindrome Partitioning II

    Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...

  5. HDU1251 统计难题 Trie树

    题目很水,但毕竟是自己第一道的Trie,所以还是发一下吧.Trie的更多的应用慢慢学,AC自动机什么的也慢慢学.... #include<iostream> #include<cst ...

  6. IT 基础设施

    http://www.cnblogs.com/wintersun/p/4355267.html

  7. lintcode:1-10题

    难度系数排序,容易题1-10题: Cosine Similarity new  Fizz Buzz  O(1)检测2的幂次  x的平方根  不同的路径  不同的路径 II  两个字符串是变位词  两个 ...

  8. spring利用注解来注册bean到容器

    1.spring利用注解来定义bean,或者利用注解来注册装配bean.包括注册到ioc中,装配包括成员变量的自动注入. 1.spring会自动扫描所有类的注解,扫描这些注解后,spring会将这些b ...

  9. Hibernate逍遥游记-第12章 映射值类型集合-003映射List(<list-index>)

    1. <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hi ...

  10. C# Java间进行RSA加密解密交互(二)

    原文:C# Java间进行RSA加密解密交互(二) 接着前面一篇文章C# Java间进行RSA加密解密交互,继续探讨这个问题. 在前面,虽然已经实现了C# Java间进行RSA加密解密交互,但是还是与 ...