CSS 的高级技巧

来源:互联网 发布:360软件管理下载 编辑:程序博客网 时间:2024/05/21 18:31
 一、黑白图像
当你需要让一张彩色的图片显示为黑白照片的时候,你可以用下面的一段代码。
img.desaturate{
        filter: grayscale(100%);
        -webkit-filter: grayscale(100%);
        -moz-filter: grayscale(100%);
        -ms-filter: grayscale(100%);
        -o-filter: grayscale(100%);
}
二、使用 :not() 在菜单上应用/取消应用边框
先给每一个菜单项添加边框
.nav li{
        border-right: 1px solid #666;
}
然后再除去最后一个元素
.nav li:last-child{
        border-right: none;
}
也可以直接使用 :not() 伪类来应用元素
.nav li:not(:last-child){
        border-right: 1px solid #666
}
如果你的元素有兄弟元素的话,也可以使用通用的熊年底选择符( ~ )
.nav li:first-child ~ li{
        border-left: 1px solid #666
}
三、页面顶部阴影
给网页加上漂亮的顶部阴影效果
body:before{
        content: '';
        position: fixed;
        top: -10px;
        left: 0;
        width: 100%;
        height: 10px;
        -webkit-box-shadow: 0px 0px 10px rgba(0,0,0,.8);
        -moz-box-shadow: 0px 0px 10px rgba(0,0,0,.8);
        box-shadow: 0px 0px 10px rgba(0,0,0,.8);
        z-index: 100;
}
四、给 body 添加行高
不需要给别给 p,h之类的添加行高,直接:
body{
        line-height: 1;
}
五、所有一切都垂直居中
html,body{
        height: 100%;
        margin: 0;
}
body{
        -webkit-align-items: center;
        -ms-flex-align: center;
        align-items: center;
        display: -webkit-flex;
        display: flex;
}
IE11中需要注意 flexbox
六、逗号分隔列表
让HTML列表项看上去像被一个真正的,分隔的列表
ul > li:not(:last-child)::after{
        content: ",";
}
七、使用负的 nth-child 选择项目
在 css 中使用负的 nth-child 选择项目1到项目n
li{
        display: none;
}
li:nth-child(-n+3){
        display: block;
}
八、对图标使用  SVG
.logo{
        background: url("logo.svg");
}
九、优化显示文本
有时候,字体并不能在所有设备上都达到最佳的显示,所以可以让设备浏览器来帮助你
html{
        -moz-osx-font-smoothing: grayscale;
        -webkit-font-smoothing: antialiased;
        text-rendering: optimizelegibility;
}
十、对纯 css 滑块使用 max-height
使用 max-height 和溢出隐藏来实现只有 css 的滑块
.slider ul{
        max-height: 0;
        overflow: hidden;
}
.slider:hover ul{
        max-height: 1000px;
        transition: .3s ease;
}
十一、继承 box-sizing
让 box-sizing 继承 html
html{
        box-sizing: border-box;
}
*,*:before, *:after{
        box-sizing: inherit;
}
十二、表格单元格等宽
.table{
        table-layout: fixed;
}
十三、 用 Flexbox 摆脱外边距的各种 hack
当你需要用到列分隔符时,通过flexbox的 space-between 属性,你就可以摆脱 nth-   first-  last-chlid 的 hack 了
.list{
        display: flex;
        justify-content: space-between;
}
.list .person{
        flex-basis: 23%;
}
十四、使用属性选择器用于空链接
当 a 元素没有文本值,但是 href 属性有链接的时候显示链接
a[href^="http"]:empty::before{
        content: attr(href);
}
十五、检测鼠标双击
HTML: <div class="test">
                <span>
                        <input type="text" value="" readonly="true"/>
                        <a href="http://renpingjun.com">Double click me</a>
                </span>
        </div>
CSS:.test span{
        position: relative;
}
.test span a{
        position: relative;
        z-index: 2;
}
.test span a:hover,.test span a:active{
        z-index: 4;
}
.test span input{
        background-color: transparent;
        border: 0;
        cursor: pointer;
        position: absolute;
        top: -1px;
        left: 0;
        width: 101%; 
        height: 301%; 
        z-index: 3;
}
.test span input:focus{
        background-color: transparent;
        border: 0;
        z-index: 1;
}
十六、 CSS 写出三角形
div.arrow-up{
        width: 0px;
        height: 0px;
        border-left: 5px solid transparent;
        border-right: 5px solid transparent;
        border-bottom: 5px solid #ccc;
        font-size: 0px;
        line-height: 0px;
}
div.arrow-down{
        width: 0px;
        height: 0px;
        border-bottom: 5px solid transparent;
        border-top: 5px solid transparent;
        border-right: 5px solid #ccc;
        font-size: 0px;
        line-height: 0px;
}
div.arrow-left{
        width: 0px;
        height: 0px;
        border-bottom: 5px solid transparent;
        border-top: 5px solid transparent;
        border-left: 5px solid #ccc;
        font-size: 0px;
        line-height: 0px;
}
div.arrow-right{
        width: 0px;
        height: 0px;
        border-bottom: 5px solid transparent;
        border-top: 5px solid transparent;
        border-left: 5px solid #ccc;
        font-size: 0px;
        line-height: 0px;
}
十七、 CSS calc() 的使用
calc() 用法类似于函数,能够给元素设置动态的值

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

.complexBlock{
        width: calc(100% - 50% / 3);
        padding: 5px calc(3% - 2px);
        margin-left: calc(10% + 10px);
}
十八、文本渐变
h2[data-text]{
        position: relative;
}
h2[data-text]::after{
        content: attr(data-text);
        z-index: 10;
        color: #e3e3e3;
        position: absolute;
        top: 0;
        left: 0;
        -webkit-mask-image: -webkit-gradient(linear, left top,left bottom,from(rgba(0,0,0,0)),color-stop(50%,rgba(0,0,0,1)),to(rgba(0,0,0,0)));
}
十九、禁用鼠标事件
.disabled{
        pointer-events: none;
}
二十、模糊文本
.blur{
        color: transparent;
        text-shadow: 0 0 5px rgba(0,0,0,.5);
}


可以进群交流的哦~WEB前端学习交流群21 598399936
原创粉丝点击