實用的 CSS 的高級技巧,總有一個你須要的!

 

 1、黑白圖像css

當你須要讓一張彩色的圖片顯示爲黑白照片的時候,你能夠用下面的一段代碼。
img.desaturate{
        filter: grayscale(100%);
        -webkit-filter: grayscale(100%);
        -moz-filter: grayscale(100%);
        -ms-filter: grayscale(100%);
        -o-filter: grayscale(100%);
}
2、使用 :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
}
3、頁面頂部陰影
給網頁加上漂亮的頂部陰影效果
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;
}
4、給 body 添加行高
不須要給別給 p,h之類的添加行高,直接:
body{
        line-height: 1;
}
5、全部一切都垂直居中
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
6、逗號分隔列表
讓HTML列表項看上去像被一個真正的,分隔的列表
ul > li:not(:last-child)::after{
        content: ",";
}
7、使用負的 nth-child 選擇項目
在 css 中使用負的 nth-child 選擇項目1到項目n
li{
        display: none;
}
li:nth-child(-n+3){
        display: block;
}
8、對圖標使用  SVG
.logo{
        background: url("logo.svg");
}
9、優化顯示文本
有時候,字體並不能在全部設備上都達到最佳的顯示,因此可讓設備瀏覽器來幫助你
html{
        -moz-osx-font-smoothing: grayscale;
        -webkit-font-smoothing: antialiased;
        text-rendering: optimizelegibility;
}html

WEB前端互動交流羣 434623999,進羣免費領取視頻學習哦前端

相關文章
相關標籤/搜索