CSS position 用于控制元素在页面中的 位置定位方式。
CSS 中有 5 种定位方式:
static(默认)relative(相对定位)absolute(绝对定位)fixed(固定定位)sticky(粘性定位)static - 静态定位(默认值)元素遵循正常的文档流,忽略 top, right, bottom, left, z-index 属性。
特点:
z-index.box {
position: static; /* 默认值,可以不写 */
top: 100px; /* 无效! */
left: 50px; /* 无效! */
}
relative - 相对定位元素相对于自身原本的位置进行偏移。
特点:
z-index<div class="container">
<div class="box box1">Box 1</div>
<div class="box box2">Box 2 - 相对定位</div>
<div class="box box3">Box 3</div>
</div>
.box {
width: 100px;
height: 60px;
background: lightblue;
margin: 10px;
}
.box2 {
position: relative;
top: 20px; /* 从原位置向下移动20px */
left: 30px; /* 从原位置向右移动30px */
background: lightcoral;
}
absolute - 绝对定位元素相对于最近的已定位祖先元素(非 static)进行定位,如果没有,则相对于初始包含块(通常是 <html>)。
特点:
<html>z-index<div class="container" style="position: relative;">
<div class="box box1">Box 1</div>
<div class="box box2">Box 2 - 绝对定位</div>
<div class="box box3">Box 3</div>
</div>
.container {
position: relative; /* 定位上下文 */
width: 300px;
height: 200px;
border: 2px solid #333;
background: #f0f0f0;
}
.box {
width: 80px;
height: 40px;
background: lightblue;
}
.box2 {
position: absolute;
top: 20px; /* 距离容器顶部20px */
right: 30px; /* 距离容器右侧30px */
background: lightcoral;
}
fixed - 固定定位元素相对于浏览器视口进行定位,即使页面滚动也不会移动。
特点:
.fixed-header {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 60px;
background: #333;
color: white;
z-index: 1000;
}
.chat-button {
position: fixed;
bottom: 20px;
right: 20px;
width: 60px;
height: 60px;
border-radius: 50%;
background: #007bff;
color: white;
}
sticky - 粘性定位元素根据正常文档流定位,然后在达到特定阈值时变为固定定位。
特点:
top, right, bottom, left).sticky-header {
position: sticky;
top: 0;
background: white;
padding: 1rem;
border-bottom: 1px solid #ddd;
z-index: 100;
}
.table-header {
position: sticky;
top: 60px; /* 在距离顶部60px处固定 */
background: #f8f9fa;
}
.element {
position: absolute;
top: 10px; /* 距离参考元素顶部10px */
right: 20px; /* 距离参考元素右侧20px */
bottom: 30px; /* 距离参考元素底部30px */
left: 40px; /* 距离参考元素左侧40px */
}
z-index - 堆叠顺序控制定位元素的堆叠顺序(仅对定位元素有效)。
.modal {
position: fixed;
z-index: 1000; /* 数值越大,越在上面 */
}
.overlay {
position: fixed;
z-index: 999; /* 在modal下面 */
}
/* 方法1:绝对定位 + transform */
.center-absolute {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
/* 方法2:绝对定位 + margin */
.center-margin {
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
margin: auto;
width: 200px;
height: 100px;
}
/* 方法3:Flexbox(现代推荐)*/
.flex-container {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
}
.dropdown {
position: relative;
display: inline-block;
}
.dropdown-menu {
position: absolute;
top: 100%; /* 在父元素下方 */
left: 0;
min-width: 150px;
background: white;
border: 1px solid #ccc;
display: none;
}
.dropdown:hover .dropdown-menu {
display: block;
}
.modal-overlay {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: rgba(0, 0, 0, 0.5);
z-index: 1000;
}
.modal-content {
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
background: white;
padding: 2rem;
border-radius: 8px;
z-index: 1001;
}
.table-container {
height: 400px;
overflow-y: auto;
}
table {
width: 100%;
}
th {
position: sticky;
top: 0;
background: #f8f9fa;
z-index: 10;
}
.fab {
position: fixed;
bottom: 24px;
right: 24px;
width: 56px;
height: 56px;
border-radius: 50%;
background: #007bff;
color: white;
border: none;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.3);
cursor: pointer;
}
.progress-bar {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 3px;
background: #007bff;
transform-origin: left;
animation: progress 2s linear infinite;
}
@keyframes progress {
0% {
transform: scaleX(0);
}
100% {
transform: scaleX(1);
}
}
.container {
position: relative; /* 创建定位上下文 */
}
.child {
position: absolute; /* 相对于.container定位 */
top: 10px;
left: 10px;
}
.parent {
position: relative;
z-index: 1; /* 创建层叠上下文 */
}
.child {
position: absolute;
z-index: 100; /* 只在父元素的层叠上下文中生效 */
}
.container {
position: relative;
overflow: hidden; /* 隐藏超出部分 */
}
.absolute-child {
position: absolute;
/* 可能超出容器 */
}
.fixed-mobile {
position: fixed;
/* 在iOS Safari中可能需要 */
-webkit-overflow-scrolling: touch;
}
.sticky-element {
position: sticky;
top: 0;
/* 确保父容器有明确高度且可滚动 */
}
.parent-container {
height: 400px; /* 必须有高度限制 */
overflow-y: auto; /* 必须可滚动 */
}