首页 技术 正文
技术 2022年11月20日
0 收藏 869 点赞 3,517 浏览 1107 个字

写给后端的前端笔记:定位(position)

既然都写了一篇浮动布局,干脆把定位(position)也写了,这样后端基本上能学会css布局了。

类别

我们所说的定位position主要有三类:固定定位fixed,相对定位relative,绝对定位absolute。它们都有相同的四个属性:topbottomleftright

区别

主要在于他们的参照物不一样

<div class="block" id="div1">固定定位bottom: 40px;<br>right: 40px;</div>
<div class="block" id="div2">相对于浏览器窗口的绝对定位<br>top: 20px;<br>right: 20px;</div>
<div class="block" id="div3">
相对定位
<div class="block" id="div4">相对于父元素的绝对定位<br>right: 10px;<br>top: 20px;</div>
</div>
<div class="block" id="div5">相对定位</div>
.block{
width: 100px;
height: 100px;
}

固定定位

固定定位的参照物是浏览器窗口,很多窗口广告就是用的固定定位,无论你怎么滚动或者放大缩小窗口,永远固定在浏览器窗口某个角落。

修改topbottomleftright的值可以调整元素在浏览器窗口的位置。

#div1{
position: fixed;
bottom: 40px;
right: 40px;
background: red;
}

绝对定位

绝对定位的参照物是该元素上一级的拥有position:relative属性的父元素,如果该元素的上一级父元素没有设置相对定位,那么该元素的参照物就会变成当前页面。

修改topbottomleftright的值可以调整元素在父元素内的位置。

#div2{
position: absolute;
top: 20px;
right: 20px;
background: green;
}
#div3{
position: relative;
width: 300px;
height: 300px;
background: blue;
}
#div4{
position: absolute;
right: 10px;
top: 20px;
background: yellow;
}

相对定位

相对定位的参照物是该元素本来的位置。

修改topbottomleftright的值可以让元素相对于原来的位置上下左右移动。

#div5{
position: relative;
top: -20px;
right: -100px;
background: grey;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,954
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,479
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,291
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,108
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,740
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,774