补上一些学习笔记,内容不多也算是做一个总结(目前正在努力学习前端、查缺补漏)
本篇文章主要谈谈自己对于Css定位的理解
本篇文章讲解相对定位,为单独一篇详细讲解
往期
主篇章
相对定位(relative)
相对定位是相对于自身位置(即原来标准流下位置)进行偏移
相对定位是不脱离标准流的(不脱标)
相对定位是相对于自身位置(即原来标准流下位置)进行偏移
例子代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.a {
width: 400px;
height: 400px;
background-color: skyblue;
}
.b {
width: 200px;
height: 200px;
/* 相对定位 */
position: relative;
/* 相对于自身原来位置进行偏移 */
left: 150px;
background-color: seagreen;
}
</style>
</head>
<body>
<div class="a"></div>
<div class="b"></div>
</body>
</html>
运行结果(之所以四周有一点点小偏移是因为没有清除元素的内外边距,下同!):
验证相对定位是不脱离标准流的(不脱标)
例子代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.a {
width: 400px;
height: 400px;
/* 相对定位 */
position: relative;
/* 相对于自身原来位置进行偏移 */
left: 200px;
background-color: skyblue;
}
.b {
width: 200px;
height: 200px;
background-color: seagreen;
}
</style>
</head>
<body>
<div class="a"></div>
<div class="b"></div>
</body>
</html>
运行结果(之所以四周有一点点小偏移是因为没有清除元素的内外边距,下同!):