[关闭]
@wangyupu 2020-05-21T06:11:23.000000Z 字数 3134 阅读 29

HTML5+CSS第八章主讲定位网页元素

HTML5+CSS


本章目标

会使用position定位网页元素
会使用z-index属性调整定位元素的堆叠次序

定位在网页中的应用

不随滚动条移动的固定导航
下拉菜单
鼠标移入弹出的消息框

定位

position属性
static:默认值,没有定位
relative:相对定位
absolute:绝对定位
fixed:固定定位

static定位

没有定位,以标准流方式显示

相对定位

relative属性值
相对自身原来位置进行偏移
偏移设置:top、left、right、bottom
示例:

third {

background-color:#C5DECC;
border:1px #395E4F dashed;
position:relative;
right:20px;
bottom:30px;

}
教员讲课示例:

.div1{
width: 100px;
height: 100px;
background-color: red;
/*相对定位
*
* 1.相对于自身进行偏移
* 2.原来的位置保留
*
* 怎么来测试自身原本位置在哪?*/
position: relative;
left: 200px;
top: 300px;

    }
    .div2{
        width: 100px;
        height: 100px;
        background-color: green;
    }
</style>
<body>
    <div class="div1"></div>
    <div class="div2"></div>
</body>

偏移量方向

left和top正、负取值的方向

相对定位元素的规律

设置相对定位的盒子会相对它原来的位置,通过指定偏移,到达新的位置
设置相对定位的盒子仍在标准文档流中,它对父级盒子和相邻的盒子都没有任何影响
设置相对定位的盒子原来的位置会被保留下来

浮动元素设置相对定位

设置第二个盒子右浮动,再设置第一、第二盒子相对定位
示例
first {
background-color:#FC9;
border:1px #B55A00 dashed;
position:relative;
right:20px;
bottom:20px;}
second {
background-color:#CCF;
border:1px #0000A8 dashed;
float:right;
position:relative;
left:20px;
top:-20px;}

绝对定位

absolute属性值
    偏移设置: left、right、top、bottom     示例:
    <style type="text/css">
    .div1{
        width: 100px;
        height: 100px;
        background-color: red;
        /*绝对定位   
         * 1.根据窗口最顶点进行偏移  如果父容器有定位,则最顶点在父容器
         * 2.原来位置不会被保留
         * 
         *  */
        position: absolute;
        left: 100px;
        top: 300px;
    }
    .div2{
        width: 100px;
        height: 100px;
        background-color: green;
        margin: 0 auto;
        /*相对定位*/
        position: relative;
    }
    .div2 span{
        /*绝对定位*/
        position: absolute;
        left:0;
        top: 0;
    }
</style>
<body>
    <div class="div1"></div>


    <div class="div2">
        <div >
            <span>张三你好</span>
        </div>
    </div>
</body>

绝对定位小结

使用了绝对定位的元素以它最近的一个“已经定位”的“祖先元素” 为基准进行偏移
如果没有已经定位的祖先元素,会以浏览器窗口为基准进行定位
绝对定位的元素从标准文档流中脱离,这意味着它们对其他元素的定位不会造成影响
元素位置发生偏移后,它原来的位置不会被保留下来

绝对定位不设置偏移量

示例
second {
background-color:#CCF;
border:1px #0000A8 dashed;
position:absolute;
right:30px;
}
经验
设置了绝对定位但没有设置偏移量的元素将保持在原来的位置。
在网页制作中可以用于需要使某个元素脱离标准流,而仍然希望它保持在原来的位置的情况

固定定位2-1

fixed属性值
偏移设置: left、right、top、bottom
类似绝对定位,不过区别在于定位的基准不是祖先元素,而是浏览器窗口
示例:
div:nth-of-type(1) { /第一个div设置绝对定位/
width: 100px;
height: 100px;
background: red;
position: absolute;
right: 0;
bottom: 0;
}
div:nth-of-type(2) { /第二个div设置固定定位/
width: 50px;
height: 50px;
background: yellow;
position: fixed;
right: 0;
bottom: 0;
}
body{
height: 1000px;
}

定位小结3-1

相对定位的特性
相对于自己的初始位置来定位
元素位置发生偏移后,它原来的位置会被保留下来
层级提高,可以把标准文档流中的元素及浮动元素盖在下边
相对定位的使用场景
相对定位一般情况下很少自己单独使用,都是配合绝对定位使用,为绝对定位创造定位父级而又不设置偏移量

z-index属性

调整元素定位时重叠层的上下位置
z-index属性值:整数,默认值为0
设置了positon属性时,z-index属性可以设置各元素之间的重叠高低关系
z-index值大的层位于其值小的层上方
老师讲课示例:

.topimg{
width: 100px;
height: 100px;
border-radius: 50%;
padding: 2px;
border: 1px #eee solid;
box-sizing: border-box;
position: relative;

    }
    .topimg .img1{
        width: 100%;
        height: 100%;
        border-radius: 50%;
        position: relative;
    }
    .topimg .img2{
        width: 30px;
        height: 30px;
        position: absolute;
        right: 0;
        top: 0;
        z-index:1000;
    }
</style>
<body>
    <div class="topimg">
        <img class="img1" src="img/adver-01.jpg" />
        <img class="img2" src="img/validate.gif" />
    </div>
</body>

网页元素透明度

opacity:x x值为0~1,值越小越透明 opacity:0.4;
filter:alpha(opacity=x) x值为0~100,值越小越透明 filter:alpha(opacity=40);
示例:

.div1{
width: 300px;
height: 150px;
position: relative;
}
.div1 img{
width: 100%;
height: 100%;
}
.content{
position: absolute;
bottom: 0;
/background-color: rgba(0,0,0,0.5);/
background-color: #000;
color: #fff;
width: 100%;
text-align: center;
opacity: 0.5;
}





这是一个图片


小结:

网页中的元素都含有两个堆叠层级
未设置绝对定位时所处的环境,z-index是0
设置绝对定位时所处的堆叠环境,此时层的位置由z-index的值确定
改变设置绝对定位和没有设置绝对定位的层的上下堆叠顺序,只需调整绝对定位层的z-index值即可

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注