使用 CSS 实现相对定位
cssweb developmentfront end technology更新于 2025/8/23 16:12:17
相对定位会改变 HTML 元素相对于其正常显示位置的位置。因此,"left:20"将元素的左侧位置增加 20 像素。
您可以使用 top 和 left 两个值以及 position 属性将 HTML 元素移动到 HTML 文档中的任何位置。
- 向左移动 - 对 left 使用负值。
- 向右移动 - 对 left 使用正值。
- 向上移动 - 对 top 使用负值。
- 向下移动 - 对 top 使用正值。
示例
您可以尝试运行以下代码来实现相对定位 −
<html> <head> </head> <body> <div style = "position:relative;left:80px;top:2px;background-color:yellow;"> This div has relative positioning. </div> </body> </html>
相关文章
有用资源
css 参考教程 - 该教程包含有关 css 的更多信息:https://www.cainiaomax.com/css/

