IMAGINE'S BLOG IMAGINE'S BLOG
首页
  • 原生JS

    • JavaScript
  • 前端框架扩展

    • Vue
    • React
    • UI组件库
  • HTML
  • CSS
  • 浏览器
  • 分类
  • 标签
  • 归档
  • 技术文档
  • GitHub相关
  • Nodejs
关于
  • 网站
  • 友情链接
GitHub (opens new window)

peng

平平无奇的web前端开发一枚
首页
  • 原生JS

    • JavaScript
  • 前端框架扩展

    • Vue
    • React
    • UI组件库
  • HTML
  • CSS
  • 浏览器
  • 分类
  • 标签
  • 归档
  • 技术文档
  • GitHub相关
  • Nodejs
关于
  • 网站
  • 友情链接
GitHub (opens new window)
  • HTML

  • CSS

    • CSS教程和技巧收藏
    • flex布局语法
    • flex布局案例-基础
    • flex布局案例-骰子
    • flex布局案例-圣杯布局
    • flex布局案例-网格布局
    • flex布局案例-输入框布局
    • CSS3之transition过渡
    • CSS3之animation动画
    • 「布局技巧」图片未加载前自动撑开元素高度
    • 文字在一行或多行时超出显示省略号
    • 从box-sizing属性入手,了解盒子模型
    • css水平垂直居中
    • 如何根据系统主题自动响应CSS深色模式
    • 「css技巧」使用hover和attr()定制悬浮提示
    • CSS给table的tbody添加滚动条
    • 常用的一些CSS样式
      • 文本溢出隐藏
      • 中英文自动换行
      • 文字阴影
      • 设置 placeholder 属性的字体样式
      • IOS 页面滑动卡顿
      • 滚动条样式
      • 绘制三角形
      • table 表格边框合并
      • css 选取第 n 个标签元素
      • onerror 处理图片异常
      • 文字间距
      • 元素占满整个屏幕
      • 文本两端对齐
      • 文字竖向排版
      • 禁止用户选择
      • 页面动画出现闪烁问题
      • 字母大小写转换
      • 将容器设置为透明
      • transition 闪屏问题
      • 识别字符串里的 '\n' 并换行
      • 移除 a 标签被点链接的边框
      • 显示链接之后的 URL
      • select 内容居中显示、下拉内容右对齐
      • 修改 input 输入框中光标的颜色不改变字体的颜色
      • 子元素固定宽度 父元素宽度被撑开
      • div 里的图片和文字上下居中
      • 宽高等比例自适应矩形
      • css3 动画
        • 旋转动画
        • 边框阴影
        • 文字立体效果
      • 全屏背景图
      • 文字描边
      • 解决 1px 边框变粗问题
      • css 不同单位运算
      • 文字模糊效果
      • 通过滤镜让图标变灰
  • 页面杂谈
  • CSS
peng
2022-09-05
目录

常用的一些CSS样式

# 文本溢出隐藏

  • 单行文本
p {
  width: 200px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
1
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

# 中英文自动换行

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

# 文字阴影

X-offset:指阴影居于字体水平偏移的位置。 Y-offset:指阴影居于字体垂直偏移的位置。 Blur:指阴影的模糊值。 color:指阴影的颜色。

p {
  text-shadow: 5px 5px 5px #ff0000;
}
1
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

# IOS 页面滑动卡顿

body,
html {
  -webkit-overflow-scrolling: touch;
}
1
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

# 绘制三角形

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

# table 表格边框合并

table,
tr,
td {
  border: 1px solid #666;
}
table {
  border-collapse: collapse;
}
1
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

# 元素占满整个屏幕

.dom {
  width: 100%;
  height: 100vh;
}
1
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

# 文字竖向排版

// 单列展示时
.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

# 禁止用户选择

.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

# 页面动画出现闪烁问题

.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

# 字母大小写转换

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

# 将容器设置为透明

.wrap {
  filter: alpha(opacity=50);
  -moz-opacity: 0.5;
  -khtml-opacity: 0.5;
  opacity: 0.5;
}
1
2
3
4
5
6

# transition 闪屏问题

.wrap {
  -webkit-transform-style: preserve-3d;
  -webkit-backface-visibility: hidden;
  -webkit-perspective: 1000;
}
1
2
3
4
5

# 识别字符串里的 '\n' 并换行

body {
  white-space: pre-line;
}
1
2
3

# 移除 a 标签被点链接的边框

a {
  outline: none;//或者outline: 0
  text-decoration:none; //取消默认下划线
}
1
2
3
4

# 显示链接之后的 URL

<a href="https://www.baidu.com">百度</a>
<style>
  a:after {
    content: " (" attr(href) ")";
  }
</style>
1
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

# 修改 input 输入框中光标的颜色不改变字体的颜色

input {
  color: #fff;
  caret-color: red;
}
1
2
3
4

# 子元素固定宽度 父元素宽度被撑开

// 父元素下的子元素是行内元素
.wrap {
  white-space: nowrap;
}
// 若父元素下的子元素是块级元素
.wrap {
  white-space: nowrap; // 子元素不被换行
  display: inline-block;
}
1
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

# 宽高等比例自适应矩形

.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

# 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

# 边框阴影

<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

# 文字立体效果

<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

# 全屏背景图

.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

# 文字描边

.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

# 解决 1px 边框变粗问题

比如在 2 倍屏时 1px 的像素实际对应 2 个物理像素

.dom {
  height: 1px;
  background: #dbdbdb;
  transform: scaleY(0.5);
}
1
2
3
4
5

# css 不同单位运算

.div {
  width: calc(100% - 50px);
}
1
2
3

# 文字模糊效果

.vague_text {
  color: transparent;
  text-shadow: #111 0 0 5px;
}
1
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
#CSS
上次更新: 2022/09/06, 10:30:04
CSS给table的tbody添加滚动条

← CSS给table的tbody添加滚动条

最近更新
01
Axios 封装
09-06
02
MySQL数据库常用操作
09-06
03
解决element表格数据量过大导致页面渲染缓慢、卡顿问题
09-06
更多文章>
Theme by Vdoing | Copyright © 2020-2024 peng | IMAGINE
image | imgloc.com
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式