CSS 面试题大全(含答案)
一、基础概念
1. CSS 选择器有哪些?优先级如何计算?
答案:
-
选择器类型:通配符(*)、元素(div)、类(.class)、ID(#id)、属性([type="text"])、伪类(:hover)、伪元素(::before)、组合器(空格、>、+、~)
-
优先级计算:
-
!important > 行内样式 > ID选择器 > 类/属性/伪类 > 元素/伪元素 > 通配符
-
具体数值:行内样式(1000) > ID(100) > 类/属性/伪类(10) > 元素/伪元素(1) > 通配符(0)
-
相同优先级,后定义的覆盖先定义的
-
2. CSS 的继承性
答案:
-
可继承属性:color、font-family、font-size、font-weight、line-height、text-align、visibility、opacity等
-
不可继承属性:width、height、margin、padding、border、background、position、display等
-
控制继承:inherit(强制继承)、initial(初始值)、unset(继承或初始)、revert(浏览器默认)
-
总结:文字相关的往下传,盒子相关的自己管
3. em/rem/px/vw/vh 的区别
答案:
-
px:绝对单位,固定大小
-
em:相对父元素字体大小,会层叠累积
-
rem:相对根元素(html)字体大小,避免累积
-
vw:视口宽度的1%
-
vh:视口高度的1%
-
vmin/vmax:vw和vh中较小/较大的值
二、布局相关
4. Flex 布局常用属性
答案:
/* 容器属性 */
.container {
display: flex;
flex-direction: row | column; /* 主轴方向 */
flex-wrap: wrap | nowrap; /* 换行 */
justify-content: center | space-between; /* 主轴对齐 */
align-items: center | stretch; /* 交叉轴对齐 */
align-content: space-around; /* 多轴线对齐 */
gap: 10px; /* 间距 */
}
/* 项目属性 */
.item {
flex-grow: 1; /* 放大比例 */
flex-shrink: 0; /* 缩小比例 */
flex-basis: 100px; /* 初始大小 */
flex: 1 0 auto; /* 简写 */
align-self: center; /* 单独对齐 */
order: -1; /* 排序 */
}
5. Grid 布局常用属性
答案:
.container {
display: grid;
grid-template-columns: 1fr 1fr 100px; /* 列宽 */
grid-template-rows: 100px auto 100px; /* 行高 */
gap: 10px; /* 间距 */
justify-items: center; /* 单元格内水平对齐 */
align-items: center; /* 单元格内垂直对齐 */
grid-template-areas:
"header header header"
"sidebar main main"
"footer footer footer";
}
.item {
grid-column: 1 / 3; /* 从第1列到第3列 */
grid-row: span 2; /* 跨2行 */
grid-area: header; /* 区域命名 */
justify-self: end; /* 单个单元格水平对齐 */
align-self: start; /* 单个单元格垂直对齐 */
}
6. 两栏/三栏布局的实现
答案:
/* 两栏布局(固定+自适应) */
1.浮动 + BFC
.fixed {
float: left;
width: 200px;
}
.adaptive {
overflow: hidden; /* 触发BFC */
}
2.Grid
.layout {
display: grid;
grid-template-columns: 200px 1fr; /* 200px + 剩余空间 */
min-height: 200px;
}
.left {
background: #f0f0f0;
}
.right {
background: #e0e0e0;
}
3.flex
.layout {
display: flex;
min-height: 200px; /* 可选 */
}
.left {
width: 200px;
background: #f0f0f0;
}
.right {
flex: 1; /* 占据剩余空间 */
background: #e0e0e0;
}
/* 三栏布局(圣杯/双飞翼) */
.container {
display: flex;
}
.left { width: 200px; }
.right { width: 200px; }
.center { flex: 1; }
/* 使用grid */
.container {
display: grid;
grid-template-columns: 200px 1fr 200px;
}
7. 水平垂直居中的多种方式
答案:
/* 1. flex */
.parent {
display: flex;
justify-content: center;
align-items: center;
}
/* 2. grid */
.parent {
display: grid;
place-items: center;
}
/* 3. 绝对定位 + transform */
.child {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
/* 4. 绝对定位 + margin:auto */
.child {
position: absolute;
top: 0; left: 0; right: 0; bottom: 0;
margin: auto;
}
/* 5. line-height 单行文本 */
.parent {
text-align: center;
line-height: 100px;
}
三、盒模型与定位
8. 什么是盒模型?IE盒模型和标准盒模型的区别?
答案:
-
标准盒模型:width = content,总宽度 = width + padding + border + margin
-
IE盒模型:width = content + padding + border,总宽度 = width + margin
-
切换 :
box-sizing: content-box(标准) |border-box(IE)
9. position 属性详解
答案:
-
static:默认,不定位
-
relative:相对自身原位置,占位保留
-
absolute:相对最近的非static祖先,不占位
-
fixed:相对视口,不占位
-
sticky:粘性定位,relative + fixed的结合
.sticky {
position: sticky;
top: 0; /* 滚动到0时固定 */
}
10. 什么是BFC?如何触发?有什么作用?
答案:
-
定义:块级格式化上下文,独立渲染区域
-
触发条件:
-
float不为none
-
position为absolute/fixed
-
overflow不为visible
-
display为inline-block/flex/grid/table-cell
-
contain为layout/content/paint
-
-
作用:
-
清除浮动
-
防止margin重叠
-
阻止元素被浮动元素覆盖
-
四、响应式与移动端
11. 响应式设计实现方式
答案:
/* 1. 媒体查询 */
@media screen and (max-width: 768px) {
.container {
width: 100%;
}
}
/* 2. 相对单位 */
.container {
width: 80%;
font-size: 2vw;
}
/* 3. flex/grid */
.container {
display: flex;
flex-wrap: wrap;
}
/* 4. 图片响应式 */
img {
max-width: 100%;
height: auto;
}
12. 移动端适配方案
答案:
-
viewport :
<meta name="viewport" content="width=device-width, initial-scale=1"> -
rem适配:根据屏幕宽度动态设置根字体大小
document.documentElement.style.fontSize = document.documentElement.clientWidth / 375 * 16 + 'px'
-
vw/vh适配:直接使用vw单位
-
1px边框问题:transform: scale(0.5)
-
媒体查询:针对不同设备设置样式
13. 什么是Retina屏?如何处理1px问题?
答案:
/* 伪元素 + transform */
.border-1px {
position: relative;
}
.border-1px::after {
content: '';
position: absolute;
bottom: 0;
left: 0;
width: 100%;
height: 1px;
background: #000;
transform: scaleY(0.5);
transform-origin: 0 0;
}
/* 媒体查询 */
@media screen and (-webkit-min-device-pixel-ratio: 2) {
.border-1px::after {
transform: scaleY(0.5);
}
}
五、动画与过渡
14. CSS3 动画属性
答案:
/* transition - 过渡 */
.element {
transition: all 0.3s ease-in-out;
transition-property: width;
transition-duration: 0.5s;
transition-timing-function: cubic-bezier(0.4, 0, 0.2, 1);
transition-delay: 0.1s;
}
/* animation - 动画 */
.element {
animation: slide 2s infinite alternate;
}
@keyframes slide {
0% { transform: translateX(0); }
100% { transform: translateX(100px); }
}
15. transform 属性
答案:
.element {
transform: translate(10px, 20px); /* 位移 */
transform: rotate(45deg); /* 旋转 */
transform: scale(1.5); /* 缩放 */
transform: skew(10deg); /* 倾斜 */
transform: matrix(1, 0, 0, 1, 0, 0); /* 矩阵变换 */
transform-origin: left top; /* 变换原点 */
}
六、伪类与伪元素
16. 伪类和伪元素的区别
答案:
-
伪类(:) - 选择特定状态,如:hover、:first-child、:nth-child
-
伪元素(::) - 创建不存在元素,如::before、::after、::first-line
/* 伪类 */
li:first-child {}
a:hover {}
input:focus {}
/* 伪元素 */
div::before { content: 'before'; }
p::first-line { font-weight: bold; }
17. :nth-child 的用法
答案:
/* 基础用法 */
li:nth-child(2) /* 第2个 */
li:nth-child(2n) /* 偶数 */
li:nth-child(2n+1) /* 奇数 */
li:nth-child(3n) /* 3的倍数 */
li:nth-child(n+4) /* 第4个及以后 */
li:nth-child(-n+3) /* 前3个 */
/* 其他变体 */
li:nth-last-child(2) /* 倒数第2个 */
li:first-child /* 第1个 */
li:last-child /* 最后1个 */
li:only-child /* 唯一子元素 */
七、常见面试题
18. 如何清除浮动?
答案:
1.额外标签法
<div class="container"> <div class="box" style="float: left;">浮动元素</div> <div class="box" style="float: left;">浮动元素</div> <!-- 在最后加一个空标签 --> <div style="clear: both;"></div> </div>
2.父元素overflow法
.container {
overflow: hidden; /* 或 auto */
/* 触发BFC,自动包含浮动 */
}
3.clearfix伪元素法
/* 现代clearfix */
.clearfix::after {
content: "";
display: table; /* 或 block */
clear: both;
}
/* 使用 */
<div class="container clearfix">
<div style="float: left;">浮动元素</div>
</div>
19. CSS 隐藏元素的几种方式
答案:
/* 1. display: none - 不占位,不响应事件 */
.hide1 { display: none; }
/* 2. visibility: hidden - 占位,不响应事件 */
.hide2 { visibility: hidden; }
/* 3. opacity: 0 - 占位,响应事件 */
.hide3 { opacity: 0; }
/* 4. position + 移出屏幕 */
.hide4 {
position: absolute;
left: -9999px;
}
/* 5. clip/clip-path */
.hide5 {
clip: rect(0, 0, 0, 0);
}
/* 6. transform: scale(0) */
.hide6 { transform: scale(0); }
20. CSS 性能优化
答案:
-
使用类选择器代替标签选择器
-
避免使用通配符(*)
-
减少层级嵌套
-
使用CSS精灵图
-
压缩CSS文件
-
使用transform代替top/left做动画
-
避免使用@import
-
将CSS放在头部
21. 层叠上下文
答案:
-
形成条件:
-
根元素(html)
-
z-index不为auto的定位元素
-
opacity小于1的元素
-
transform不为none的元素
-
filter不为none的元素
-
will-change指定属性
-
-
层叠顺序(从低到高):
-
背景和边框
-
负z-index
-
块级元素
-
浮动元素
-
内联元素
-
正z-index
-
八、实战场景
22. 实现一个三角形
.triangle {
width: 0;
height: 0;
border-left: 50px solid transparent;
border-right: 50px solid transparent;
border-bottom: 50px solid red;
}
/* 不同方向 */
.triangle-up { border-bottom-color: red; }
.triangle-down { border-top: 50px solid red; }
.triangle-left { border-right: 50px solid red; }
.triangle-right { border-left: 50px solid red; }
23. 实现一个加载动画
.loader {
width: 48px;
height: 48px;
border: 5px solid #f3f3f3;
border-top: 5px solid #3498db;
border-radius: 50%;
animation: spin 1s linear infinite;
}
@keyframes spin {
0% { transform: rotate(0deg); }
100% { transform: rotate(360deg); }
}
24. 文本溢出省略
/* 单行 */
.single-line {
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
/* 多行 */
.multi-line {
display: -webkit-box;
-webkit-line-clamp: 3;
-webkit-box-orient: vertical;
overflow: hidden;
}