通过id获取dom元素:[DOM]获取元素:根据ID、标签名、HTML5新增的方法、特殊元素获取
目录[DOM]获取元素:根据ID、标签名、HTML5新增的方法、特殊元素获取 1.根据 ID 获取[.getElementById( )] 2.根据标签名获取[.getElementsByTagNam
顺晟科技
2022-09-15 22:25:15
53
块元素位置居中有两种存在:
第一种实现方式如下:
<html> <head> <meta charset="utf-8" /> <title>测试</title> <style type="text/css"> .aa { width: 500px; height: 500px; background-color: #00FFFF; /*设置当前块元素生成绝对定位的元素,相对于浏览器窗口进行定位。*/ position: fixed; top: 50%; left: 50%; /*将块元素定位好位置后减去自身的长和宽*/ margin-left: -250px; margin-top: -250px; } </style> </head> <body> <div class="aa"></div> </body> </html>
效果图:
第二种实现方法如下:
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title>表单标签</title> <style type="text/css"> .aa { width: 100px; height: 100px; position: relative; background-color: antiquewhite; } .bb { width: 50px; height: 50px; background-color: #00FFFF; position: absolute; top: 50%; left: 50%; margin-top: -25px; margin-left: -25px; } </style> </head> <body> <div class="aa"> <div class="bb"></div> </div> </body> </html>
实现效果:
在这里顺便记录一下上面个代码用到的一个属性position以及这个属性的值的各种含义
定义:
osition 属性规定元素的定位类型。
这个属性定义建立元素布局所用的定位机制。任何元素都可以定位,不过绝对或固定元素会生成一个块级框,而不论该元素本身是什么类型。相对定位元素会相对于它在正常流中的默认位置偏移。
值 | 描述 | absolute
---|
19
2022-10
19
2022-10
25
2022-09
16
2022-09
15
2022-09
15
2022-09