批改状态:合格
老师批语:
定位属性
position在前端开发中非常常用,他的定位类型有哪些呢?
| 编号 | 类型 | 值 | 说明 |
|---|---|---|---|
| 1 | 静态定位 | static | 默认,也就是文档流定位,元素的显示位置与源码顺序一致 |
| 2 | 相对定位 | relative | 相对于之前的位置偏移,它在文档流中的原始空间会保留下来 |
| 3 | 绝对定位 | absolue | 相对于它的祖先中离它最近的定位元素的位置发生偏移如果祖先中不存在定位元素,它就参考根元素(HTML)进行定位。它在文档流中的原始空间不会保留下来 |
| 4 | 固定定位 | fixed | 是绝对定位的一个特例,它是始终相对与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>* {margin: 0;padding: 0;box-sizing: border-box;}.box {height: 20em;width: 20em;/* 设置相对定位 */position: relative;border: solid red 1px;top: 1em;}.box h2 {/* 设置绝对定位 */position: absolute;background-color: rgb(36, 230, 204);top: 0;left: 0;right: 0;bottom: 0;margin: auto;width: 5em;height: 1.5em;}</style></head><body><div class="box"><h2>php中文网</h2></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>.main {background-color: rgba(255, 192, 203, 0.212);height: 100em;position: relative;}.back-top img {width: 50px;position: fixed;right: 2em;top: 80vh;}</style></head><body><div class="main">主体<a href="#" class="back-top"><imgsrc="https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1608572122793&di=512f5d7c2bd0a352b72abf76124aaa3d&imgtype=0&src=http%3A%2F%2Fku.90sjimg.com%2Felement_origin_min_pic%2F01%2F47%2F97%2F1157440c088f8f9.jpg"/></a></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>* {margin: 0;padding: 0;box-sizing: border-box;overflow: hidden;}header {background-color: cadetblue;padding: 0.5em 2em;}header > h2 {float: left;}header > button {float: right;height: 2.5em;width: 12em;border-radius: 0.3em;}header > button:hover {background-color: rgb(255, 255, 255);cursor: pointer;}.modal {display: none;}/* 蒙板 */.backdrop {background-color: rgba(0, 0, 0, 0.281);position: fixed;top: 0;left: 0;right: 0;bottom: 0;}.modal-body {padding: 1em;max-width: 20em;border: 1px solid red;background: linear-gradient(to right, red, #efefef);position: fixed;top: 30vh;left: 0em;right: 0em;margin: auto;}.modal-body form button {margin: 1em;width: 20em;height: 2.5em;}.close {position: absolute;top: 0;right: 0;width: 5em;}</style></head><body><header><h2>我的博客</h2><button>登录</button></header><!-- 模态框 --><div class="modal"><!-- 蒙板 --><div class="backdrop"><div class="modal-body"><button class="close">关闭</button><br /><form action="#" method="POST"><label for="username">用户:</label><input type="text" name="username" id="username" required /><br /><label for="password">密码:</label><input type="text" name="password" id="password" required /><br /><br /><hr /><button>登录</button></form></div></div></div><script>const btn = document.querySelector("header button");const modal = document.querySelector(".modal");const close = document.querySelector(".close");btn.addEventListener("click", setModal, false);close.addEventListener("click", setModal, false);function setModal(ev) {ev.preventDefault();let status = window.getComputedStyle(modal, null).getPropertyValue("display");modal.style.display = status === "none" ? "block" : "none";}</script></body></html>
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号