批改状态:合格
老师批语:写的很不错,很认真,继续加油!
margin :包含 margin-top,margin-left,margin-right,margin-bottom ;
border :可以定义边框宽度、边框线样式、颜色、形状等;
padding :包含 padding-top,padding-left,padding-right,padding-bottom ;
在html代码中是这样定义的:
<style>div {width: 100px;height: 100px;/* 设置4周外边距为20px, margin: 20px 20px 20px 20px;的简写*/margin: 20px;/* 设置边框为1px,实线,黑色 */border: 2px solid black;/* 设置4周内边距为10px, padding: 10px 10px 10px 10px;的简写*/padding: 10px;background-color: lightblue;}</style><body><div>盒子</div></body>
运行效果示意:

我们来看一个盒子模型示意:

其中:
width = 100px
margin = 20px
border = 2px
padding = 10px
如果增加padding或者border 的值,那么盒子大小会随之改变;
这样会影响页面布局,那么不想让盒子大小随属性变化而变化则需要给盒子样式增加一个属性:
box-sizing: border-box; 重新计算盒子大小,以边框为准,布局中会经常用到这个属性。
* {margin: 0;padding: 0;box-sizing: border-box;}
我们可以利用定位和视口实现子元素在父元素中的垂直居中
例如:
<!DOCTYPE html><html lang="zh-cn"><head><meta charset="UTF-8" /><meta name="viewport" content="width=device-width, initial-scale=1.0" /><title>小刚开发日志:块元素垂直居中</title></head><style>.box1 {width: 200px;height: 200px;background-color: lightblue;/* 水平居中 */margin: auto;/* 设置父元素为定位参照 */position: relative;}.box2 {width: 100px;height: 100px;background-color: red;/* 左右居中 */margin: auto;/* 参照父元素调整位置 */position: absolute;/* 设置视口:从父元素的左上顶点到右下顶点 */top: 0;left: 0;right: 0;bottom: 0;}</style><body><div class="box1">box1<div class="box2">box2</div></div></body></html>
运行效果:

<!DOCTYPE html><html lang="zh-cn"><head><meta charset="UTF-8" /><meta name="viewport" content="width=device-width, initial-scale=1.0" /><title>Document</title></head><style>.box {width: 200px;height: 200px;background-color: lightcoral;padding: 10px;margin: 20px;border: 2px solid black;background-clip: content-box;}</style><body><div id="box" class="box"></div><div id="box" class="box"></div><div id="box" class="box"></div><div id="box" class="box"></div><div id="box" class="box"></div><div id="box" class="box"></div><div id="box" class="box"></div><div id="box" class="box"></div><div id="box" class="box"></div><div id="box" class="box"></div></body><script>const box = document.querySelector("#box");// clientWidth = width + 左右paddingconsole.log(box.clientWidth);// clientHeight = height + 上下paddingconsole.log(box.clientHeight);// 当前视口宽度console.log(document.documentElement.clientWidth);// 当前视口高度console.log(document.documentElement.clientHeight);// offsetWidth = width + 左右padding + 左右boderconsole.log(box.offsetWidth);// offsetHeight = height + 上下padding + 上下boderconsole.log(box.offsetHeight);// 获取定位父级console.log(box.offsetParent);// 距离定位父级顶部距离console.log(box.offsetTop);// scrollWidth:获取指定标签内容层的真实宽度(可视区域宽度+被隐藏区域宽度)console.log(box.scrollWidth);// scrollHeight:获取指定标签内容层的真实高度(可视区域高度+被隐藏区域高度)console.log(box.scrollHeight);// scrollTop:内容层顶部 到 可视区域顶部的距离console.log(box.scrollTop);</script></html>
运行结果

代码如下
<!DOCTYPE html><html lang="zh-cn"><head><meta charset="UTF-8" /><meta name="viewport" content="width=device-width, initial-scale=1.0" /><title>小刚的日志:小动画</title></head><style>button {width: 50px;height: 30px;}.box {width: 200px;height: 300px;position: absolute;top: 50px;left: 20px;}.box img {width: 100%;}</style><body><div class="box"><img src="img.png" alt="刀剑神域" /></div><button>go</button><button>stop</button></body><script>const img = document.querySelector(".box");const imggo = document.querySelector("button:first-of-type");const imgstop = document.querySelector("button:last-of-type");// 监听事件imggo.addEventListener("click", go);imgstop.addEventListener("click", stop);// 动画开始及停止方法let timer = null;function go() {console.log(img.offsetLeft);timer = setInterval(function () {img.style.left = img.offsetLeft + 10 + "px";img.style.top = img.offsetTop + 10 + "px";}, 200);}function stop() {clearInterval(timer);}</script></html>
运行效果

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号