css-210301-01

css - 210301 - 01

  • 定位
    • 相对定位
    • 绝对定位
    • 固定定位
    • z-index

定位

相对定位
position: relative;		相对定位,相对于原来的位置指定偏移;原来的位置还					   会被保留(div不会塌陷)
	
	top			上
	bottom		下
	left		左
	right		右
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>test02</title>

    <style>
        body{
            padding: 20px;
        }
        div{
            margin: 20px;
            padding: 5px;
            line-height: 15px;
        }
        #father{
            border: 2px solid #05e59d;
            padding: 10px;
            font-size: 13px;
        }
        #first{
            background-color: aliceblue;
            border: 2px dashed #1c9008;
            position: relative;
            top: -50px;     /*向上平移50px*/
        }
        #second{
            background-color: aquamarine;
            border: 2px dashed #131a18;
            position: relative;
            left: 50px;     /*距离当前位置,向右50px*/
        }
        #third{
            background-color: cadetblue;
            border: 2px dashed #e50518;
            position: relative;
            right: 50px;       /*距离当前位置,向左50px*/
        }
    </style>
</head>
<body>

<div id="father">
    <div id="first">窗口一</div>
    <div id="second">窗口二</div>
    <div id="third">窗口三</div>
</div>

</body>
</html>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>test03</title>

    <style>
        body{
            padding: 10px;
            margin: 10px;
        }
        #father{
            padding: 5px;
            width: 300px;
            height: 300px;
            border: 2px solid #c40928;
        }
        a{
            width: 100px;
            height: 100px;
            line-height: 100px;
            text-align: center; /*字体居中*/
            display: block;     /*变成块元素*/
            background-color: #f6879d;
            color: white;       /*字体颜色*/
            text-decoration: none;  /*去掉下滑线*/
        }
        a:hover{
            background-color: #5656f6;
        }
        #address02,#address04{
            position: relative;
            left: 200px;
            top: -100px;
        }
        #address05{
            position: relative;
            left: 100px;
            top: -300px;
        }
    </style>
</head>
<body>

<div id="father">
    <a id="address01" href="">链接一</a>
    <a id="address02" href="">链接二</a>
    <a id="address03" href="">链接三</a>
    <a id="address04" href="">链接四</a>
    <a id="address05" href="">链接五</a>
</div>

</body>
</html>

绝对定位
position: absolute;

1.没有父级元素定位前,相对于浏览器定位
2.父级元素定位下,会对父级元素进行偏移(在父级范围移动)
现对于父级或浏览器的位置,进行指定的偏移,绝对定位的话,不在标准文档流中,原来的位置不会保存
<!--相对于父级绝对定位-->
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>test04</title>

    <style>
        body{
            padding: 20px;
        }
        div{
            margin: 20px;
            padding: 5px;
            line-height: 15px;
        }
        #father{
            border: 2px solid #05e59d;
            padding: 10px;
            font-size: 13px;
            position: relative;
        }
        #first{
            background-color: aliceblue;
            border: 2px dashed #1c9008;
        }
        #second{
            background-color: aquamarine;
            border: 2px dashed #131a18;
            position: absolute;
            right: 20px;
        }
        #third{
            background-color: cadetblue;
            border: 2px dashed #e50518;
        }
    </style>
</head>
<body>

<div id="father">
    <div id="first">窗口一</div>
    <div id="second">窗口二</div>
    <div id="third">窗口三</div>
</div>

</body>
</html>

固定定位
position: fixed;
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>test05</title>
    <style>
        body{
            height: 1500px;
        }
        #div01{
            /*绝对定位*/
            width: 50px;
            height: 50px;
            background-color: #f6879d;
            color: white;
            text-align: center;
            line-height: 50px;
            position: absolute;
            right: 0;
            bottom: 0;
        }
        #div02{
            /*固定定位*/
            width: 100px;
            height: 100px;
            background-color: #7e6df1;
            color: white;
            text-align: center;
            line-height: 100px;
            position: fixed;
            right: 0;
            bottom: 0;
        }
    </style>
</head>
<body>

<div id="div01">绝对</div>
<div id="div02">固定</div>

</body>
</html>

z-index
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值