31、CSS定位position之fixed&sticky

固定定位:
  • 将元素的position的属性设置为fixed则开启了元素的固定定位
  • 固定定位也是一种绝对定位,所以固定定位的大部分特点和绝对定位是一样的

唯一的不同点就是不同的固定定位永远参照与浏览器的是口进行定位

粘滞定位:
  • 当元素的position属性设置为sticky时则开启了元素的粘滞定位
  • 粘滞定位和相对定位的特点基本一致。

不同的是粘滞定位可以在元素到达某个位置时将其固定。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>fixed&sticky</title>
    <link rel="stylesheet" href="../css/reset.css">
    <style>
        /* 
        固定定位(fixed):
            - 将元素的position的属性设置为fixed则开启了元素的固定定位
            - 固定定位也是一种绝对定位,所以固定定位的大部分特点和绝对定位是一样的
                唯一的不同点就是不同的固定定位永远参照与浏览器的是口进行定位

        粘滞定位(sticky):
            - 当元素的position属性设置为sticky时则开启了元素的粘滞定位
            - 粘滞定位和相对定位的特点基本一致。
            不同的是粘滞定位可以在元素到达某个位置时将其固定。
         */
        body{
            background-color: #777777;
            height: 2000px;
        }
        .box{
            height: 448px;
            background-color: #bac;
             position :relative; 
        }
        .box1{
            width: 1230px;
            height: 48px;
            background-color: #e8e7e3;
            margin: 200px auto;
            /* position:sticky; */
            position :fixed;
            right: 0;
            left: 0;
            /* top: 0px; */
        }
        .box1 li{
            float: left;
            /* height: 100%; */
            line-height: 48px;
            /* background-color: red; */
        }
        .box1 li a{
            display: block;
            height: 100%;
            padding:0 44px;
            text-decoration: none;
           color: #777777;
           font-size: 19px;
        }
        .box1 li a:hover{
            color: #e8e7e3;
            background-color: #3f3f3f;
        }
        .box1 li:first-child a{
            padding:0 44px 0 44px;
        }
    </style>
</head>
<body>
    <div class="box">
        <ul class="box1">
            <li><a href="https://www.baidu.com">html/css</a></li><li><a href="https://www.baidu.com">browserside</a></li><li><a href="https://www.baidu.com">xml</a></li> <li><a href="https://www.baidu.com">refrence</a></li> <li><a href="https://www.baidu.com">webbuilding</a></li><li><a href="https://www.baidu.com">programming</a></li><li><a href="https://www.baidu.com">serverside</a></li>  
        </ul>
    </div>
</body>
</html>
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 定位(position)的简介 定位是一种更加高级的布局手段 通过定位可以将元素摆放到页面的任意位置 使用posi...
    Syaroing阿樱阅读 479评论 0 2
  • 1.定位的简介 定位(position) 定位是一种更加高级的布局手段 通过定位可以将元素摆放到页面的任意位置 使...
    小海怪的互联网阅读 314评论 0 3
  • 1. 简介 1. 定义 定位是一种更加高级的布局手段 通过定位可以将元素摆放到页面的任意位置 使用position...
    cxq要努力阅读 233评论 0 0
  • 五、定位 1、定位的简介 定位(position),定位是一种更加高级的布局手段,通过定位可以将元素摆放到页面的任...
    西北有高楼lee阅读 546评论 0 0
  • 在CSS中,使用position属性可以改变元素的定位方式,其取值有以下几种: static relative ...
    shandamengcheng阅读 621评论 0 1