CSS定位

相对定位

元素添加position属性

  1. 相对定位
    position: relative 相对父元素
  2. 制定偏移量
    top: -10px
    注意
    相对定位依然在标准文档流中,原来的位置会被保留,不会出现父级边框塌陷问题
body {
    padding: 20px;
}

div {
    margin: 10px;
    padding: 5px;
    color: white;
}

#app {
    border: 1px solid red;
}

#app:after {
    content: "";
    display: block;
    clear: both;
}

#first {
    background-color: #da2ae7;
    position: relative;
    top: -10px;
}

#second {
    background-color: #2a59e7;
}

#third {
    background-color: #2ae749;
}

相对定位联系

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="css/style.css">
</head>
<body>

<div id="app">
    <a class="a1" href="#">地鼠1</a>
    <a class="a2" href="#">地鼠2</a>
    <a class="a3" href="#">地鼠3</a>
    <a class="a4" href="#">地鼠4</a>
    <a class="a5" href="#">地鼠5</a>
</div>

</body>
</html>
#app {
    width: 300px;
    height: 300px;
    padding: 10px;
    border: 2px solid red;
}

a {
    text-decoration: none;
    background-color: #da2ae7;
    display: block;
    width: 100px;
    height: 100px;
    text-align: center;
    line-height: 100px;
    font-size: 20px;
    color: white;
}

a:hover {
    background-color: #2700ff;
}

.a2, .a4 {
    position: relative;
    left: 200px;
    top: -100px;
}

.a5 {
    position: relative;
    left: 100px;
    top: -300px;
}

绝对定位

  1. 绝对定位
    position: absolute

  2. 偏移量
    top: -10px

注意

  1. 父级元素有定位时,子元素相对于父元素偏移
  2. 父级元素没有定位时,子元素相对于浏览器偏移
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="css/style.css">
</head>
<body>

<div id="app">
    <div id="first">狂人日记</div>
    <div id="second">孔乙己</div>
    <div id="third">阿Q正传</div>
</div>

</body>
</html>
body {
    padding: 20px;
}

div {
    margin: 10px;
    padding: 5px;
    color: white;
}

#app {
    border: 1px solid red;
    position: relative; /*父元素没有定位,则子元素相对于浏览器绝对定位*/
}

#app:after {
    content: "";
    display: block;
    clear: both;
}

#first {
    background-color: #da2ae7;
}

#second {
    background-color: #2a59e7;
}

#third {
    width: 100px;
    background-color: #2ae749;
    position: absolute;
    right: 30px;
}

固定定位

position: fixed
相对浏览器位置固定不变

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="css/style.css">
</head>
<body>

<div id="app">
    <div id="first">狂人日记</div>
    <div id="second">孔乙己</div>
    <div id="third">阿Q正传</div>
</div>

</body>
</html>
body {
    padding: 20px;
    height: 1000px;
}

div {
    margin: 10px;
    padding: 5px;
    color: white;
}

#app {
    border: 1px solid red;
}

#app:after {
    content: "";
    display: block;
    clear: both;
}

#first {
    background-color: #da2ae7;
}

#second {
    background-color: #2a59e7;
}

#third {
    width: 100px;
    background-color: #2ae749;
    position: fixed;
    right: 0;
    bottom: 0;
}

z-index

posted @ 2020-09-30 08:40  CrazyGod  阅读(113)  评论(0)    收藏  举报