CSS之定位和opacity使用

web.jpeg

定位:将指定的元素放在指定的位置,通过定位可以任意的排放元素,定位分为:
相对定位,绝对定位,固定定位 和默认的值

1.相对定位(相对自己原来的位置偏移)

  • 当开启相对定位以后,没有设置偏移量,元素是不会发生任何变化
  • 当开启元素定位是我们可以通过方位名词:top,right,left,bottom来设置元素的偏移量, (比如:left元素相对其定位位置的左边),就是相对于元素在文档流中原来的位置进行定位
  • 相对定位的元素不会脱离文档流
  • 相对定位会提高自己的层级
  • 相对定位不会改变元素的性质,块级元素还是块级,行内元素还是行内
 <style>
        div{
            width: 200px;
            height: 200px;
        }
        div:first-child{
            background-color: yellow;
        }
        div:nth-child(2){
            background-color: green;
              /* 开启相对定位 */
             position: relative;
             left: 200px;
             top: 200px;

        }
        div:last-child{
            background-color: purple;
        }
    </style>

<body>
    <!-- 定位 -->
    <div class="box1"></div>
    <div class="box2"></div>
    <div class="box3"></div>
</body>
image.png

2.绝对定位(脱离标准流)

  • position: absolute;
  • 如果盒子的没有父元素,那么将来在定位的时候,我们top 、left 、right 、bottom 是相对于body元素的(视口)
  • 如果定位的盒子有父元素,但是父元素/祖先元素没有定位,那么这个子元素定位的方位名词还是相对于body.
  • 如果定位的盒子有父元素/祖先元素,并且父/祖先元素有定位,那么这个元素的定位是相对于它的父元素(子元素定位的trbl是相对于父元素。)相邻的祖先元素有定位
  • 绝对定位会提高元素的层级
  • 绝对定位会改变元素的性质 display: inline-block;
  • 子绝父相
<style>
        div{
            width: 200px;
            height: 200px;
            background-color: yellowgreen;
            
        }
        div:nth-child(2){
            background-color: green;
            /* 绝对定位 */
            position: absolute;
            left: 100px;
            top: 100px;
        }
        div:last-child{
            background-color: purple;
        }

    </style>

<body>
    <div class="box1"></div>
    <div class="box2"></div>
    <div class="box3"></div>
</body>

image.png

3.固定定位(脱离标准流)

  • 永远是相对于浏览器的窗口定位
  • 改变元素的显示方式 display: inline-block;
  • 固定定位是固定在浏览器窗口的一个位置,不会跟随页面的滚动而滚动
  • position: fixed; 开启固定定位
 <style>
        div{
            width: 200px;
            height: 200px;
            background-color: yellowgreen;
            
        }
        div:nth-child(2){
            background-color: green;
            /* 开启固定定位 */
           position: fixed;
           /* 设置位置 */
           left: 100px;
           top: 100px;
          
          
        }
        div:last-child{
            background-color: purple;
            height: 600px;
        }

    </style>

<body>
    <div class="box1">111</div>
    <div class="box2">222</div>
    <div class="box3">333</div>
</body>

Untitled.gif

总结

定位模式 是否脱标占有位置 是否发生偏移 移动位置基准
静态static 不脱标,正常模式 不可以 正常模式
相对定位relative 不脱标,占有位置 可以 相对自身位置移动(自恋型)
绝对定位absolute 完全脱标,不占有位置 可以 相对于定位父级移动位置(拼爹型)
固定定位fixed 完全脱标,不占有位置 可以 相对于浏览器移动位置(认死理型)

4.层级z-index

  • 如果定位的元素的层级是一样的,则下面的元素会盖着上面的元素
image.png
  • z-index这个属性,来调整元素的层级,层级越高越优先显示
  • z-index只有开启定位的元素才可以使用
<style>
        div{
            width: 200px;
            height: 200px;
            background-color: yellowgreen;
            position: relative;
            
        }
        div:nth-child(2){
            background-color: green;
            position: absolute;
            left: 100px;
            top: 100px;
             /* 调整层级 */
            z-index: 1000;
          
          
          
        }
        div:last-child{
            background-color: purple;
            position: relative;
        }

    </style>

image.png

4.1应用

 <style>
        div{
            width: 200px;
            height: 400px;
            border: 1px solid #cccccc;
            float: left;
            /* 避免边框重合 */
            margin-left: -1px;
        }
        div:hover{
            border: 1px solid orange; 
            /* 在悬停的时候 有一个边框被层级 高的覆盖 */
            position: relative;
            /* 定位 可以提高自身的层级 */
        }
    </style>

<body>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
</body>

层级的应用.gif

5.透明

  • opacity设置透明,取值范围在0-1
  • 需要注意的是,这个属性在IE8及一下的浏览器中不起作用

需要使用 这个属性来兼容 filter: alpha(opacity=50) 这个里面的取值是0-100

 <style>
        div{
            width: 200px;
            height: 200px;
            background-color: yellowgreen;
            position: relative;
            
        }
        div:nth-child(2){
            background-color: green;
            position: absolute;
            left: 100px;
            top: 100px;
            /* 调整层级 */
            z-index: 1000;
            /* 设置透明 0-1之间的取值 */
            opacity: 0.5;
            filter: alpha(opacity=50);
            /* 
            opacity IE8一下的浏览器不支持
            可以使用 filter: alpha(opacity=50)代替
            0-100之间的值,表示透明度
            
             */
          
          
          
        }
        div:last-child{
            background-color: purple;
            position: relative;
        }

    </style>

image.png

6.定位盒子居中的问题

我们知道在标准流中使用margin: 0 auto可以设置盒子居中,但是在定位中,这个属性不起作用,那么如何设置定位盒子的居中?

如果在定位中,设置left的偏移量是50%,那么就会如下图所示: 我们只需要使用margin-left让盒子便宜起宽度的一半

image.png
 <style>
        .box {
            width: 400px;
            height: 200px;
            background-color: rebeccapurple;
            position: relative;
          
        }
        span {
            position: absolute;
            width: 100px;
            height: 20px;
            background-color: pink;
            left: 50%;
            bottom: 10px;
            margin-left: -50px;
        }
        /* 
            定位的盒子在使用   margin:  0 auto;设置盒子居中是不起作用的 ,这个适合的事标准流
        
        */
    </style>

<body>
    <!-- 定位的盒子 -->
    <div class="box">
        <span class="center"></span>
    </div>

    
    
</body>

image.png
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 1.定位 1.1 定位基础 需要定位的原因:1.当需要某个元素自由的在盒子内移动位置,并且压住其他盒子时。2.当滚...
    OohMuYi阅读 751评论 0赞 0
  • 一、CSS入门 1、css选择器 选择器的作用是“用于确定(选定)要进行样式设定的标签(元素)”。 有若干种形式的...
    宠辱不惊丶岁月静好阅读 1,780评论 0赞 6
  • 1.CSS基本概念 1.1 CSS的定义 CSS(Cascading Style Sheets)层叠样式表,主要用...
    寥寥十一阅读 2,159评论 0赞 6
  • 学习目标 理解能说出为什么要用定位能说出定位的 4 种分类能说出四种定位的各自特点能说出我们为什么常用子绝父相布局...
    jovelin阅读 1,758评论 0赞 0
  • 一、标准流(普通流/文档流) 所谓的标准流: 就是标签按照规定好默认方式排列. 块级元素会独占一行,从上向下顺序排...
    testleaf阅读 2,512评论 1赞 7

友情链接更多精彩内容