使用五种定位方式实现十字路口

第一种:margin 绝对定位

 1 <!DOCTYPE html>
 2 <html>
 3 <head lang="en">
 4     <meta charset="UTF-8">
 5     <title>十字路口</title>
 6     <style>
 7        /* html,body{
 8             height: 100px;
 9         }*/
10         #p1{
11             width: 200px;
12             height: 200px;
13             background: #ffa9db;
14             margin-top: 100px;
15             margin-left:100px;
16         }
17         #p2{
18             width: 200px;
19             height: 200px;
20             background: #80c9ff;
21             margin-top: -200px;
22             margin-left:400px;
23         }
24         #p3{
25             width: 200px;
26             height: 200px;
27             background: #73ff5c;
28             margin-top: 100px;
29             margin-left:100px;
30         }
31         #p4{
32             width: 200px;
33             height: 200px;
34             background: #c070ff;
35             margin-top: -200px;
36             margin-left:400px;
37         }
38     </style>
39 </head>
40 <body>
41 <div id="p1">
42 </div>
43 <div id="p2">
44 </div>
45 <div id="p3">
46 </div>
47 <div id="p4">
48 
49 </div>
50 </body>
51 </html>

第二种:固定定位

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
    <style>
        #a{
            width: 200px;
            height: 200px;
            border: 2px #c070ff solid;
            background-color: #c070ff;
            position: fixed;

        }
        #b{
            width: 200px;
            height: 200px;
            border: 2px #c070ff solid;
            background-color: #c070ff;
            position: fixed;
            left:20%;
        }
        #c{
            width: 200px;
            height: 200px;
            border: 2px #c070ff solid;
            background-color: #c070ff;
            position:fixed;
            top:40%;
        }
        #d{
            width: 200px;
            height: 200px;
            border: 2px #c070ff solid;
            background-color: #c070ff;
            position: fixed;
            top:40%;
            left:20%
        }
    </style>
</head>
<body>
<div id="a">

</div>
<div id="b">

</div>
<div id="c">

</div>
<div id="d">
</div>
</body>
</html>

第三种:float浮动

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title>float</title>
    <style>
        #a{
            width: 200px;
            height: 200px;
            border: 2px #c070ff solid;
            background-color: #c070ff;
            margin-right:20px;
            margin-top: 20px;
            float:left;
        }
        #b{
            width: 200px;
            height: 200px;
            border: 2px #80c9ff solid;
            background-color: #80c9ff;
            margin-top: 20px;
            margin-right:20px;
            float:left;
        }
        #c{
            width: 200px;
            height: 200px;
            border: 2px #73ff5c solid;
            margin-top: 20px;
            margin-right:20px;
            background-color: #73ff5c;
            float: left;
            clear: both;
        }
        #d{
            width: 200px;
            height: 200px;
            border: 2px #152427 solid;
            background-color: #152427;
            margin-top: 20px;
            margin-right:20px;
            float:left;
        }
    </style>
</head>
<body>
<div id="a">

</div>
<div id="b">

</div>
<div id="c">

</div>
<div id="d">

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

第四种:relative相对定位

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
    <style>
        #a{
            width: 200px;
            height: 200px;
            border: 2px #80c9ff solid;
            background-color: #80c9ff;
            position: relative;
        }
        #b{
            width: 200px;
            height: 200px;
            border: 2px #80c9ff solid;
            background-color: #80c9ff;
            position: relative;
            left:250px;
            top:-200px;
        }
       #c{
            width: 200px;
            height: 200px;
            border: 2px #80c9ff solid;
            background-color: #80c9ff;
            position: relative;
            top:-150px;
        }
        #d {
            width: 200px;
            height: 200px;
            border: 2px #80c9ff solid;
            background-color: #80c9ff;
            position: relative;
            left: 250px;
            top: -350px;
        }
    </style>
</head>
<body>
<div id="a">

</div>
<div id="b">

</div>
<div id="c">

</div>
<div id="d">
</div>
</body>
</html>

 第五种:absolute绝对定位

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title>ausolute</title>
    <style>
        #a{
            width: 200px;
            height: 200px;
            border: 2px #c070ff solid;
            background-color: #c070ff;
            position: absolute;

        }
        #b{
            width: 200px;
            height: 200px;
            border: 2px #c070ff solid;
            background-color: #c070ff;
            position: absolute;
            left:20%;
        }
        #c{
            width: 200px;
            height: 200px;
            border: 2px #c070ff solid;
            background-color: #c070ff;
            position: absolute;
            top:40%;
        }
        #d{
            width: 200px;
            height: 200px;
            border: 2px #c070ff solid;
            background-color: #c070ff;
            position: absolute;
            top:40%;
            left:20%
        }
    </style>
</head>
<body>
<div id="a">

</div>
<div id="b">

</div>
<div id="c">

</div>
<div id="d">
</div>
</body>
</html>

 

转载于:https://www.cnblogs.com/xy-milu/p/6013862.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值