css各种定位问题

1.定位的简介和相对定位

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        body{
            font-size: 60px;
        }
        .box1{
            width: 200px;
            height: 200px;
            background-color: palegreen;
        }
        .box2{
            width: 200px;
            height: 200px;
            background-color: rgb(243, 82, 8);
 /* 定位
    -定位是一种更加高级的布局手段
    -通过定位可以将一个元素摆放到页面的任意位置
    -使用position属性来设置定位
    可选值:
    static 默认值,元素禁止,未开启定位
    relative 开启元素的相对定位
    absolute 开启元素的绝对定位
    fixed 开启元素的固定定位
    sticky 开启元素的粘滞定位 
                   
相对定位:将position设置为relative,设置相对定位一定要设置偏移量(偏移量就是确定元素的位置)
   top  bottom left right
   top是顶上的距离,其他类似*/
   position: relative;
   left: 200px;
   top: -200px;
}
        .box3{
            width: 200px;
            height: 200px;
            background-color: rgb(13, 65, 236);
        }
    </style>
</head>
<body>
    <div class="box1">1</div>
    <div class="box2">2</div>
    <div class="box3">3</div>
</body>
</html>

2.绝对定位

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        body{
            font-size: 60px;
        }
        .box1{
            width: 200px;
            height: 200px;
            background-color: palegreen;
        }
        .box2{
            width: 200px;
            height: 200px;
            background-color: rgb(243, 82, 8);
 /* 
                   
绝对定位:将position设置为absolute
  特点:1.开启绝对定位后,如果不设置偏移量,元素位置不会发生变化
        2.开启绝对定位后,元素会从文档流中脱离
        3.绝对定位会改变元素的性质,行内变成块,块的宽度被内容撑开
        4.绝对定位会使元素提升一个层级
        5.绝对定位元素是相对于其包含块进行定位

        包含块:
           -正常情况下就是离当前元素最近的祖先块元素
           -开启绝对定位的情况下,就是离他最近的开启了定位的祖先元素
           如果所有的祖先元素都没有开启定位则根元素就是他的包含块
 */
   position: absolute;
   left: 0;
   top: 0;
}
        .box3{
            width: 200px;
            height: 200px;
            background-color: rgb(13, 65, 236);
        }
        .box4{
            width: 400px;
            height: 400px;
            background-color: paleturquoise;
            /* position: relative; */
        }
        .box5{
            width: 300px;
            height: 300px;
            background-color: yellow;
            /* position: relative; */
        }
    </style>
</head>
<body>
    <div class="box1">1</div>
    <div class="box4">4
        <div class="box5">5
            <div class="box2">2
        </div>
    </div>
</div>
    <div class="box3">3</div>
</body>
</html>

3.固定定位

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        body{
            font-size: 60px;
            height: 2000px;
        }
        .box1{
            width: 200px;
            height: 200px;
            background-color: palegreen;
        }
        .box2{
            width: 200px;
            height: 200px;
            background-color: rgb(243, 82, 8);
 /*                 
固定定位:将position设置为fixed
        固定定位也是一种绝对定位,特点基本一样
        唯一不同的是固定定位永远参照于浏览器的视口进行定位(固定在一个位置)
        固定定位的元素不会随着网页的滚动条滚动
 */
   position: fixed;
   left: 0;
   top: 0;
}
        .box3{
            width: 200px;
            height: 200px;
            background-color: rgb(13, 65, 236);
        }
        .box4{
            width: 400px;
            height: 400px;
            background-color: paleturquoise;
        }
        .box5{
            width: 300px;
            height: 300px;
            background-color: yellow;
        }
    </style>
</head>
<body>
    <div class="box1">1</div>
    <div class="box4">4
        <div class="box5">5
            <div class="box2">2
        </div>
    </div>
</div>
    <div class="box3">3</div>
</body>
</html>

4.粘滞定位

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        body{
            font-size: 60px;
            height: 2000px;
        }
        .box1{
            width: 200px;
            height: 200px;
            background-color: palegreen;
        }
        .box2{
            width: 200px;
            height: 200px;
            background-color: rgb(243, 82, 8);
 /*                 
粘滞定位:将position设置为sticky
        粘滞定位可以在元素到达某个位置时将其固定不动
 */
   position: sticky;
   top: 0px;
}
        .box3{
            width: 200px;
            height: 200px;
            background-color: rgb(13, 65, 236);
        }
        .box4{
            width: 400px;
            height: 400px;
            background-color: paleturquoise;
        }
        .box5{
            width: 300px;
            height: 300px;
            background-color: yellow;
        }
    </style>
</head>
<body>
    <div class="box1">1</div>
    <div class="box4">4
        <div class="box5">5
    </div>
</div>
<div class="box2">2</div>
    <div class="box3">3</div>
</body>
</html>

5.绝对定位元素的布局

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .box1{
            width: 500px;
            height: 500px;
            background-color: chartreuse;
            position: relative;
        }
        .box2{
            width: 100px;
            height: 100px;
            background-color: orange;
            position: absolute;
            margin: auto;
            /* 当我们开启了绝对定位后:
            水平方向的布局等式就需要添加left  right
            此时规则和之前的一样
            当发生过度约束时,如果9个值中没有auto则自动调整为right值以使等式满足 
            如果有auto,则自动调整auto的值使等式满足*/
            left: 0;
            right: 0;
            top: 0;
            bottom: 0;
        }
    </style>
</head>
<body>
    <div class="box1">
        <div class="box2"></div>
    </div>
</body>
</html>

