- CSS 教程
- CSS - 首页
- CSS - 路线图
- CSS - 简介
- CSS - 语法
- CSS - 选择器
- CSS - 引入
- CSS - 测量单位
- CSS - 颜色
- CSS - 背景
- CSS - 字体
- CSS - 文本
- CSS - 图片
- CSS - 链接
- CSS - 表格
- CSS - 边框
- CSS - 块级边框
- CSS - 内联边框
- CSS - 外边距
- CSS - 列表
- CSS - 内边距
- CSS - 光标
- CSS - 轮廓
- CSS - 尺寸
- CSS - 滚动条
- CSS - 内联块
- CSS - 下拉菜单
- CSS - 可见性
- CSS - 溢出
- CSS - Clearfix
- CSS - 浮动
- CSS - 箭头
- CSS - 调整大小
- CSS - 引号
- CSS - 顺序
- CSS - 位置
- CSS - 连字符
- CSS - 悬停
- CSS - 显示
- CSS - 聚焦
- CSS - 缩放
- CSS - 转换
- CSS - 高度
- CSS - 连字符字符
- CSS - 宽度
- CSS - 不透明度
- CSS - Z-Index
- CSS - 底部
- CSS - 导航栏
- CSS - 覆盖层
- CSS - 表单
- CSS - 对齐
- CSS - 图标
- CSS - 图片库
- CSS - 注释
- CSS - 加载器
- CSS - 属性选择器
- CSS - 组合器
- CSS - 根
- CSS - 盒模型
- CSS - 计数器
- CSS - 剪辑
- CSS - 书写模式
- CSS - Unicode-bidi
- CSS - min-content
- CSS - 全部
- CSS - 内嵌
- CSS - 隔离
- CSS - 滚动溢出
- CSS - Justify Items
- CSS - Justify Self
- CSS - 制表符大小
- CSS - 指针事件
- CSS - Place Content
- CSS - Place Items
- CSS - Place Self
- CSS - 最大块大小
- CSS - 最小块大小
- CSS - 混合模式
- CSS - 最大内联大小
- CSS - 最小内联大小
- CSS - 偏移
- CSS - 重音颜色
- CSS - 用户选择
- CSS 高级
- CSS - 网格
- CSS - 网格布局
- CSS - Flexbox
- CSS - 可见性
- CSS - 定位
- CSS - 图层
- CSS - 伪类
- CSS - 伪元素
- CSS - @规则
- CSS - 文本效果
- CSS - 分页媒体
- CSS - 打印
- CSS - 布局
- CSS - 验证
- CSS - 图片精灵
- CSS - 重要
- CSS - 数据类型
- CSS3 教程
- CSS3 - 教程
- CSS - 圆角
- CSS - 边框图像
- CSS - 多背景
- CSS - 颜色
- CSS - 渐变
- CSS - 盒阴影
- CSS - 盒装饰断开
- CSS - 光标颜色
- CSS - 文本阴影
- CSS - 文本
- CSS - 2d 变换
- CSS - 3d 变换
- CSS - 过渡
- CSS - 动画
- CSS - 多列
- CSS - 盒尺寸
- CSS - 工具提示
- CSS - 按钮
- CSS - 分页
- CSS - 变量
- CSS - 媒体查询
- CSS - 函数
- CSS - 数学函数
- CSS - 遮罩
- CSS - 形状
- CSS - 样式图像
- CSS - 特异性
- CSS - 自定义属性
- CSS 响应式
- CSS RWD - 简介
- CSS RWD - 视口
- CSS RWD - 网格视图
- CSS RWD - 媒体查询
- CSS RWD - 图片
- CSS RWD - 视频
- CSS RWD - 框架
- CSS 工具
- CSS - PX 到 EM 转换器
- CSS - 颜色选择器和动画
- CSS 资源
- CSS - 有用资源
- CSS - 讨论
CSS - 定位
CSS **定位**有助于操作网页中任何元素的位置。在本教程中,我们将学习 position 属性及其关联的值。
目录
什么是 CSS 位置?
在 CSS 中,**position** 属性用于通过调整网页中元素的位置来创建浮动元素、浮动侧边栏和其他交互式功能。
除了 position 属性之外,其他属性如 **top,bottom,right** 和 **left** 用于控制其在页面上的精确位置。它们指定元素与其边缘的偏移量。我们将在本教程中看到这些示例。
语法
以下是 css position 的可能值。
position: static | relative | absolute | fixed | sticky;
静态定位元素
当您使用 **position: static** 属性时,元素将在文档流中正常定位。left、right、top、bottom 和 z-index 属性不会影响元素。这是 position 属性的默认值。
示例
在此示例中,我们定义了两个静态定位的 div 元素和一个相对定位的元素。查看区别。
<!DOCTYPE html>
<html lang="en">
<head>
<style>
.container {
padding: 20px;
border: 1px solid #ccc;
}
.static-element {
position: static;
background-color: lightblue;
padding: 10px;
margin: 10px 0;
}
.non-static-element {
position: relative;
top: 20px;
background-color: lightgreen;
padding: 10px;
}
</style>
</head>
<body>
<div class="container">
<div class="static-element">
This is a static element (default position).
</div>
<div class="non-static-element">
This element is not static and is moved
20px down.
</div>
<div class="static-element">
This is another static element (default
position).
</div>
</div>
</body>
</html>
相对定位元素
CSS **position: relative** 属性相对于其在页面上的原始位置定位元素。您可以使用 left、right、top 和 bottom 属性来移动元素,但它仍将在文档流中占用空间。
示例
在此示例中,我们定义了一个静态定位的 div 元素和一个相对定位的元素。查看区别。
<!DOCTYPE html>
<html lang="en">
<head>
<style>
*{
padding: 15px;
}
.container {
border: 1px solid #ccc;
}
.static-element {
position: static;
background-color: lightblue;
}
.relative-element {
position: relative;
top: 20px;
left: 30px;
background-color: lightgreen;
}
.normal-flow {
background-color: lightcoral;
margin: 10px 0;
}
</style>
</head>
<body>
<div class="container">
<div class="static-element">
This is a static element (default position).
</div>
<div class="relative-element">
This element is relatively positioned 20px
down and 30px right.
</div>
<div class="normal-flow">
This element is in the normal document
flow, after the relative element.
</div>
</div>
</body>
</html>
绝对定位元素
具有 **position: absolute** 的元素将从文档流中移除,并相对于其最近的已定位祖先(如果有)进行定位。如果没有已定位的祖先,则元素相对于视口进行定位。
您可以使用 top、right、bottom 和 left 属性来指定元素相对于其包含块的位置。
示例
此示例显示了 position absolute 的用法
<!DOCTYPE html>
<html lang="en">
<head>
<style>
*{
padding: 15px;
}
.container {
/* This makes container positioned ancestor */
position: relative;
width: 70%;
height: 200px;
border: 1px solid #ccc;
}
.static-element {
position: static;
background-color: lightblue;
padding: 10px;
margin: 10px 0;
}
.absolute-element {
position: absolute;
/* 50px from top of nearest positioned ancestor */
top: 50px;
/* 30px from left of nearest positioned ancestor */
left: 30px;
background-color: lightgreen;
padding: 10px;
}
</style>
</head>
<body>
<div class="container">
<div class="static-element">
This is a static element (default position).
</div>
<div class="absolute-element">
This element is absolutely positioned 30px
from top and 50px from left.
</div>
</div>
</body>
</html>
固定定位元素
在 CSS 中,**position: fixed** 属性用于使元素即使在用户滚动时也保持在屏幕上的相同位置。然后,您可以使用 left、right、top 和 bottom 属性将元素定位到所需位置。
示例
在此示例中,您可以看到当您向下滚动时,段落元素仍然存在。
<html>
<head>
<style>
div {
width: 100%;
height: 500px;
background-color: lightgrey;
overflow: auto;
padding: 15px;
}
.fixed-position {
position: fixed;
top: 50%;
left: 20%;
padding: 5px;
background-color: white;
}
</style>
</head>
<body>
<div>
<p>You can Scroll down...</p>
<p class="fixed-position">
Tutorialspoint CSS Position Fixed
</p>
<p>
White screen will remain at 50%
height from top
</p>
</div>
</body>
</html>
粘性定位元素
在 CSS 中,**position: sticky** 属性用于使任何元素即使在用户滚动时也保持在容器顶部。然后,您可以使用 left、right、top 和 bottom 属性将元素定位到所需位置。
**position: sticky** 属性是 **position: relative** 和 **position: fixed** 属性的组合。
示例
在此示例中,您可以看到当您滚动时,fixed 移动到顶部
<html>
<head>
<style>
div {
width: 100%;
height: 500px;
background-color: lightgrey;
overflow: auto;
padding: 15px;
}
.sticky-position {
position: sticky;
top: 50%;
left: 20%;
padding: 5px;
background-color: white;
}
</style>
</head>
<body>
<div>
<p>You can Scroll down...</p>
<p class="sticky-position">
TutorialsPoint CSS Position Fixed
</p>
<p>
White screen will stick at top of
screen when you scroll down.
</p>
</div>
</body>
</html>
在图像中定位文本
在以下示例中,您可以使用 **position: absolute** 属性在图像内不同方向定位文本。文本元素位于左上角、右上角、左下角和右下角。
示例
在以下示例中,我们在图像上定位了文本。
<html>
<head>
<style>
.container {
position: relative;
border: 2px solid #ef2c2c;
}
.center {
position: absolute;
top: 45%;
width: 100%;
text-align: center;
}
.top-left {
position: absolute;
top: 12px;
left: 30px;
}
.top-right {
position: absolute;
top: 12px;
right: 30px;
}
.bottom-left {
position: absolute;
bottom: 12px;
left: 30px;
}
.bottom-right {
position: absolute;
bottom: 12px;
right: 30px;
}
img {
width: 100%;
opacity: 0.7;
}
</style>
</head>
<body>
<div class="container">
<img src="/css/images/red-flower.jpg"
width="1000px" height="350px">
<h3 class="center">
Text at Center
</h3>
<h3 class="top-left">
Text at Top Left
</h3>
<h3 class="top-right">
Text at Top Right
</h3>
<h3 class="bottom-left">
Text at Bottom Left</h3>
<h3 class="bottom-right">
Text at Bottom Right
</h3>
</div>
</body>
</html>
CSS 与位置相关的属性
以下是所有与 **position** 相关的 CSS 属性列表
| 属性 | 描述 | 示例 |
|---|---|---|
| bottom | 与 position 属性一起使用以放置元素的底部边缘。 | |
| clip | 设置元素的剪切蒙版。 | |
| left | 与 **position** 属性一起使用以放置元素的左边缘。 | |
| overflow | 确定如何呈现溢出内容。 | |
| right | 与 **position** 属性一起使用以放置元素的右边缘。 | |
| top | 设置元素的定位模型。 | |
| vertical-align | 设置元素的垂直位置。 | |
| z-index | 设置当前元素的渲染层。 |