半导体应届生offer怎么挑:大厂还是Fab
2026/8/8 11:07:54
position不会继承。每个元素的position默认都是static,不会从父元素继承。
.parent{position:absolute;/* 父元素设了 absolute */}.child{/* 没写 position,默认是 static */top:100px;/* ❌ 不生效!child 还是 static */}正确写法:每个元素都要自己写position。
.parent{position:absolute;}.child{position:relative;/* ✅ 自己也要写 */top:100px;/* ✅ 生效 */}| 属性 | 默认值 | 说明 |
|---|---|---|
position | static | 每个元素独立 |
display | inline/block(看标签) | 每个元素独立 |
width/height | auto | 每个元素独立 |
margin/padding | 0 | 每个元素独立 |
border | none | 每个元素独立 |
background | transparent | 每个元素独立 |
z-index | auto | 每个元素独立 |
| 属性 | 说明 |
|---|---|
color | 文字颜色 |
font-family | 字体 |
font-size | 字号 |
line-height | 行高 |
text-align | 文本对齐 |
visibility | 可见性 |
布局类属性(position、display、width、margin 等)都不继承;文本类属性(color、font 等)会继承。
在王者荣耀小乔页面中,.circles li设了position: absolute,但里面的.circle1还要再写一次position: relative,才能让top生效。
.circles li{position:absolute;/* li 设了 absolute */}.circle1{position:relative;/* ✅ circle1 自己也要设 position */top:900px;/* ✅ 才能生效 */}| 值 | 定位基准 | 是否脱离文档流 | 是否占原位置 |
|---|---|---|---|
static(默认) | 不定位 | ❌ | — |
relative | 自己原来的位置 | ❌ | ✅ 占(原位置保留) |
absolute | 最近的 position≠static 的祖先 | ✅ | ❌ 不占 |
fixed | 视口(浏览器窗口) | ✅ | ❌ 不占 |
sticky | 滚动到阈值时变 fixed | ❌ | ✅ 占 |
| 属性 | 作用 |
|---|---|
top | 距离上方的偏移量 |
left | 距离左方的偏移量 |
right | 距离右方的偏移量 |
bottom | 距离下方的偏移量 |
z-index | 层叠顺序(谁盖谁) |