css定位有哪些


本文摘自PHP中文网,作者藏色散人,侵删。

css定位有:1、static,表示静态定位;2、relative,表示相对定位;3、absolute,表示绝对定位;4、fixed,表示固定定位。

本文操作环境:windows7系统、HTML5&&CSS3版、Dell G3电脑。

CSS 定位 (Positioning) 属性允许你对元素进行定位。CSS 为定位提供了一些属性,利用这些属性,可以建立列式布局,将布局的一部分与另一部分重叠,还可以完成多年来通常需要使用多个表格才能完成的任务。

css定位的几种方式:

1、static(静态定位):

默认值。没有定位,元素出现在正常的流中(忽略 top, bottom, left, right 或者 z-index 声明)。参考上篇随笔。

2、relative(相对定位):

定位为relative的元素脱离正常的文档流,但其在文档流中的位置依然存在,只是视觉上相对原来的位置有移动。

通过top,bottom,left,right的设置相对于其正常(原先本身)位置进行定位。可通过z-index进行层次分级 。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

.static1{

            height:80px;

            background-color: red;

        }

        .relative{

            height:80px;

            position:relative;

            top:40px;

            left:40px;

            background-color: black;

        }

        .static2{

            height:80px;

            background-color: blue;

        }

    </style>

</head>

<body>

    <div class="static1"></div>

    <div class="relative"></div>

    <div class="static2"></div>

</body>

3、absolute(绝对定位):生成绝对定位的元素,相对于 static 定位以外的第一个父元素进行定位。元素的位置通过 "left", "top", "right" 以及 "bottom" 属性进行规定。可通过z-index进行层次分级。

定位为absolute的层脱离正常文档流,但与relative的区别是其在正常流中的位置不再存在。

这个属性总是有人给出误导。说当position属性设为absolute后,总是按照浏览器窗口来进行定位的,这其实是错误的。实际上,这是fixed属性的特点。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

<style type="text/css">

        .static1{

            height:80px;

            background-color: red;

     

        }

        .father{

            height:100px;

            background-color: pink;

            position:relative;

            left:20px;   

        }

        .relative{

            height:80px;

            width:80px;

            position:absolute;

            top:10px;

            left:10px;

         

            background-color: black;

        }

        .static2{

            height:80px;

            background-color: blue;

        }

    </style>

</head>

<body>

    <div class="static1"></div>

    <div class="father">

        <div class="relative"></div>

    </div>

    <div class="static2"></div>

4、fixed(固定定位):生成绝对定位的元素,相对于浏览器窗口进行定位。元素的位置通过 "left", "top", "right" 以及 "bottom" 属性进行规定。可通过z-index进行层次分级。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

<style type="text/css">

        .static1{

            height:80px;

            background-color: red;

     

        }

        .father{

            height:100px;

            width:300px;

            background-color: pink;           

            left:100px;   

            top:100px;

        }

        .relative{

            height:80px;

            width:80px;

            position:fixed;

            left:20px;       

            background-color: black;

        }

        .static2{

            height:80px;

            background-color: blue;

        }

    </style>

</head>

<body>

    <div class="static1"></div>

    <div class="father">

        <div class="relative"></div>

    </div>

    <div class="static2"></div>

【推荐学习:css视频教程】

以上就是css定位有哪些的详细内容,更多文章请关注木庄网络博客

相关阅读 >>

css white-space属性怎么用

实现弹幕效果的方法总结(css和canvas)

html与css有什么区别

16 个css开发中需要了解的devtools技巧

css怎么连接到html

css的选择符有哪些

css怎么给图片添加两个边框

css如何设置不透明

详解css中优先级和stacking context等高级特性,带你更深入了解css!!

css如何设置自动换行

更多相关阅读请进入《css》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...