6.元素的层级

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        body{
            font-size: 60px;
        }
        .box1{
            width: 200px;
            height: 200px;
            background-color: palegreen;
            position: absolute;
            /* 对于开启定位元素,可以通过z-index属性来指定元素层级
              z-index需要一个整数作为参数,值越大,元素的层级越高
               元素的层级越高,越优先显示 
               祖先元素的层级在高,也不会盖住后代元素*/
            z-index: 1;
        }
        .box2{
            width: 200px;
            height: 200px;
            background-color: rgba(255, 0, 0,.3);
            position: absolute;
            top: 50px;
            left: 50px;
}
        .box3{
            width: 200px;
            height: 200px;
            background-color: rgb(13, 65, 236);
            position: absolute;
            top: 100px;
            left: 100px;
            z-index: 3;
        }
        .box4{
            width: 100px;
            height: 100px;
            background-color: rgb(248, 135, 6);
            position: absolute;
        }
    </style>
</head>
<body>
    <div class="box1">1</div>
    <div class="box2">2</div>
    <div class="box3">3
        <div class="box4">4</div>
    </div>
</body>
</html>

  • 4
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: CSS粘性定位(position:sticky)是一种新的定位方式,它可以在不同滚动位置下保留元素位置,使得页面更加直观。但是这种方式还存在一些采坑需要注意。 首先是浏览器兼容性问题。目前,不支持CSS粘性定位的浏览器非常常见,如IE、Edge,这使得使用CSS粘性定位时需要考虑到网站使用的浏览器类型和版本。 其次是定位方式问题。CSS粘性定位的原理是在元素滚动到指定位置时切换元素的position属性,如果在滚动到指定位置之前改变元素位置,则会影响其粘性定位效果。因此,在使用CSS粘性定位时需要特别注意元素位置是否会发生改变。 最后是滚动容器问题。CSS粘性定位是基于滚动容器进行计算的,因此滚动容器的设置会影响粘性定位效果。例如,如果没有设置滚动容器,粘性定位的元素将无法执行其粘性效果。另外,在滚动容器,如果存在其他元素会影响CSS粘性定位元素的位置,也会影响粘性定位效果。 总结来说,CSS粘性定位虽然具有很多优点,但其实现也需要我们仔细考虑各种问题。在使用CSS粘性定位时,需要考虑到浏览器兼容性、定位方式和滚动容器设置,这样才能真正实现理想的效果。 ### 回答2: CSS粘性定位position:sticky对于前端开发者来说已经是一种必备的技能。它可以让元素在滚动时保持位置的稳定性,同时也能始终保持相对应的位置。但是,尽管这个特性看似简单,却有着许多的问题和坑点需要注意。 第一个坑:浏览器兼容性问题。 尽管CSS粘性定位position:sticky已成为CSS三大定位属性之一,但实际上在早期的所有浏览器版本它并不存在。因此,需要考虑浏览器的兼容性问题。在IE11和最新版Chrome、Firefox、Edge等浏览器已支持。 第二个坑: z-index问题。 当元素使用position:sticky定位时,有时候会出现 z-index 失效的问题。可以通过设置该元素 position 属性为 relative 或者设置 z-index 属性来解决这个问题。 第三个坑:父级元素的高度问题。 CSS粘性定位position:sticky必须要有一个与父元素等高度的容器,否则它的粘性定位就不会生效。 第四个坑:滚动元素的高度限制问题。 如果滚动元素的高度不够,因此没有滚动条,那么粘性元素就会跟随滚动元素消失,而不是一直保持在相应的位置。 第五个坑:移动设备兼容性问题。 在移动设备上,由于touch事件的特性,很难达到粘性定位的效果, 因此需要进行移动端的优化。 总之, CSS粘性定位position:sticky对于提高用户体验来说是相当不错的,但是它在使用需要注重以下几个方面的问题:浏览器兼容性, z-index问题,父级元素的高度问题,滚动元素的高度限制问题,移动设备兼容性问题,只有注重这些问题,我们才能避免采坑的风险。 ### 回答3: CSS 粘性定位 position:sticky 是在滚动时保持元素位置不变直到到达特定位置才滑动的 CSS 属性。相比于 fixedabsolute 定位,它的一个重要优点是具有相对定位的特性,可以在父元素内使用。 然而,在使用 CSS 粘性定位时,会遇到一些采坑的问题。 1. 浏览器兼容性问题 CSS 粘性定位 position:sticky 并不是所有浏览器都支持。在国内主流浏览器,Chrome 和 Firefox 都已经支持,但是 IE 和 Safari 并不可用。由于兼容性问题,需要考虑向下兼容,使用传统的 JavaScript 实现滚动粘性定位。 2. 内容多少不一致,表现难以统一 如果父元素的高度不固定,而导致子元素的高度也不确定时,粘性定位会出现问题。如果子元素超过了父元素的高度,那么粘性定位就会失效。因此,在使用 CSS 粘性定位时,需要确保父元素的高度和子元素的高度保持一致,以保持表现的一致性。 3. 滚动条过长,性能开销大 当页面滚动距离过长,粘性定位会占用大量的 CPU 资源。如果页面存在大量粘性定位的元素,那么会对页面的性能造成很大的影响。因此,在使用 CSS 粘性定位时,需要考虑到页面性能的因素,尽量减少使用量。 总之,在使用 CSS 粘性定位时需要认真考虑兼容性、表现一致性和性能因素。建议在使用时灵活选择,并且在必要时使用 JavaScript 实现滚动粘性定位

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值