常用的一些CSS样式
# 文本溢出隐藏
- 单行文本
p {
width: 200px;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
1
2
3
4
5
6
2
3
4
5
6
- 多行文本
p {
display: -webkit-box;
-webkit-box-orient: vertical;
-webkit-line-clamp: 3;
overflow: hidden;
}
1
2
3
4
5
6
2
3
4
5
6
# 中英文自动换行
word-break:break-all;只对英文起作用,以字母作为换行依据 word-wrap:break-word; 只对英文起作用,以单词作为换行依据 white-space:pre-wrap; 只对中文起作用,强制换行 white-space:nowrap; 强制不换行,都起作用
p {
word-wrap: break-word;
white-space: normal;
word-break: break-all;
}
//不换行
.wrap {
white-space: nowrap;
}
//自动换行
.wrap {
word-wrap: break-word;
word-break: normal;
}
//强制换行
.wrap {
word-break: break-all;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# 文字阴影
X-offset:指阴影居于字体水平偏移的位置。 Y-offset:指阴影居于字体垂直偏移的位置。 Blur:指阴影的模糊值。 color:指阴影的颜色。
p {
text-shadow: 5px 5px 5px #ff0000;
}
1
2
3
2
3
# 设置 placeholder 属性的字体样式
input::-webkit-input-placeholder {
/* Chrome/Opera/Safari */
color: red;
}
input::-moz-placeholder {
/* Firefox 19+ */
color: red;
}
input:-ms-input-placeholder {
/* IE 10+ */
color: red;
}
input:-moz-placeholder {
/* Firefox 18- */
color: red;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# IOS 页面滑动卡顿
body,
html {
-webkit-overflow-scrolling: touch;
}
1
2
3
4
2
3
4
# 滚动条样式
.test::-webkit-scrollbar {
/*滚动条整体样式*/
width: 10px; /*高宽分别对应横竖滚动条的尺寸*/
height: 1px;
}
.test::-webkit-scrollbar-thumb {
/*滚动条里面小方块*/
border-radius: 10px;
background-color: skyblue;
background-image: -webkit-linear-gradient(
45deg,
rgba(255, 255, 255, 0.2) 25%,
transparent 25%,
transparent 50%,
rgba(255, 255, 255, 0.2) 50%,
rgba(255, 255, 255, 0.2) 75%,
transparent 75%,
transparent
);
}
.test::-webkit-scrollbar-track {
/*滚动条里面轨道*/
box-shadow: inset 0 0 5px rgba(0, 0, 0, 0.2);
background: #ededed;
border-radius: 10px;
}
------------------------------------------------------------
//隐藏滚动条同时支持滚动
.demo::-webkit-scrollbar {
display: none; /* Chrome Safari */
}
.demo {
scrollbar-width: none; /* firefox */
-ms-overflow-style: none; /* IE 10+ */
overflow-x: hidden;
overflow-y: auto;
}
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
34
35
36
37
38
39
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
34
35
36
37
38
39
# 绘制三角形
div {
width: 0;
height: 0;
border-width: 0 40px 40px;
border-style: solid;
border-color: transparent transparent red;
}
-------------------------------------------------------------------------------
//带边框
<div id="blue"><div>
#blue {
position: relative;
width: 0;
height: 0;
border-width: 0 40px 40px;
border-style: solid;
border-color: transparent transparent blue;
}
#blue:after {
content: "";
position: absolute;
top: 1px;
left: -38px;
border-width: 0 38px 38px;
border-style: solid;
border-color: transparent transparent yellow;
}
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
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
# table 表格边框合并
table,
tr,
td {
border: 1px solid #666;
}
table {
border-collapse: collapse;
}
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
# css 选取第 n 个标签元素
- first-child 表示选择列表中的第一个标签
- last-child 表示选择列表中的最后一个标签
- nth-child(3) 表示选择列表中的第 3 个标签
- nth-child(2n) 这个表示选择列表中的偶数标签
- nth-child(2n-1) 这个表示选择列表中的奇数标签
- nth-child(n+3) 这个表示选择列表中的标签从第 3 个开始到最后
- nth-child(-n+3) 这个表示选择列表中的标签从 0 到 3,即小于 3 的标签
- nth-last-child(3) 这个表示选择列表中的倒数第 3 个标签
# onerror 处理图片异常
<img onerror="this.src='url;this.onerror=null'" />
1
# 文字间距
p{
text-indent:10px;//单词抬头距离
letter-spacing:10px;//间距
}
1
2
3
4
2
3
4
# 元素占满整个屏幕
.dom {
width: 100%;
height: 100vh;
}
1
2
3
4
2
3
4
# 文本两端对齐
.wrap {
text-align: justify;
text-justify: distribute-all-lines; //ie6-8
text-align-last: justify; //一个块或行的最后一行对齐方式
-moz-text-align-last: justify;
-webkit-text-align-last: justify;
}
1
2
3
4
5
6
7
2
3
4
5
6
7
# 文字竖向排版
// 单列展示时
.wrap {
width: 25px;
line-height: 18px;
height: auto;
font-size: 12px;
padding: 8px 5px;
word-wrap: break-word; /*英文的时候需要加上这句,自动换行*/
}
// 多列展示时
.wrap {
height: 210px;
line-height: 30px;
text-align: justify;
writing-mode: vertical-lr; //从左向右
writing-mode: tb-lr; //IE从左向右
//writing-mode: vertical-rl; -- 从右向左
//writing-mode: tb-rl; -- 从右向左
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# 禁止用户选择
.wrap {
-webkit-touch-callout: none;
-webkit-user-select: none;
-khtml-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
# 页面动画出现闪烁问题
.cube {
-webkit-backface-visibility: hidden;
backface-visibility: hidden;
-webkit-perspective: 1000;
perspective: 1000;
/* Other transform properties here */
}
-------------------------------------------------------------------- .cube {
-webkit-transform: translate3d(0, 0, 0);
transform: translate3d(0, 0, 0);
/* Other transform properties here */
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
2
3
4
5
6
7
8
9
10
11
12
13
14
# 字母大小写转换
p {
text-transform: uppercase;
} // 将所有字母变成大写字母
p {
text-transform: lowercase;
} // 将所有字母变成小写字母
p {
text-transform: capitalize;
} // 首字母大写
p {
font-variant: small-caps;
} // 将字体变成小型的大写字母
1
2
3
4
5
6
7
8
9
10
11
12
2
3
4
5
6
7
8
9
10
11
12
# 将容器设置为透明
.wrap {
filter: alpha(opacity=50);
-moz-opacity: 0.5;
-khtml-opacity: 0.5;
opacity: 0.5;
}
1
2
3
4
5
6
2
3
4
5
6
# transition 闪屏问题
.wrap {
-webkit-transform-style: preserve-3d;
-webkit-backface-visibility: hidden;
-webkit-perspective: 1000;
}
1
2
3
4
5
2
3
4
5
# 识别字符串里的 '\n' 并换行
body {
white-space: pre-line;
}
1
2
3
2
3
# 移除 a 标签被点链接的边框
a {
outline: none;//或者outline: 0
text-decoration:none; //取消默认下划线
}
1
2
3
4
2
3
4
# 显示链接之后的 URL
<a href="https://www.baidu.com">百度</a>
<style>
a:after {
content: " (" attr(href) ")";
}
</style>
1
2
3
4
5
6
2
3
4
5
6
# select 内容居中显示、下拉内容右对齐
select {
text-align: center;
text-align-last: center;
}
select option {
direction: rtl;
}
1
2
3
4
5
6
7
2
3
4
5
6
7
# 修改 input 输入框中光标的颜色不改变字体的颜色
input {
color: #fff;
caret-color: red;
}
1
2
3
4
2
3
4
# 子元素固定宽度 父元素宽度被撑开
// 父元素下的子元素是行内元素
.wrap {
white-space: nowrap;
}
// 若父元素下的子元素是块级元素
.wrap {
white-space: nowrap; // 子元素不被换行
display: inline-block;
}
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
# div 里的图片和文字上下居中
.wrap {
height: 100,
line-height: 100
}
img {
vertival-align:middle
}
// vertical-align css的属性vertical-align用来指定行内元素(inline)或表格单元格(table-cell)元素的垂直对齐方式。
只对行内元素、表格单元格元素生效,不能用它垂直对齐块级元素
// vertical-align:baseline/top/middle/bottom/sub/text-top;
1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
# 宽高等比例自适应矩形
.scale { width: 100%; padding-bottom: 56.25%; height: 0; position: relative; }
.item { position: absolute; width: 100%; height: 100%; background-color:
#499e56; }
<div class="scale">
<div class="item">这里是所有子元素的容器</div>
</div>
1
2
3
4
5
6
2
3
4
5
6
# css3 动画
# 旋转动画
<div class="loader"></div>
<style>
.loader {
border: 16px solid #f3f3f3;
border-radius: 50%;
border-top: 16px solid #3498db;
width: 80px;
height: 80px;
-webkit-animation: spin 2s linear infinite;
animation: spin 2s linear infinite;
}
@-webkit-keyframes spin {
0% {
-webkit-transform: rotate(0deg);
}
100% {
-webkit-transform: rotate(360deg);
}
}
@keyframes spin {
0% {
transform: rotate(0deg);
}
100% {
transform: rotate(360deg);
}
}
</style>
### 文字/背景渐变 ```html
<div class="text_signature ">前端css动画</div>
<style>
.text_signature {
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
background-image: linear-gradient(to right, #ec2239, #40a4e2, #ea96f5);
width: 320px;
}
</style>
-----------------------------------------------------------------------------------------
// 背景渐变色
<div class="text_gradient"></div>
<style>
.text_gradient {
width: 500px;
height: 100px;
background: linear-gradient(
25deg,
rgb(79, 107, 208),
rgb(98, 141, 185),
rgb(102, 175, 161),
rgb(92, 210, 133)
) rgb(182, 228, 253);
}
</style>
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# 边框阴影
<div class="text_shadow"></div>
<style>
.text_shadow {
width: 500px;
height: 100px;
box-shadow: 0px 0px 13px 1px rgba(51, 51, 51, 0.1);
}
</style>
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
# 文字立体效果
<div class="text_solid">文字立体效果</div>
<style>
.text_solid {
font-size: 32px;
text-align: center;
font-weight: bold;
line-height: 100px;
text-transform: uppercase;
position: relative;
background-color: #333;
color: #fff;
text-shadow: 0px 1px 0px #c0c0c0, 0px 2px 0px #b0b0b0, 0px 3px 0px #a0a0a0, 0px
4px 0px #909090, 0px 5px 10px rgba(0, 0, 0, 0.6);
}
</style>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# 全屏背景图
.swper {
background-image: url(./img/bg.jpg);
width: 100%;
height: 100%; //父级高不为100%请使用100vh
zoom: 1;
background-color: #fff;
background-repeat: no-repeat;
background-size: cover;
-webkit-background-size: cover;
-o-background-size: cover;
background-position: center 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
2
3
4
5
6
7
8
9
10
11
12
# 文字描边
.stroke {
-webkit-text-stroke: 1px greenyellow;
text-stroke: 1px greenyellow;
}
------------------------------------------------------------------------
.stroke {
text-shadow: #000 1px 0 0, #000 0 1px 0, #000 -1px 0 0, #000 0 -1px 0;
-webkit-text-shadow: #000 1px 0 0, #000 0 1px 0, #000 -1px 0 0, #000 0 -1px 0;
-moz-text-shadow: #000 1px 0 0, #000 0 1px 0, #000 -1px 0 0, #000 0 -1px 0;
*filter: Glow(color=#000, strength=1);
}
1
2
3
4
5
6
7
8
9
10
11
12
2
3
4
5
6
7
8
9
10
11
12
# 解决 1px 边框变粗问题
比如在 2 倍屏时 1px 的像素实际对应 2 个物理像素
.dom {
height: 1px;
background: #dbdbdb;
transform: scaleY(0.5);
}
1
2
3
4
5
2
3
4
5
# css 不同单位运算
.div {
width: calc(100% - 50px);
}
1
2
3
2
3
# 文字模糊效果
.vague_text {
color: transparent;
text-shadow: #111 0 0 5px;
}
1
2
3
4
2
3
4
# 通过滤镜让图标变灰
<a href="" class="icon"><img src="01.jpg" /></a>
<style>
.icon {
-webkit-filter: grayscale(100%);
-moz-filter: grayscale(100%);
-ms-filter: grayscale(100%);
-o-filter: grayscale(100%);
filter: grayscale(100%);
filter: gray;
}
.icon:hover {
filter: none;
-webkit-filter: grayscale(0%);
}
</style>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
2
3
4
5
6
7
8
9
10
11
12
13
14
15
上次更新: 2022/09/06, 10:30:04