实用的 CSS 样式

MuYan2022-03-07web

极端数据换行

例如:全是 1111、全是连体的英文字符、全是单个汉字等

.text-wrap {
  word-wrap: break-word;
  white-space: pre-wrap;
  word-break: break-all;
}

文本省略号限制

.line-1 {
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}

.line-2 {
  -webkit-line-clamp: 2;
}

.line-3 {
  -webkit-line-clamp: 3;
}

.line-4 {
  -webkit-line-clamp: 4;
}

.line-5 {
  -webkit-line-clamp: 5;
}

.line-2,
.line-3,
.line-4,
.line-5 {
  overflow: hidden;
  word-break: break-all;
  text-overflow: ellipsis;
  display: -webkit-box; // 弹性伸缩盒
  -webkit-box-orient: vertical; // 设置伸缩盒子元素排列方式
}

样式动态计算

.demo {
  width: calc(100% - 200px);
}

iPhoneX 底部安全区定义

[底部安全区](/blogs/web/shiyongde CSS yangshi.html#安全区域)

.safe-area-inset-bottom {
  padding-bottom: 0;
  padding-bottom: constant(safe-area-inset-bottom);
  padding-bottom: env(safe-area-inset-bottom);
}

去除 webkit 的默认样式

.fix-ios-appearance {
  -webkit-appearance: none;
}

伪类边框

.border,
.border-bottom,
.border-left,
.border-right,
.border-top,
.border-top-bottom {
  position: relative;
}

.border-bottom:after,
.border-left:after,
.border-right:after,
.border-top-bottom:after,
.border-top:after,
.border:after {
  content: "";
  position: absolute;
  left: 0;
  top: 0;
  pointer-events: none;
  box-sizing: border-box;
  -webkit-transform-origin: 0 0;
  transform-origin: 0 0;
  // 多加0.1%,能解决有时候边框缺失的问题
  width: 199.8%;
  height: 199.7%;
  transform: scale(0.5, 0.5);
  border: 0 solid #eeeeee;
  z-index: 2;
}

.border-top:after {
  border-top-width: 1px;
}

.border-left:after {
  border-left-width: 1px;
}

.border-right:after {
  border-right-width: 1px;
}

.border-bottom:after {
  border-bottom-width: 1px;
}

.border-top-bottom:after {
  border-width: 1px 0;
}

.border:after {
  border-width: 1px;
}

移动端去除 input 框获取焦点高亮问题

input {
  outline: none;
}

...不定时更新

上次更新 2026/6/23 11:49:15
评论
  • 按正序
  • 按倒序
  • 按热度
Powered by Waline v2.15.8