批改状态:合格
老师批语:写的很好!但是要注意文章排版!
| 属性 | 描述 |
|---|---|
| width | 宽度 |
| height | 高度 |
| border | 边框 |
| margin | 外边距 |
| padding | 内边距 |
| box-sizing | 自定义盒模型宽和高的规范 , box-sizing: content-box;在自己设定的宽度和高度之外绘制元素的内边距和边框(默认),box-sizing: border-box;在自己设定的宽度和高度之内绘制元素的内边距和边框。 |
| backgroumd-clip | 背景的延伸 |
<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8" /><meta name="viewport" content="width=device-width, initial-scale=1.0" /><title>盒/框模型基础</title><style>.box {/* 宽,高: 内容区 */width: 200px;height: 200px;}.box.one {padding: 10px;border: 2px solid #000;background-color: lightgreen;background-clip: content-box;/* margin: top right bottom left; *//* margin: 0 0 20px 0; */margin-bottom: 20px;}.box.two {padding: 10px;border: 2px solid #000;background-color: lightcoral;background-clip: content-box;/* 当二个盒子在垂直方向上,外边距会产生折叠 */margin-top: 30px;}.box.parent {background-color: lightblue;/* 一旦一个元素被添加了position,且值非static,那么它就是定位元素 */position: relative;/* 相对定位是相对自己做了偏移,这个元素在文档流中的位置不释放 *//* left: 30px;top: 40px; */}.son {width: 100px;height: 100px;background-color: violet;/* 绝对定位: 相对于它的定位父级进行定位 */position: absolute;/* 固定定位: 忽略你的定位父级,总是相对于<body>定位 */position: fixed;left: 50px;top: 50px;}</style></head><body><!-- 块级元素默认垂直排列 --><div class="box one"></div><div class="box two"></div><hr /><div class="box parent"><div class="son"></div></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>用户自定义元素大小的计算方式</title><style>.box1 {width: 200px;height: 200px;background-color: red;margin: 20px;padding: 20px;border: 2px solid green;box-sizing: content-boxbackground-clip: content-box;}.box2 {width: 200px;height: 200px;background-color: blue;margin: 20px;padding: 20px;border: 2px solid green;background-clip: content-box;box-sizing: border-box;}</style></head><body><div class="box1">1</div><div class="box2">2</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>定位元素的对齐</title><style>.container {width: 200px;height: 200px;border: 1px solid red;position: relative;}.item {width: 100px;height: 100px;background-color: greenyellow;/* 水平左右居中1 *//* margin-left: auto;margin-right: auto; *//* 水平左右居中2 *//* position: absolute;left: 0;right: 0;margin: auto; *//* 垂直上下居中 *//* position: absolute;top: 0;bottom: 0;margin: auto; *//* 完全居中 */position: absolute;top: 0;left: 0;right: 0;bottom: 0;margin: auto;}</style></head><body><div class="container"><div class="item"></div></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>元素的offset位置与大小</title><style>.box {width: 120px;height: 120px;position: absolute;top: 50px;left: 10px;}.box img {width: 100%;}</style></head><body><div class="box"><img src="thief.png" alt="" /></div><button>快跑</button><button>站住</button></body><script>// 只要不断的改变小偷的在定位父级中的偏移量就可以实现const thief = document.querySelector(".box");const btn1 = document.querySelector("button:first-of-type");const btn2 = document.querySelector("button:last-of-type");// 添加事件btn1.addEventListener("click", start, { once: true });btn2.addEventListener("click", stop);// 定时器let timer = null;function start() {console.log(thief.offsetLeft);timer = setInterval(function () {// thief.offsetLeft: 只读, 且是一个整数// thief.style.left: 可读写,且是一个字符串thief.style.left = thief.offsetLeft + 10 + "px";thief.style.top = thief.offsetTop + 10 + "px";}, 100);}function stop() {clearInterval(timer);}</script></html>

总结
querySelector() 获取元素box.clientWidth 获取元素的宽,只是content的宽度,不加内边距和边框box.clientWidth 获取元素的高,只是content的高度,不加内边距和边框document.documentElement.clientWidth 获取视口的宽度document.documentElement.clientHeight 获取视口的高度box.offsetParent 返回当前最近的经过定位的父级元素box.offsetWidth 元素真实的宽度,加内边距和边框box.offsetHeight 元素真实的高度,加内边距和边框box.offsetLeft 元素的左边间距box.offsetTop 元素的顶边间距document.documentElement 获取html元素html.style.height html文档高度 html.scrollHeighthtml.clientHeight 当前可视区的高度html.scrollTop 获取滚动条距离顶部的高度
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号