首页 > IT职业 > > 55个提高你CSS开发效率的必备片段
(给前端宇宙加星标,提升你的前端小宇宙.)
作者:Eno_Yao
https://github.com/Wscats/CV/issues/2
<div style="clear:both;"></div>;不过这样会增加很多无用的代码。此时我们用:after这个伪元素来解决浮动的问题,如果当前层级有浮动元素,那么在其父级添加上 clearfix 类即可。清除浮动:after {content: "\00A0";display: block;visibility: hidden;width: 0;height: 0;clear: both;: 0;: 0;overflow: hidden;}{zoom: 1;}
绝对定位方式且已知宽高position: absolute;top: 50%;left: 50%;margin-top: -3em;margin-left: -7em;width: 14em;height: 6em;
绝对定位 + 未知宽高 + translateposition: absolute;left: 50%;top: 50%;transform: translate(-50%, -50%);//需要补充浏览器前缀
flex 轻松搞定水平垂直居中(未知宽高)display: flex;align-items: center;justify-content: center;
宽度固定,适合单行显示...overflow: hidden;text-overflow: ellipsis;white-space: nowrap;
宽度不固定,适合多行以及移动端显示overflow: hidden;text-overflow: ellipsis;display: -webkit-box;-webkit-line-clamp: 3;-webkit-box-orient: vertical;
color: transparent;text-shadow: 0 0 2px rgba(0, 0, 0, 0.5);
:after {display: inline-block;overflow: hidden;: bottom;content: "\2026";: ellipsis 2s infinite;}动画部分ellipsis {from {width: 2px;}to {width: 15px;}}
注意只能修改这两个属性 字体颜色 选中背景颜色element::selection {color: green;: pink;}element::-moz-selection {color: green;: pink;}
html<div class="wrap"><div class="ribbon"><a href="#">Fork me on GitHub</a></div></div>
css/* 外层容器基本设置 */.wrap { width: 160px; height: 160px; overflow: hidden; position: relative; background-color: #f3f3f3;}.ribbon { background-color: #a00; overflow: hidden; white-space: nowrap; position: absolute; /* shadom */ -webkit-box-shadow: 10px #888; -moz-box-shadow: 10px #888; box-shadow: 10px #888; /* rotate */ -webkit-transform: rotate(-45deg); -moz-transform: rotate(-45deg); -ms-transform: rotate(-45deg); -o-transform: rotate(-45deg); transform: rotate(-45deg); /* position */ left: -50px; top: 40px;}.ribbon a { border: 1px solid #faa; color: #fff; display: block; font: bold 81.25% "Helvetica Neue", Helvetica, Arial, sans-serif; margin: 1px ; padding: 10px 50px; text-align: center; text-decoration: none; /* shadow */ text-shadow: 5px #444;}input::-webkit-input-placeholder {color: green;background-color: #f9f7f7;font-size: 14px;}input::-moz-input-placeholder {color: green;background-color: #f9f7f7;font-size: 14px;}input::-ms-input-placeholder {color: green;background-color: #f9f7f7;font-size: 14px;}
-webkit-tap-highlight-color: rgba(255, 255, 255, 0);
element:first-letter {float: left;color: green;font-size: 30px;}
.triangle {/* 基础样式 */border: solid 10px transparent;}/*下*/.triangle.bottom {border-top-color: green;}/*上*/.triangle.top {border-bottom-color: green;}/*左*/.triangle.top {border-right-color: green;}/*右*/.triangle.top {border-left-color: green;}
a
submit
input[type="iamge"]
input[type="button"]
button
label
select
a[href],input[type="submit"],input[type="image"],input[type="button"],label[for],select,button {cursor: pointer;}
-webkit-touch-callout: none;-webkit-user-select: none;-khtml-user-select: none;-moz-user-select: none;-ms-user-select: none;user-select: none;
body,p,h1,h2,h3,h4,h5,h6,dl,dd,ul,ol,th,td,button,figure,input,textarea,form {margin: ;padding: ;}
input,select,textarea {-webkit-box-sizing: content-box;-moz-box-sizing: content-box;box-sizing: content-box;}table {/*table 相邻单元格的边框间的距离设置为 0*/border-spacing: ;/*默认情况下给 tr 设置 border 没有效果,如果 table 设置了边框为合并模式:「border-collapse: collapse;」就可以了*/border-collapse: collapse;}
img,input,button,textarea {border: none;-webkit-appearance: none;}input {/*由于 input 默认不继承父元素的居中样式,所以设置:「text-align: inherit」*/text-align: inherit;}textarea {/*textarea 默认不可以放缩*/resize: none;}
a,h1,h2,h3,h4,h5,h6,input,select,button,option,textarea,optgroup {font-family: inherit;font-size: inherit;font-weight: inherit;font-style: inherit;line-height: inherit;color: inherit;outline: none;}
a {text-decoration: none;}ol,ul {/*开发中 UI 设计的列表都是和原生的样式差太多,所以直接给取消 ol,ul 默认列表样式*/list-style: none;}button,input[type="submit"],input[type="button"] {/*鼠标经过是「小手」形状表示可点击*/cursor: pointer;}input::-moz-focus-inner {/*取消火狐浏览器部分版本 input 聚焦时默认的「padding、border」*/padding: ;border: ;}
input[type="number"] {-moz-appearance: textfield;}input[type="number"]::-webkit-inner-spin-button,input[type="number"]::-webkit-outer-spin-button {margin: ;-webkit-appearance: none;}
input::-webkit-input-placeholder,textarea::-webkit-input-placeholder {color: #999;}input:-moz-placeholder,textarea:-moz-placeholder {color: #999;}input::-moz-placeholder,textarea::-moz-placeholder {color: #999;}input:-ms-input-placeholder,textarea:-ms-input-placeholder {color: #999;}template {/*由于部分浏览 template 会显示出来,所以要隐*/display: none;}
.pf {position: fixed;/*chrome 内核 浏览器 position: fixed 防止抖动*/-webkit-transform: translateZ(0);}
.middle {position: absolute;top: ;right: ;bottom: ;left: ;margin: auto;}
.v-middle {position: relative;top: 50%;-webkit-transform: -webkit-translateY(-50%);-moz-transform: -moz-translateY(-50%);-o-transform: -o-translateY(-50%);transform: translateY(-50%);}
.bb {-webkit-box-sizing: border-box;-moz-box-sizing: border-box;box-sizing: border-box;}
.to {overflow: hidden;white-space: nowrap;text-overflow: ellipsis;}
body,h1,h2,h3,h4,h5,h6,hr,p,blockquote,dl,dt,dd,ul,ol,li,pre,form,fieldset,legend,button,input,textarea,th,td {margin: ;padding: ;}body,button,input,select,textarea {font: 12px/1.5 tahoma, arial, \5b8b\4f53;}h1,h2,h3,h4,h5,h6 {font-size: 100%;}address,cite,dfn,em,var {font-style: normal;}code,kbd,pre,samp {font-family: couriernew, courier, monospace;}small {font-size: 12px;}ul,ol {list-style: none;}a {text-decoration: none;}a:hover {text-decoration: underline;}sup {vertical-align: text-top;}sub {vertical-align: text-bottom;}legend {color: #000;}fieldset,img {border: ;}button,input,select,textarea {font-size: 100%;}table {border-collapse: collapse;border-spacing: ;}
/* 强制不换行 */div {white-space: nowrap;}/* 自动换行 */div {word-wrap: break-word;word-break: normal;}/* 强制英文单词断行 */div {word-break: break-all;}
table {border: 1px solid #000;padding: ;border-collapse: collapse;table-layout: fixed;margin-top: 10px;}table td {height: 30px;border: 1px solid #000;background: #fff;font-size: 15px;padding: 3px 3px 3px 8px;color: #000;width: 160px;}
.container {position: relative;width: 300px;height: 200px;border: 1px solid #333333;}.content {background-color: #ccc;width: 160px;height: 100px;position: absolute;top: 50%;left: 50%;margin-left: -80px; /* 宽度的一半 */margin-top: -50px; /* 高度的一半 */}
.container {position: relative;width: 300px;height: 200px;border: 1px solid #333333;}.content {background-color: #ccc;position: absolute;top: 50%;left: 50%;transform: translate3d(-50%, -50%, 0);text-align: center;}
line-height其实是行高,我们可以用行高来调整布局!.container {width: 300px;height: 200px;border: 1px solid #333333;}.content {line-height: 200px;}
display: table,当前元素设置display: table-cell:.container {width: 300px;height: 200px;border: 1px solid #333333;display: table;}.content {display: table-cell;vertical-align: middle;text-align: center;}
display: flex,利用 flex 中的align-items和justify-content设置垂直方向和水平方向的居中。这种方式也不限制中间元素的宽度和高度。line-height方案在某些 Android 机型中文字不居中的问题。.container {width: 300px;height: 200px;border: 1px solid #333333;display: flex;align-items: center;justify-content: center;}.content {background-color: #ccc;text-align: center;}
.content {width: 400px;height: 400px;border: 1px solid #ccc;text-align: center;display: table-cell;vertical-align: middle;}
<div class="content"><img src="./4.jpg" alt="img" /></div>
border-top属性,然后再把中间的文字进行绝对定位,同时给这个文字一个背景颜色,把中间的这部分盖住。<div class="title">标题</div>
title {color: #e1767c;: 0.3rem;position: relative;:before,:after {content: "";position: absolute;display: block;left: 50%;top: 50%;: translate3d(-50%, -50%, 0);transform: translate3d(-50%, -50%, 0);: 0.02rem solid #e1767c;width: 0.4rem;}:before {: -1.2rem;}:after {: 1.2rem;}}
<div class="triangle1"></div><div class="triangle2"></div><div class="trapezoid"></div>
.triangle1 {/*锐角三角形*/width: ;height: ;border-top: 50px solid transparent;border-bottom: 100px solid #249ff1;border-left: 30px solid transparent;border-right: 100px solid transparent;}.triangle2 {/*直角三角形*/width: ;height: ;border-top: 80px solid transparent;border-bottom: 80px solid #ff5b01;border-left: 50px solid #ff5b01;border-right: 50px solid transparent;}.trapezoid {/*梯形*/width: ;height: ;border-top: none;border-right: 80px solid transparent;border-bottom: 60px solid #13dbed;border-left: 80px solid #13dbed;}
border-radius主要用于绘制圆点、圆形、椭圆、圆角矩形等形状,以下为简单绘制的两个图形。<div class="circle"></div><div class="ellipse"><div></div></div>
.circle,.ellipse {width: 100px;height: 100px;background: #249ff1;border-radius: 50%;}.ellipse {width: 150px;background: #ff9e01;}
border-radius属性实际上可以设置最多 8 个值,通过改变 8 个值可以得到许多意想不到的图像box-shadow,其完整的声明为box-shadow: h-shadow v-shadow blur spread color inset,各个值表示的意义分别为:s 水平方向的偏移,垂直方向的便宜,模糊的距离(羽化值),阴影的大小(不设置或为 0 时阴影与主体的大小一致),阴影的颜色和是否使用内阴影。实际应用时可以接收 3-6 个值,对应分别如下:3 个值:h-shadow v-shadow color
4 个值:h-shadow v-shadow blur color
5 个值:h-shadow v-shadow blur spread color
6 个值:h-shadow v-shadow blur spread color inset
border-shadow接受由多个以上各种值组成的以逗号分隔的值,通过这一特性,我们可以实现如多重边框的等效果。以下我们用该属性来实现一个单标签且不借助伪元素的添加图标和代表目标的的图标。<div class="plus"></div><div class="target"></div>
.plus {width: 30px;height: 30px;margin-left: 50px; /*由于box-shadow不占空间,常常需要添加margin来校正位置*/background: #000;box-shadow: -30px red, 30px red, -30px red, 30px red;}.target {width: 30px;height: 30px;background: red;border-radius: 50%;margin-left: 50px;box-shadow: 10px #fff, 20px red, 30px #fff, 40px red;}
<div class="gradient"></div>
.gradient {position: relative;width: 300px;height: 300px;border-radius: 50%;background-color: silver;background-image: linear-gradient(335deg, #b00 23px, transparent 23px),linear-gradient(155deg, #d00 23px, transparent 23px), linear-gradient(335deg,#b00 23px,transparent 23px), linear-gradient(155deg, #d00 23px, transparent 23px);background-size: 58px 58px;background-position: 0px 2px, 4px 35px, 29px 31px, 34px 6px;}
杯子
.cup {display: inline-block;width: 0.9em;height: 0.4em;border: 0.25em solid;border-bottom: 1.1em solid;border-radius: 0.25em 0.25em;}cup:before {position: absolute;right: -0.6em;top: ;width: 0.3em;height: 0.8em;border: 0.25em solid;border-left: none;border-radius: 0.25em 0.25em ;content: "";}
心形
.heart {display: inline-block;margin-top: 1.5em;width: 50px;height: 50px;background: green;}.heart:before,.heart:after {position: absolute;width: 1em;height: 1.6em;background: #000;border-radius: 50% 50% ;content: "";bottom: ;}.heart:before {-webkit-transform: rotate(45deg);-webkit-transform-origin: 100% 100%;right: ;background: red;opacity: 0.5;z-index: 5;}.:after {-webkit-transform: rotate(-45deg);-webkit-transform-origin: 100%;left: ;opacity: 0.8;}
相机
.camera {display: inline-block;border-style: solid;border-width: 0.65em 0.9em;border-radius: 0.1em;}.camera:before {position: absolute;top: -0.3em;left: -0.3em;width: 0.4em;height: 0.4em;border-radius: 50%;border: 0.1em solid #fff;box-shadow: 0.08em, 0.16em #fff;content: "";}.camera:after {position: absolute;top: -0.5em;left: 0.5em;width: 0.2em;border-top: 0.125em solid #fff;content: "";}
月亮
.moon {display: inline-block;height: 1.5em;width: 1.5em;box-shadow: inset -0.4em ;border-radius: 2em;transform: rotate(20deg);}
.float-left {float: left;}.float-right {float: right;}.float-li li,/*定义到li父元素或祖先元素上*/ li.float-li {float: left;}.float-img img,/*定义到img父元素或祖先元素上*/ img.float-li {float: left;}.float-span span,/*定义到span父元素或祖先元素上*/ span.float-span {float: right;}
.bg-img {background-image: url("../img/bg.png");background-position: center top;background-repeat: no-repeat;}.bg01-img {background-image: url("../img/bg01.png");background-position: center top;background-repeat: no-repeat;}.bg02-img {background-image: url("../img/bg02.png");background-position: center top;background-repeat: no-repeat;}.bg03-img {background-image: url("../img/bg03.png");background-position: center top;background-repeat: no-repeat;}.bg04-img {background-image: url("../img/bg04.png");background-position: center top;background-repeat: no-repeat;}
.inherit-width {width: inherit;}.inherit-min-width {min-width: inherit;}.inherit-height {height: inherit;}.inherit-min-height {min-height: inherit;}.inherit-color {color: inherit;}
.text-indent {text-indent: 2rem;}/*16px*/.text-indent-xs {text-indent: 1.5rem;}/*12px*/.text-indent-sm {text-indent: 1.7rem;}/*14px*/.text-indent-md {text-indent: 2rem;}/*18px*/.text-indent-lg {text-indent: 2.4rem;}/*20px*/
.line-height-xs {line-height: 1.3rem;}.line-height-sm {line-height: 1.5rem;}.line-height-md {line-height: 1.7rem;}.line-height-lg {line-height: 2rem;}.line-height-25x {line-height: 25px;}.line-height-30x {line-height: 30px;}
.ul-indent-xs {margin-left: 0.5rem;}.ul-indent-sm {margin-left: 1rem;}.ul-indent-md {margin-left: 1.5rem;}.ul-indent-lg {margin-left: 2rem;}.ol-list,.ul-list {list-style: disc;}
.truncate {max-width: 100%;overflow: hidden;text-overflow: ellipsis;white-space: nowrap;}.hide {display: none;}
.img-max,.video-max {width: 100%;height: auto;}/*display显示方式*/.inline {display: inline;}.inline-block {display: inline-block;}.block {display: block;}
.border-xs-black {border: 1px solid #000;}.border-sm-black {border: 2px solid #000;}.border-md-black {border: 3px solid #000;}.border-lg-black {border: 5px solid #000;}.border-xs-gray {border: 1px solid #9c9c9c;}.border-sm-gray {border: 2px solid #9c9c9c;}.border-md-gray {border: 3px solid #9c9c9c;}.border-lg-gray {border: 5px solid #9c9c9c;}
.bg-white {background: #fff ;}.bg-black {background: #1b1c1d ;}.bg-gray {background: #767676 ;}.bg-light-gray {background: #f8f7f7 ;}.bg-yellow {background: #fbbd08 ;}.bg-orange {background: #f2711c ;}.bg-red {background: #db2828 ;}.bg-olive {background: #b5cc18 ;}.bg-green {background: #21ba45 ;}.bg-teal {background: #00b5ad ;}.bg-darkGreen {background: #19a97b ;}.bg-threeGreen {background: #097c25 ;}.bg-blue {background: #2185d0 ;}.bg-violet {background: #6435c9 ;}.bg-purple {background: #a333c8 ;}.bg-brown {background: #a5673f ;}
hr,.hr-xs-Silver,.hr-sm-black,.hr-sm-Silver,.hr-xs-gray,.hr-sm-gray {margin: 20px ;}hr {border: none;border-top: 1px solid #000;}.hr-xs-Silver {border: none;border-top: 1px solid #c0c0c0;}.hr-sm-black {border: none;border-top: 2px solid #000;}.hr-sm-Silver {border: none;border-top: 2px solid #c0c0c0;}.hr-xs-gray {border: none;border-top: 1px solid #767676;}.hr-sm-gray {border: none;border-top: 2px solid #767676;}
.hover-red a:hover,/*为a标签祖先元素添加类名 默认无智能提醒*/ a.hover-red:hover {color: red;} /*单独为a标签添加类名*/.hover-yellow a:hover,/*为a标签祖先元素添加类名 默认无智能提醒*/ a.hover-yellow:hover {color: #ffd700;} /*单独为a标签添加类名*/.hover-green a:hover,/*为a标签祖先元素添加类名 默认无智能提醒*/ a.hover-green:hover {color: #70aa39;} /*单独为a标签添加类名*/.hover-blue a:hover,/*为a标签祖先元素添加类名 默认无智能提醒*/ a.hover-blue:hover {color: blue;} /*单独为a标签添加类名*/.hover-gray a:hover,/*为a标签祖先元素添加类名 默认无智能提醒*/ a.hover-gray:hover {color: #9c9c9c;} /*单独为a标签添加类名*/.underline a:hover,a.underline:hover {text-decoration: underline;}
.shadow-text-xs {text-shadow: 4px 3px #1d9d74, 9px 8px rgba(0, 0, 0, 0.15);} /*智能兼容ie10以上 暂不考虑*/.shadow-xs {-ms-filter: "progid:DXImageTransform.Microsoft.Shadow(Strength=1, Direction=100, Color='#cccccc')"; /* For IE 8 */filter: progid:DXImageTransform.Microsoft.Shadow(Strength=1, Direction=100, Color='#cccccc'); /* For IE 5.5 - 7 */-moz-box-shadow: 1px 1px 2px #cccccc; /* for firefox */-webkit-box-shadow: 1px 1px 2px #cccccc; /* for safari or chrome */box-shadow: 1px 1px 2px #cccccc; /* for opera or ie9 */}.shadow-sm {-ms-filter: "progid:DXImageTransform.Microsoft.Shadow(Strength=2, Direction=120, Color='#cccccc')"; /* For IE 8 */filter: progid:DXImageTransform.Microsoft.Shadow(Strength=2, Direction=120, Color='#cccccc'); /* For IE 5.5 - 7 */-moz-box-shadow: 2px 2px 3px #cccccc; /* for firefox */-webkit-box-shadow: 2px 2px 3px #cccccc; /* for safari or chrome */box-shadow: 2px 2px 3px #cccccc; /* for opera or ie9 */}.shadow-md {-ms-filter: "progid:DXImageTransform.Microsoft.Shadow(Strength=3, Direction=135, Color='#cccccc')"; /* For IE 8 */filter: progid:DXImageTransform.Microsoft.Shadow(Strength=3, Direction=135, Color='#cccccc'); /* For IE 5.5 - 7 */-moz-box-shadow: 3px 3px 5px #cccccc; /* for firefox */-webkit-box-shadow: 3px 3px 5px #cccccc; /* for safari or chrome */box-shadow: 3px 3px 5px #cccccc; /* for opera or ie9 */}.shadow-lg {-ms-filter: "progid:DXImageTransform.Microsoft.Shadow(Strength=5, Direction=150, Color='#cccccc')"; /* For IE 8 */filter: progid:DXImageTransform.Microsoft.Shadow(Strength=5, Direction=150, Color='#cccccc'); /* For IE 5.5 - 7 */-moz-box-shadow: 5px 5px 8px #cccccc; /* for firefox */-webkit-box-shadow: 5px 5px 8px #cccccc; /* for safari or chrome */box-shadow: 5px 5px 8px #cccccc; /* for opera or ie9 */}
.border-radius-xs {-webkit-border-radius: 3px;-moz-border-radius: 3px;border-radius: 3px;}.border-radius-sm {-webkit-border-radius: 5px;-moz-border-radius: 5px;border-radius: 5px;}.border-radius-md {-webkit-border-radius: 7px;-moz-border-radius: 5px;border-radius: 5px;}.border-radius-lg {-webkit-border-radius: 9px;-moz-border-radius: 9px;border-radius: 9px;}

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/69975286/viewspace-3123171/ ,如需转载,请注明出处,否则将追究法律责任。