CSS Hacks

CSS Hacks

CSS Hacks, 持续补充

解决 div 高度崩塌

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>Testing</title>
<style>
.o {
border: 1px solid #000;
}
/* 将 after 伪元素设置为块元素添加到元素最后,然后对其清除浮动 */
.o:after {
content: "";
display: block;
clear: both;
}

.i {
width: 50px;
height: 50px;
background: red;
float: left;
clear: both;
}
</style>
</head>
<body>
<div class="o">
<div class="i"></div>
</div>
</body>
</html>

CSS 禁止换行

1
2
3
4
overflow: hidden; /* 超出隐藏 */
text-overflow: ellipsis; /* 文字省略号 */
word-break: keep-all; /* 禁止文字中断 */
white-space: nowrap; /* 空白不允许换行 */

使用 vertical-align 解决 inline-block 元素的基线问题

1
2
display: inline-block;
vertical-align: top; /* 设置 vertical-align 的元素必须是内联元素 */

黑白图片

1
2
3
img.desaturate {
filter: grayscale(100%);
}

页面顶部阴影

1
2
3
4
5
6
7
8
9
body:before {
content: "";
position: fixed;
top: -10px;
left: 0;
width: 100%;
height: 10px;
box-shadow: 0px 0px 10px rgba(0, 0, 0, 0.8);
}

优化显示文本

1
2
3
html {
text-rendering: optimizeLegibility;
}

文本渐变

1
<p class="gradient" data-text="文本渐变"></p>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
.gradient[data-text] {
position: relative;
}
.gradient[data-text]::after {
content: attr(data-text);
position: absolute;
top: 0;
left: 0;
color: red;
-webkit-mask-image: -webkit-gradient(
linear,
0 bottom,
100 bottom,
from(#ff0000),
to(rgba(0, 0, 255, 0))
);
}

模糊文本

1
2
3
4
.blur {
color: transparent;
text-shadow: 0 0 5px rgba(0, 0, 0, 0.5);
}

修改鼠标手势

  • 以下是常用的:

    1
    cursor: pointer | wait | hand | text | move | not-allowed;
  • 自定义图片:

    1
    cursor: url(cat.png), auto; /* 尽量在后面加上一般的手势,以防自定义URL找不到时出现问题 */

currentColor

currentColor: 标识当前的标签所继承的文字颜色

1
2
3
4
<div class="out-text">
鼠标来我这儿!
<div class="inside-box"></div>
</div>
1
2
3
4
5
6
7
8
9
10
11
12
.out-text {
width: 50px;
color: red;
}
.out-text:hover {
color: green;
}
.inside-box {
width: 50px;
height: 50px;
background-color: currentColor;
}

修改 Chrome 默认滚动条样式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
::-webkit-scrollbar-track {
border-radius: 10px;
background-color: transparent;
}

::-webkit-scrollbar {
width: 0.25rem;
height: 0.25rem;
background-color: transparent;
}

::-webkit-scrollbar-thumb {
border-radius: 10px;
background-color: rgba(153, 153, 153, 0.9);
display: none; /* 默认隐藏滚动条 */
}

*:hover::-webkit-scrollbar-thumb {
display: block; /* 当鼠标进入标签时显示滚动条 */
}

设置图片的高度与宽度相等

HTML:

1
2
3
<div class="box">
<img src=".......">
</box>

CSS:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/* box css*/
.box {
position: relative;
width: 300px;
}
.box:before {
content: '';
display: block;
padding-top: 100%;
}

/* img */
.box img {
position: absolute;
width: 100%;
height: 100%;
top: 0;
left: 0;
bottom: 0;
right: 0;
}
# CSS, Web
Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×