zoukankan      html  css  js  c++  java
  • position:relative

    "对于页面中一个static的div#demo,我想让这个#demo里的一个div#sub相对于#demo定位在右上角的某个地方,应该是用这个position:relative这个吧,然后再加上top,right给限制一下。 
    我理解得没有错吧? " 

    首先对你疑惑的地方,我先解答一下: 
    position的默认值是static,(也就是说对于任意一个元素,如果没有定义它的position属性,那么它的position:static) 

    如果你想让这个#demo里的一个div#sub相对于#demo定位在右上角的某个地方,应该给#demo相对定位,#sub绝对定位。 

    absolute是相对于自己最近的父元素来定位的,如果你不给#demo相对定位,那么#sub的绝对定位就是相对于body来定位的。 

    relative是相对于自己来定位的,例如:#demo{position:relative;top:-50px;},这时#demo会在相对于它原来的位置上移50px。 

    另:relative 不脱离文档流,absolute 脱离文档流。也就是说: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{
            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>

  • 相关阅读:
    字段修改名称
    coercing to Unicode: need string or buffer, geoprocessing value object found
    为什么ArcGIS 10.3导出 Shapefile的字段名会被截断成3个汉字?解决方法如下
    arcgis python 使用光标和内存中的要素类将数据加载到要素集 学习:http://zhihu.esrichina.com.cn/article/634
    arcgis python 获得arcgis安装版本和安装位置
    arcgis python 不知道一个工具怎么用
    arcgis 地理坐标系 699个,投影坐标系是4767
    arcgis python 参数类型和含义
    win10 svn commit无响应
    新建网站与新建Asp.Net Web 应用程序的区别
  • 原文地址:https://www.cnblogs.com/danghuijian/p/4400142.html
Copyright © 2011-2022 走看看