Skip to content

常用的一些CSS样式

文本溢出隐藏

  • 单行文本
css
p {
  width: 200px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
  • 多行文本
css
p {
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 3;
  overflow: hidden;
}

中英文自动换行

word-break:break-all;只对英文起作用,以字母作为换行依据 word-wrap:break-word; 只对英文起作用,以单词作为换行依据 white-space:pre-wrap; 只对中文起作用,强制换行 white-space:nowrap; 强制不换行,都起作用

css
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;
}

文字阴影

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

css
p {
  text-shadow: 5px 5px 5px #ff0000;
}

设置 placeholder 属性的字体样式

css
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;
}

IOS 页面滑动卡顿

css
body,
html {
  -webkit-overflow-scrolling: touch;
}

滚动条样式

css
.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;
}

绘制三角形

css
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;
}

table 表格边框合并

css
table,
tr,
td {
  border: 1px solid #666;
}
table {
  border-collapse: collapse;
}

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 处理图片异常

html
<img onerror="this.src='url;this.onerror=null'" />

文字间距

css
p{
  text-indent:10px;//单词抬头距离
  letter-spacing:10px;//间距
}

元素占满整个屏幕

css
.dom {
  width: 100%;
  height: 100vh;
}

文本两端对齐

css
.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;
}

文字竖向排版

css
// 单列展示时
.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;        -- 从右向左
}

禁止用户选择

css
.wrap {
  -webkit-touch-callout: none;
  -webkit-user-select: none;
  -khtml-user-select: none;
  -moz-user-select: none;
  -ms-user-select: none;
  user-select: none;
}

页面动画出现闪烁问题

css
.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 */
}

字母大小写转换

css
p {
  text-transform: uppercase;
} // 将所有字母变成大写字母
p {
  text-transform: lowercase;
} // 将所有字母变成小写字母
p {
  text-transform: capitalize;
} // 首字母大写
p {
  font-variant: small-caps;
} // 将字体变成小型的大写字母

将容器设置为透明

css
.wrap {
  filter: alpha(opacity=50);
  -moz-opacity: 0.5;
  -khtml-opacity: 0.5;
  opacity: 0.5;
}

transition 闪屏问题

css
.wrap {
  -webkit-transform-style: preserve-3d;
  -webkit-backface-visibility: hidden;
  -webkit-perspective: 1000;
}

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

css
body {
  white-space: pre-line;
}

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

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

显示链接之后的 URL

html
<a href="https://www.baidu.com">百度</a>
<style>
  a:after {
    content: " (" attr(href) ")";
  }
</style>

select 内容居中显示、下拉内容右对齐

css
select {
  text-align: center;
  text-align-last: center;
}
select option {
  direction: rtl;
}

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

css
input {
  color: #fff;
  caret-color: red;
}

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

css
// 父元素下的子元素是行内元素
.wrap {
  white-space: nowrap;
}
// 若父元素下的子元素是块级元素
.wrap {
  white-space: nowrap; // 子元素不被换行
  display: inline-block;
}

div 里的图片和文字上下居中

css
.wrap {
  height: 100,
  line-height: 100
}
img {
  vertival-alignmiddle
}
// vertical-align css的属性vertical-align用来指定行内元素(inline)或表格单元格(table-cell)元素的垂直对齐方式。
只对行内元素、表格单元格元素生效,不能用它垂直对齐块级元素
// vertical-align:baseline/top/middle/bottom/sub/text-top;

宽高等比例自适应矩形

html
.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>

css3 动画

旋转动画

html
<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>

边框阴影

html
<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>

文字立体效果

html
<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>

全屏背景图

css
.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;
}

文字描边

css
.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);
}

解决 1px 边框变粗问题

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

css
.dom {
  height: 1px;
  background: #dbdbdb;
  transform: scaleY(0.5);
}

css 不同单位运算

css
.div {
  width: calc(100% - 50px);
}

文字模糊效果

css
.vague_text {
  color: transparent;
  text-shadow: #111 0 0 5px;
}

通过滤镜让图标变灰

html
<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>