CSS那些超好用的样式

CSS那些超好用的样式和三角形原理相同 但是需要设置宽度或者高度其中之一 边框依然只保留其中一个颜色 其余皆设置为透明 但两边要比保留的那一边要小

大家好,欢迎来到IT知识分享网。

1、选择第n个标签

<div class="content"> <span class="one"></span> <span class="two"></span> <span class="three"></span> </div> 

选择content下第一个标签

.content { 
    span:first-child { 
    color: red; } } 

选择content下最后一个标签

.content { 
    span:last-child { 
    color: red; } } 

选择content下第n个标签

.content { 
    span:nth-child(n) { 
    color: red; } } 

2、border、padding不计入宽高

box-sizing: border-box; -moz-box-sizing: border-box; /* Firefox */ -webkit-box-sizing: border-box; /* Safari */ 

3、把图片设置为背景

background: url("~@/assets/images/login/bg.png") center center no-repeat; background-size: 100% 100%; 

4、伪类样式

以tab栏下划线举例。

content: ""; width: 60%; height: 0.04rem; background-color: $blue; position: absolute; bottom: -6px; left: 50%; transform: translate(-50%, 0); 

常用伪类

  1. :hover
  2. :active
  3. :before
  4. :after

5、文字溢出省略号

单行

white-space: nowrap; overflow: hidden; text-overflow: ellipsis; 

多行

overflow: hidden; text-overflow: ellipsis; display: -webkit-box; -webkit-line-clamp: 2; // 几行省略 -webkit-box-orient: vertical; 

注意:多行省略不能有 padding-bottom,否则下面的文字会显示出来。

6、纯字母换行

word-break: break-all; // 只对英文起作用,以字母作为换行依据 word-wrap: break-word; // 只对英文起作用,以单词作为换行依据 

7、三角形、梯形

三角形: 元素本身没有画三角形的样式,但是边框两边是斜角,所以只要把元素宽高设为0,再把边框设成透明,保留其中一个边框即可

width: 0; height: 0; border-bottom: 100px solid #333; border-left: 100px solid transparent; border-right: 100px solid transparent; 

梯形: 和三角形原理相同,但是需要设置宽度或者高度其中之一,边框依然只保留其中一个颜色,其余皆设置为透明,但两边要比保留的那一边要小

width: 100px; height: 0; border-bottom: 100px solid #333; border-left: 30px solid transparent; border-right: 30px solid transparent; 

8、事件穿透

pointer-events: none; // 捕获不到任何点击,让事件穿透到它的下面,默认auto 

9、文字水平对齐

text-align: right; text-align: left; text-align: center; 

10、绝对定位

举例: 垂直水平居中

// 父元素 position: relative; // 子元素 position: absolute; left: 50%; top: 50%; transform: translate(-50%, -50%); 

11、滚动条

  1. 修改滚动条颜色和粗细
::-webkit-scrollbar-track-piece { 
    background: #ebebeb; } ::-webkit-scrollbar { 
    width: 6px; } ::-webkit-scrollbar-thumb { 
    background: #c9c9c9; border-radius: 3px; } 
  1. 隐藏滚动条
::-webkit-scrollbar-track-piece { 
    background: #ebebeb; } ::-webkit-scrollbar { 
    width: 0px; } ::-webkit-scrollbar-thumb { 
    background: #c9c9c9; border-radius: 0px; } 

免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://haidsoft.com/117207.html

(0)
上一篇 2025-11-22 21:20
下一篇 2025-11-22 21:33

相关推荐

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注微信