首頁 > web前端 > css教學 > 主體

20 個 CSS 進階技巧總結

小云云
發布: 2017-12-05 13:29:16
原創
1818 人瀏覽過

在開發過程中能學會使用CSS 高階技巧對開發也是很有利的,本文我們將和大家分享20 個 CSS 進階技巧。

1. 黑白影像

這段程式碼會讓你的彩色照片顯示為黑白照片,是不是很酷?


img.desaturate {
    filter: grayscale(100%);
    -webkit-filter: grayscale(100%);
    -moz-filter: grayscale(100%);
    -ms-filter: grayscale(100%);
    -o-filter: grayscale(100%);
}
登入後複製


#2. 使用:not() 在選單上套用/取消套用邊框

先給每個選單項目新增邊框


/* add border */
.nav li {
  border-right: 1px solid #666;
}
……然后再除去最后一个元素……
// remove border /
.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. 頁面頂部陰影

下面這個簡單的CSS3 程式碼片段可以為網頁加上漂亮的頂部陰影效果:


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 新增一行高

你不需要分別新增line-height 到每個p,h標記等。只要加入 body 即可:


body {
  line-height: 1;
}
登入後複製


#這樣文字元素就可以很容易地從 body 繼承。

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: ",";
}
登入後複製


對最後一個清單項目使用:not() 偽類別。

7. 使用負面的 nth-child 選擇項目

#在CSS中使用負的 nth-child 選擇項目1到項目n。


li {
  display: none;
}
/* select items 1 through 3 and display them */
li:nth-child(-n+3) {
  display: block;
}
登入後複製


#8. 對圖示使用SVG

##我們沒有理由不對圖示使用SVG:


.logo {
  background: url("logo.svg");
}
登入後複製


#SVG對所有的解析度類型都具有良好的擴展性,並支援所有瀏覽器都回歸到IE9。這樣可以避開.png、.jpg或.gif檔了。

9. 最佳化顯示文字

有時,字體並不能在所有裝置上達到最佳的顯示,所以可以讓裝置瀏覽器來幫助你:


html {
  -moz-osx-font-smoothing: grayscale;
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
}
登入後複製


#註:請負責任地使用optimizeLegibility。此外,IE /Edge沒有 text-rendering 支援。

10. 對純CSS 滑桿使用max-height

#使用max-height 和溢出隱藏來實現只有CSS的滑桿:


.slider ul {
  max-height: 0;
  overlow: hidden;
}
.slider:hover ul {
  max-height: 1000px;
  transition: .3s ease;
}
登入後複製


#11. 繼承box-sizing

讓box-sizing 繼承html:


html {
  box-sizing: border-box;
}
*, *:before, *:after {
  box-sizing: inherit;
}
登入後複製


#這樣在外掛程式或槓桿其他行為的其他元件中就能更容易改變box- sizing 了。

12. 表格儲存格等寬

#表格運作起來很麻煩,所以務必盡量使用table-layout: fixed 來保持儲存格的等寬:


.calendar {
  table-layout: fixed;
}
登入後複製


#13. 用Flexbox 擺脫外邊距的各種hack

#當需要用到列分隔符號時,透過flexbox的space-between 屬性,你就可以擺脫nth-,first-,和last-child 的hack了:


#

.list {
  display: flex;
  justify-content: space-between;
}
.list .person {
  flex-basis: 23%;
}
登入後複製


現在,清單分隔符號就會在均勻間隔的位置出現。

14. 使用屬性選擇器用於空連結

#當a元素沒有文字值,但href 屬性有連結的時候顯示連結:


a[href^="http"]:empty::before {
  content: attr(href);
}
登入後複製


相當方便。

15. 偵測滑鼠雙擊

#HTML:


<p class="test3">
  <span><input type="text" value=" " readonly="true" />
  <a href="http://renpingjun.com">Double click me</a></span>
</p>
登入後複製


#CSS:


.test3 span {
  position: relative;
}
.test3 span a {
  position: relative;
  z-index: 2;
}
.test3 span a:hover, .test3 span a:active {
  z-index: 4;
}
.test3 span input {
  background: transparent;
  border: 0;
  cursor: pointer;
  position: absolute;
  top: -1px;
  left: 0;
  width: 101%;  /* Hacky */
  height: 301%; /* Hacky */
  z-index: 3;
}
.test3 span input:focus {
  background: transparent;
  border: 0;
  z-index: 1;
}
登入後複製


#16. CSS 寫出三角形


/* create an arrow that points up */
p.arrow-up {
  width:0px;
  height:0px;
  border-left:5px solid transparent;  /* left arrow slant */
  border-right:5px solid transparent; /* right arrow slant */
  border-bottom:5px solid #2f2f2f; /* bottom, add background color here */
  font-size:0px;
  line-height:0px;
}
/* create an arrow that points down */
p.arrow-down {
  width:0px;
  height:0px;
  border-left:5px solid transparent;
  border-right:5px solid transparent;
  border-top:5px solid #2f2f2f;
  font-size:0px;
  line-height:0px;
}
/* create an arrow that points left */
p.arrow-left {
  width:0px;
  height:0px;
  border-bottom:5px solid transparent;  /* left arrow slant */
  border-top:5px solid transparent; /* right arrow slant */
  border-right:5px solid #2f2f2f; /* bottom, add background color here */
  font-size:0px;
  line-height:0px;
}
/* create an arrow that points right */
p.arrow-right {
  width:0px;
  height:0px;
  border-bottom:5px solid transparent;  /* left arrow slant */
  border-top:5px solid transparent; /* right arrow slant */
  border-left:5px solid #2f2f2f; /* bottom, add background color here */
  font-size:0px;
  line-height:0px;
}
登入後複製


#17. CSS3 calc() 的使用

calc() 用法類似函數,能夠為元素設定動態的值:


#

/* basic calc */
.simpleBlock {
  width: calc(100% - 100px);
}
/* calc in calc */
.complexBlock {
  width: calc(100% - 50% / 3);
  padding: 5px calc(3% - 2px);
  margin-left: calc(10% + 10px);
}
登入後複製


18 . 文字漸層

文字漸層效果很流行,使用CSS3 能夠很簡單就實作:


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)));}
登入後複製


########## ##########19. 停用滑鼠事件#########

CSS3 新增的 pointer-events 让你能够禁用元素的鼠标事件,例如,一个连接如果设置了下面的样式就无法点击了。


.disabled { pointer-events: none; }
登入後複製


20. 模糊文本

简单但很漂亮的文本模糊效果,简单又好看!


.blur {
   color: transparent;
   text-shadow: 0 0 5px rgba(0,0,0,0.5);
}
登入後複製

以上内容就是20 个 CSS 高级技巧汇总,希望能帮助到大家。

相关推荐:

最实用的制作网页小技巧总结

css的开发小技巧

CSS布局有哪些技巧

几个好用的css函数小技巧

最全的CSS开发常用技巧

以上是20 個 CSS 進階技巧總結的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!