相对定位:position:relative

特点:a.相对于自己原来位置的定位,以自己的左上角为基准。

b.相对定位原来的位置仍然算位置,不会出现浮动现象。

以下为初始位置:(可以看出设置margin和position的区别)

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>相对定位</title>
<style>
* {
margin: 0;
padding: 0;
}
.top {
height: 100px;
width: 100px;
margin: 50px;
background-color: pink;
/* position: relative;
top:100px;
bottom: 100px; */
}
/* .bottom {
height: 100px;
width: 120px;
background-color: yellow;
} */
</style>
</head>
<body>
<div class="top"></div>
<div class="bottom"></div>
</body>
</html>

 效果图如下:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>相对定位</title>
<style>
* {
margin: 0;
padding: 0;
}
.top {
height: 100px;
width: 100px;
margin: 50px;
background-color: pink;
/* 设定相对位置 */
position: relative;
top:100px;
bottom: 100px;
}
/* .bottom {
height: 100px;
width: 120px;
background-color: yellow;
} */
</style>
</head>
<body>
<div class="top"></div>
<div class="bottom"></div>
</body>
</html>

设定相对定位之后的效果图:

top设置相对定位,bottom不设置定位:

初始状态如下:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>相对定位</title>
<style>
* {
margin: 0;
padding: 0;
}
.top {
height: 100px;
width: 100px;
background-color: pink;
/* 设定相对位置
position: relative;
top:100px;
bottom: 100px; */
}
.bottom {
height: 100px;
width: 100px;
background-color: yellow;
}
</style>
</head>
<body>
<div class="top"></div>
<div class="bottom"></div>
</body>
</html>

 效果图如下:

设定了相对定位后:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>相对定位</title>
<style>
* {
margin: 0;
padding: 0;
}
.top {
height: 100px;
width: 100px;
background-color: pink;
/* 设定相对位置 */
position: relative;
top:100px;
bottom: 100px;
}
.bottom {
height: 100px;
width: 120px;
background-color: yellow;
}
</style>
</head>
<body>
<div class="top"></div>
<div class="bottom"></div>
</body>
</html>

效果图如下:

day6 相对定位:position:relative的更多相关文章

  1. 层模型--相对定位(position:relative)

    如果想为元素设置层模型中的相对定位,需要设置position:relative(表示相对定位),它通过left.right.top.bottom属性确定元素在正常文档流中的偏移位置.相对定位完成的过程 ...

  2. 相对定位position: relative;

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  3. CSS绝对定位和相对定位 position: absolute/relative

    absolute(绝对定位): 会把对象拖离HTML文档流,并通过top, left, right, bottom确定对象的具体位置,这个四个位置属性至少要设置一个,否则无法激活对象的absolute ...

  4. CSS position relative absolute fixed

    position属性absolute与relative 详解   最近一直在研究javascript脚本,熟悉DOM中CSS样式的各种定位属性,以前对这个属性不太了解,从网上找到两篇文章感觉讲得很透彻 ...

  5. css - position relative与display table-cell深入分析

    在很多时候,想使用 display: table; 以及其子元素使用 display: table-cell 都只是为了使用 vertical-align 这一属性.这是一个很简单的常识,但这次在项目 ...

  6. 解决td标签上的position:relative属性在各浏览器中的兼容性问题

    在css中的position属性规定了页面元素的定位类型,它有以下几个值: absolute:绝对定位,相对于static以外的第一个父元素进行定位: fixed:生成绝对定位的元素,相对于浏览器窗口 ...

  7. css中position:relative的真正理解

    其实话说一直以来也没真正去理解好position:relative的用法的真实意义. 我想很多人实实在在用的多都是position:relative和position:absolute结合起来一起用的 ...

  8. css position relative obsolution

    层级关系为:<div ——————————— position:relative; 不是最近的祖先定位元素,不是参照物<div—————————-没有设置为定位元素,不是参照物<di ...

  9. position relative

    position的默认值是static,(也就是说对于任意一个元素,如果没有定义它的position属性,那么它的position:static) 如果你想让这个#demo里的一个div#sub相对于 ...

随机推荐

  1. Chrome 里的请求报错 " Provisional headers are shown"

    之所以会出现这个警告,是因为去获取该资源的请求其实并(还)没有真的发生; 背景:提交表单,按钮点击<button>标签,触发事件,ajax发送请求,服务器返回信息; <button& ...

  2. 前端css图片固定宽高问题

    img需要宽高都固定时,图片往往会因此变形,此时可采用的方法有: 上述代码会使得图片居中,边缘部分不显示.这是在图片大小跟container大小差不多的情况下.如果图片很大的话,只显示中心部分是不行的 ...

  3. Yet Another Array Queries Problem CodeForces - 863D (暴力/思维)

    You are given an array a of size n, and q queries to it. There are queries of two types: 1 li ri — p ...

  4. wpf passwobox 添加水印

    之前有做过wpf texbox添加水印,这个并不难 重写一下样式就可以了,今天用到了passwordbox 添加水印的时候 发现还是有点难度的. 这个难度就在于如何去取password的长度来控制水印 ...

  5. 修改jupyter notebook响应的浏览器

    Windows下更改jupyter notebook默认响应的浏览器为Chrome 1.命令行下输入:jupyter notebook --generate-config 2.C盘中找到并打开文件:C ...

  6. 浅析 Nginx 网络事件

    Nginx 是一个事件驱动的框架,所谓事件主要指的是网络事件,Nginx 每个网络连接会对应两个网络事件,一个读事件一个写事件.在深入了解 Nginx 各种原理及在极端场景下的一些错误场景处理时,需要 ...

  7. 14.python函数

    原文:https://www.cnblogs.com/linhaifeng/articles/6113086.html 1.python中函数定义:函数是逻辑结构化和过程化的一种编程方法. pytho ...

  8. python单例设计模式

    class Dog(object): __instance = None def __init__(self): pass def __new__(cls): if not cls.__instanc ...

  9. 第二阶段:2.商业需求分析及BRD:3.产品需求分析

    产品需求收集之后就可以进行产品需求分析了. 比如微信功能的逐步完善,偏向于做加法,但有时候也会做减法. Y轴是重要跟不重要 X轴是紧急跟不紧急 然后通过各个需求的分数来确定坐标位置.同时可以根据阶段调 ...

  10. 用Tasker实现收到Android手机短信自动转发到邮箱

    发送短信到邮箱的原理与 <用Tasker实现收到Android手机短信自动转发到邮箱>有些类似.  发送短信到邮箱是利用Ifttt这个服务将短信转发到邮箱中.Ifttt服务的可扩展性很强, ...