分享日常开发过程中遇到的常见 CSS 代码,来看看有没有你熟悉的代码,相信这么多的场景能为你节省一些些时间,建议收藏起来,说不定哪天就用上啦~
overscroll-behavior-x
内部盒子滚动到边界的时候,将触发整个页面滚动,可通过设置overscroll-behavior-x阻止此行为
div { height: 300px; width: 500px; overflow: auto; overscroll-behavior-x: contain;}
复制代码
使用 sroll-snap-type 优化滚动
在实际应用中,应用在全屏滚动或 banner 上有很多用武之地,不需要原始的各种尺寸位置计算
ul { scroll-snap-type: x mandatory;} li { scroll-snap-align: center;}
复制代码
横竖虚线
css 自带的虚线在某些设计场景下不够用,通过linear-gradient绘制虚线。
// 横虚线.dashed { height: 1px; width: 100px; background: linear-gradient(to right, #000, #000 5px, transparent 5px, transparent); background-size: 10px 100%;}
// 竖虚线.dashed { background: linear-gradient(to bottom, #000, #000 3.2px,transparent 3.2px, transparent); background-size: 100% 10px; width: 1px; height: 100px;}
复制代码
画格子
基于linear-gradient 渐变画格子,格子的角度及条纹之间的间隙可自行调整。
background-image: linear-gradient( 90deg, rgba(52,83,139,.5) 50%, transparent 50%), linear-gradient( rgba(52,83,139,.5) 50%, transparent 50%)
复制代码
改变输入框光标的颜色
解决 IOS 滚动条卡顿
body,html{ -webkit-overflow-scrolling: touch;}
复制代码
隐藏滚动条
.box::-webkit-scrollbar { display: none;}
复制代码
webkit 下修改滚动条样式
::-webkit-scrollbar 滚动条整体部分
::-webkit-scrollbar-button 滚动条两端的按钮
:-webkit-scrollbar-track 外层轨道
::-webkit-scrollbar-track-piece 内层滚动区
::-webkit-scrollbar-thumb 滚动的滑块
::-webkit-scrollbar-corner 边角
::-webkit-resizer 定义右下角拖动块的样式
::-webkit-scrollbar { width: 12px;}::-webkit-scrollbar-track { -webkit-box-shadow: inset 0 0 6px rgba(0,0,0,0.4); border-radius: 10px;}::-webkit-scrollbar-thumb { border-radius: 10px; background: rgba(0,0,0,0.1); -webkit-box-shadow: inset 0 0 6px rgba(0,0,0,0.6);}::-webkit-scrollbar-thumb:window-inactive { background: rgba(255,0,0,0.4);}
复制代码
开启硬件加速
开启了 3D 模式后的 transition 过渡在 IOS 中闪屏,卡顿,可设置开启硬件加速解决
.dom { backface-visibility: hidden; perspective: 1000;}
复制代码
在 webkit 内核的浏览器中,另一个行之有效的方法是
.dom { transform: translate3d(0, 0, 0);}
复制代码
清除浮动
@mixin clearfix() { &::after { content: ''; display: table; clear: both; }}
复制代码
文本溢出显示省略号
单行文本溢出,定义 ellipsis 函数方便调用
@mixin ellipsis() { overflow: hidden; white-space: nowrap; text-overflow: ellipsis;}
.ellipsis { @include ellipsis();}
复制代码
多行文本溢出,定义 multi-ellipsis 函数方便调用,line 是对应的行数
@mixin multi-ellipsis($line: 1) { @if $line <= 0 { $line: 1; }
overflow: hidden; text-overflow: ellipsis; display: -webkit-box; -webkit-line-clamp: $line; -webkit-box-orient: vertical;}
.ellipsis-1 { @include multi-ellipsis(1);}
.ellipsis-2 { @include multi-ellipsis(2);}
复制代码
使用:not() 解决最后一个元素特殊样式
// before.nav li { border-right: 1px solid #666; }.nav li:last-child { border-right: none; }
// after.nav li:not(:last-child) { border-right: 1px solid #666; }
复制代码
让列表项看上去像一个真正的用逗号分隔的列表
ul > li:not(:last-child)::after { content: ",";}
复制代码
安卓 CSS 不识别边框 0.5px 解决办法
// 按钮边框.border { position: relative; &::after { content: ''; position: absolute; width: 200%; height: 200%; left: -50%; top: -50%; border: 1px solid #000; border-radius: 4px; transform: scale(0.5); box-sizing: border-box; }}
复制代码
CSS3 nth-child()选择器
1、选择列表中的偶数标签 :nth-child(2n)2、选择列表中的奇数标签 :nth-child(2n-1)3、选择从第 6 个开始的,直到最后:nth-child(n+6)4、选择第 1 个到第 6 个 :nth-child(-n+6)5、两者结合使用,可以限制选择某一个范围,选择第 6 个到第 9 个 :nth-child(n+6):nth-child(-n+9)
禁止文字选中
*:not(input,textarea) { -webkit-user-select:none; user-select:none; -webkit-tap-highlight-color:rgba(255,0,0,0);}
复制代码
显示链接名称的同时并显示当前 URL
<a href="http://www.baidu.com">baidu</a><style>a::after { content: " (" attr(href) ")";}</style>
复制代码
当 a 标签没有文本值,但 href 属性有链接的时候显示链接
a[href^="http"]:empty::before { content: attr(href);}
复制代码
利用 border 实现三角形
要实现不同方向的三角形改变其 border 的代码,三角形底部对应两侧的颜色为透明。
.arrow-up { width: 0px; height: 0px; border-left: 5px solid transparent; border-right: 5px solid transparent; border-bottom: 5px solid #2f2f2f; font-size: 0px; line-height: 0px;}
复制代码
图片灰度滤镜
img { filter: gray; -webkit-filter: grayscale(1);}
复制代码
自定义选中文本颜色
::selection { background: #00ff00;} ::-moz-selection { background: #00ff00;} ::-webkit-selection { background: #00ff00;}
复制代码
禁用鼠标事件
.disabled { pointer-events: none; }
复制代码
参考
专注前端开发,分享前端相关技术干货,公众号:南城大前端(ID: nanchengfe)
评论