css学习之定位

1、css定位

2、定位机制:

普通流:元素按照其在HTML中的位置顺序决定排布的过程

浮动

绝对布局

3、css定位属性

position:static relative absolute fixed

(1)position设置为static

static属性表示被修饰的div在父容器内按照普通流布局,left,right等设置属性值不会生效

示例:


代码部分:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>定位</title>
    <link rel="stylesheet" href="style.css" type="text/css">
    <style type="text/css">
        #position1{
            width:100px;
            height:100px;
            background-color: #25ff32;
            position: static;
            left: 20px;
            top:20px;
            border: 1px solid blue;
        }
        #position2{
            width:100px;
            height:100px;
            background-color: #2016ff;
            position: static;
            left: 10px;
            top:10px;
            border:1px solid green;
        }
        #body{
            margin-left: 200px;
            margin-top: 200px;
            border:1px solid red;
        }
    </style>
</head>
<body>
<div id="body">
    <div id="position1">

    </div>
    <div id="position2">

    </div>
</div>
    <script>
        for(var i=0;i<100;i++){
            document.write(i+"<br/>")
        }
    </script>
</body>
</html>

(2)position设置为relative时

相对定位是指相对于元素本身应该的位置(针对于父容器而言的)而发生的偏移量。

示例:


代码部分:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>定位</title>
    <link rel="stylesheet" href="style.css" type="text/css">
    <style type="text/css">
        *{
            margin:0px;
        }
        #position1{
            width:100px;
            height:100px;
            /*background-color: #25ff32;*/
            position: relative;
            border: 1px solid blue;
            left:10px;
            top:10px;
        }
        #position2{
            width:100px;
            height:100px;
            /*background-color: #2016ff;*/
            position: relative;
            border:1px solid green;
            left:20px;
            top:20px;
        }
        #position3{
            width:100px;
            height:100px;
            /*background-color: #2016ff;*/
            position: relative;
            border:1px solid #17ff26;
        }
        #body{
            margin: 100px;
            border:1px solid red;
        }
    </style>
</head>
<body>
<div id="body">
    <div id="position1">

    </div>
    <div id="position2">

    </div>
    <div id="position3">

    </div>

</div>

    <script>
        for(var i=0;i<100;i++){
            document.write(i+"<br/>")
        }
    </script>
</body>
</html>
(3)position设置为absolute

absolute表示元素浮动在页面上,位置偏移量完全取决于left和top等的设置

效果图:


代码部分:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>定位</title>
    <link rel="stylesheet" href="style.css" type="text/css">
    <style type="text/css">
        *{
            margin:0px;
        }
        #position1{
            width:100px;
            height:100px;
            /*background-color: #25ff32;*/
            position: absolute;
            border: 1px solid blue;
            left:10px;
            top:10px;
        }
        #position2{
            width:100px;
            height:100px;
            /*background-color: #2016ff;*/
            position: absolute;
            border:1px solid green;
            left:20px;
            top:20px;
        }
        #position3{
            width:100px;
            height:100px;
            /*background-color: #2016ff;*/
            position: absolute;
            left: 30px;
            top: 30px;
            border:1px solid #17ff26;
        }
        #body{
            margin: 100px;
            border:1px solid red;
        }
    </style>
</head>
<body>
    <div id="position1">

    </div>
    <div id="position2">

    </div>
    <div id="position3">

    </div>


    <script>
        for(var i=0;i<100;i++){
            document.write(i+"<br/>")
        }
    </script>
</body>
</html>

(4)position设置为fixed时

position为fixed时,页面浮动在页面上,且是固定浮动在页面上,不随滚动条的滚动而滚动。

效果图:


代码部分:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>定位</title>
    <link rel="stylesheet" href="style.css" type="text/css">
    <style type="text/css">
        *{
            margin:0px;
        }
        #position1{
            width:100px;
            height:100px;
            /*background-color: #25ff32;*/
            position: fixed;
            border: 1px solid blue;
            left:10px;
            top:10px;
        }
        #position2{
            width:100px;
            height:100px;
            /*background-color: #2016ff;*/
            position: fixed;
            border:1px solid green;
            left:20px;
            top:20px;
        }
        #position3{
            width:100px;
            height:100px;
            /*background-color: #2016ff;*/
            position: fixed;
            left: 30px;
            top: 30px;
            border:1px solid #17ff26;
        }
    </style>
</head>
<body>
    <div id="position1">

    </div>
    <div id="position2">

    </div>
    <div id="position3">

    </div>


    <script>
        for(var i=0;i<100;i++){
            document.write(i+"<br/>")
        }
    </script>
</body>
</html>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Henry_Jing

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值