CSS定位:position属性

css positon属性

 一、什么是css position属性?
         css positon属性:用于指定一个元素在文档中的定位方式,top、right、bottom、left属性则决定了该元素的最终位置。
二、css position属性有哪些属性值?
         css position属性值:
                static:默认值。没有定位,元素出现在正常的流中(忽略top,bottom,left,right或者z-index声明)。
                absolute:生成绝对定位的元素、相对于static定位以外的第一个父元素进行定位。元素的位置通过 ”left” , ”top” , “right” 以及 “bottom” 属性进行规定。
                relative:生成相对定位的元素,相对于其正常位置进行定位。因此,”left: 20px” 会向元素的left位置添加20像素。
                fixed:生成绝对定位的元素,相对于浏览器窗口进行定位。元素的位置通过 ”left” , ”top” , “right” 以及 “bottom” 属性进行规定。
                inherit:规定应该从父元素继承position属性的值。
三、position各个属性值的用处。
        position: relative;
        相对定位:会按照元素的原始位置对该元素进行移动。

        如图示:

具体代码书写示例:

<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title></title>
    <style type="text/css">
        .wrap {
            width: 500px;
            margin: 0 auto;
            border: 5px solid #000;
        }
        
        h2.pos_left {
            position: relative;
            left: -20px;
            background: #f0f;
        }
        
        h2.pos_right {
            position: relative;
            left: 20px;
            background: #0f0;
        }
    </style>
</head>

<body>
    <div class="wrap">
        <h2>这是位于正常位置的标题</h2>
        <h2 class="pos_left">这个标题相对于其正常位置向左移动</h2>
        <h2 class="pos_right">这个标题相对于其正常位置向右移动</h2>
        <p>相对定位会按照元素的原始位置对该元素进行移动。</p>
        <p>样式 "left:-20px" 从元素的原始左侧位置减去 20 像素。</p>
        <p>样式 "left:20px" 向元素的原始左侧位置增加 20 像素。</p>
    </div>
</body>

</html>
position: absolute;
绝对定位:通过绝对定位,元素可以放置到页面上的任何位置。

如图示:

具体代码书写示例:

<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title></title>
    <style type="text/css">
        .wrap {
            width: 500px;
            height: 300px;
            margin: 50px auto;
            border: 5px solid #000;
            position: relative;
        }
        
        h2.pos_abs {
            position: absolute;
            left: 100px;
            top: 150px;
            background: #f00;
        }
    </style>
</head>

<body>
    <div class="wrap">
        <h2 class="pos_abs">这是带有绝对定位的标题</h2>
        <p>通过绝对定位,元素可以放置到页面上的任何位置。下面的标题距离页面左侧 100px,距离页面顶部 150px。</p>
    </div>
</body>

</html>
position: fixed;
固定定位:将元素固定在浏览器窗口的某个位置。

如图示:

具体代码书写示例:

<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title></title>
    <style type="text/css">
        .wrap {
            width: 500px;
            margin: 50px auto;
            border: 5px solid #000;
        }
        
        h2.pos_abs {
            position: fixed;
            left: 100px;
            top: 100px;
            background: #f00;
        }
    </style>
</head>

<body>
    <div class="wrap">
        <h2 class="pos_abs">这是带有固定定位的标题</h2>
        <p>通过固定定位,元素可以放置到浏览器窗口的任何位置。下面的标题距离浏览器窗口左侧 100px,距离窗口顶部 100px。</p>
    </div>
</body>

</html>
position: static;
默认值。没有定位,元素出现在正常的流中(忽略 top, bottom, left, right 或者 z-index 声明)。
position: inherit;
规定应该从父元素继承 position 属性的值。

补充:position定位属性定义建立元素布局所用的定位机制。任何元素都可以定位,不过==绝对==或==固定==元素会生成一个==块级框==,而不论该元素本身是什么类型。相对定位元素会相对于它在正常流中的默认位置偏移。

猜你喜欢

转载自www.cnblogs.com/cd-wang/p/12386694.html