首页 > web前端 > css教程 > css的三种定位方式是什么

css的三种定位方式是什么

青灯夜游
发布: 2021-11-02 12:00:58
原创
12712 人浏览过

css的三种定位方式是:1、相对定位,元素的位置相对于它的原始位置计算而来,语法“position:relative;”;2、固定定位,语法“position:fixed;”;3、绝对定位,语法“position:absolute;”。

css的三种定位方式是什么

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

相对定位

该元素的位置是相对于它的原始位置计算而来的。

1

position:relative;

登录后复制

他是默认参照父级的原始点为原始点,配合top、right、bottom、left进行定位。

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

34

35

36

<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8" />

<title>相对定位</title>

<style type="text/css">

.king{

margin-top: 30px;

margin-left: 30px;

border: 1px solid silver;

background-color: skyblue;

width: 40%;

}

.king div{

width: 100px;

height: 60px;

margin: 10px;

background-color: snow;

color: black;

border: 1px solid black;

}

.three{

position: relative;

top: 20px;

left: 50px;

}

</style>

<body>

<div class="king">

<div class="one">one</div>

<div class="two">two</div>

<div class="three">three</div>

<div class="four">four</div>

</div>

</body>

</html>

登录后复制

固定定位

被固定的元素不会随着滚动条的拖动而改变位置。

1

position:fixed;

登录后复制

在默认情况下,固定定位元素的位置是相对浏览器而言,结合top、bottom、left和right这4个属性一起使用。

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

<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8" />

<title>固定定位</title>

<style type="text/css">

.first{

width: 50px;

height: 160px;

border: 1px solid gray;

background-color: #b7f1b7;

}

.second{

position: fixed;

top: 50px;

left: 160px;

width: 150px;

height: 100px;

border: 1px solid silver;

background-color:#b7f1b7;

}

</style>

<body>

<div class="first">div元素</div>

<div class="second">固定定位的div元素</div>

</body>

</html>

登录后复制

绝对定位

1

position:absolute;

登录后复制

默认情况下,绝对定位的位置是相对于浏览器而言,配合top、right、bottom、left进行定位。

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

34

35

36

37

38

39

40

<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8" />

<title>绝对定位</title>

<style type="text/css">

.king{

padding: 15px;

border: 1px solid silver;

background-color: skyblue;

width: 30%;

}

.king div{

padding: 10px;

}

.one{

background-color: chartreuse;

}

.two{

background-color: cyan;

position: absolute;

top: 20px;

right: 40px;

}

.three{

background-color: darkred;

}

.four{

background-color: dimgrey;

}

</style>

<body>

<div class="king">

<div class="one">one</div>

<div class="two">two</div>

<div class="three">three</div>

<div class="four">four</div>

</div>

</body>

</html>

登录后复制

z-index

z-index 属性设置元素的堆叠顺序。拥有更高堆叠顺序的元素总是会处于堆叠顺序较低的元素的前面。

  • 元素可拥有负的 z-index 属性值。
  • Z-index 仅能在定位元素上奏效(例如 position:absolute;)

属性值: auto:默认,堆叠顺序与父元素相等。 number:设置元素的堆叠顺序。 inherit:规定应该从父元素继承 z-index 属性的值。

例: 设置图像的 z-index:

1

2

3

4

5

6

img{

position:absolute;

left:0px;

top:0px;

z-index:-1;

}

登录后复制

(学习视频分享:css视频教程

以上是css的三种定位方式是什么的详细内容。更多信息请关注PHP中文网其他相关文章!

相关标签:
css
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板