CSS currentColor研究,csscurrentcolor

來源:互聯網
上載者:User

CSS currentColor研究,csscurrentcolor

剛剛寫了篇《CSS變數試玩兒》,我們瞭解到可以使用原生的CSS來定義使用變數,簡化CSS書寫、最佳化代碼的組織與維護,但可怕的相容性問題,又讓我們望而卻步、一笑了之。
但是有這麼一個CSS變數currentColor,相容良好、功能強大,接下來我們來一探究竟。

簡介

CSS裡已經有一個長期存在的相容性良好的變數currentColor,表示對當前元素顏色的引用,可以應用在當前元素的其他屬性和後代元素上。

h1 {     color: hsl(0,0%,44%);     padding: 1rem;     /* 這裡調用預設顏色 */    border-bottom: 4px solid; }/* 使用currentColor,用在其他屬性上 */h1 {     color: hsl(0,0%,44%);     padding: 1rem;     /* 這裡調用預設顏色 */    border-bottom: currentColor 4px solid; }/* 使用currentColor,用在後代元素上 */a.blog{   text-decoration: none;  font-weight:bold;}/*設定不同狀態顏色*/a.blog           { color: #900; }a.blog:hover,a.blog:focus     { color: #990; }a.blog:active    { color: #999; }/*設定箭頭*/a.blog:after{  width: 0;    height: 0;    border: 0.4em solid transparent;    border-right: none;    content: '';    display: inline-block;  position:relative;  top:1px;  left:2px;}/*設定箭頭繼承父物件顏色*/a.blog::after,a.blog:hover::after,a.blog:focus::after,a.blog:active::after{    border-left-color: currentColor;}

我們可以發現,使用currentColor能夠大大簡化代碼書寫,最佳化代碼的組織與維護。

執行個體

為了示範currentColor的應用,我們造了一個案例,參見codepen,大家可以-線上編輯-,-下載收藏-。我們在案例裡嘗試了currentColor和漸層的結合,和動畫的結合,和偽對象元素的結合。

html檔案放上來,順便弄點廣告哈。
html檔案

<h2 class="icon">Let's go to whqet's blog</h2><p>前端開發whqet,<a class="blog" href="http://blog.csdn.net/whqet/">王海慶的技術部落格</a>,關注前端開發,分享相關資源,希望可以對您有所協助。csdn專家部落格http://blog.csdn.net/whqet和個人獨立部落格http://whqet.github.io同步更新,希望可以得到您的支援與肯定,您的支援是我最大的動力!王海慶,浙江郵電職業技術學院軟體技術青椒一枚,其貌不揚、其名不翔,關心技術、熱愛生活,我愛<a class="link" href="#">怒放的生命</a>。</p>

然後在CSS中,我們使用-prefix free不再用考慮複雜的瀏覽器廠商首碼。
這裡使用本博文章《蘋果風格的底線》所述效果,利用漸層劃線,然後利用text-shadow隔離線條,在漸層裡面使用了currentColor.

/*使用googlefont*/@import url(//fonts.googleapis.com/css?family=Cedarville+Cursive);/*使用漸層劃線,利用text-shadow隔離線條*/h2.icon{  margin:10px 0;  display: inline-block;  font-size:3em;  width:auto;  font-family:Cedarville Cursive;  text-shadow: 2px 0 0 #fff, -2px 0 0 #fff;  color: #000;  background-image: linear-gradient( to right, currentColor 0%, #fff 120% );  background-repeat: repeat-x;  background-position: 0 75%;  background-size: 100% 3px;}

然後,我們探索將currentColor應用到:after元素中去,實現連結裝飾元素的顏色與連結元素的綁定。

p{  text-indent: 2em;  line-height: 1.5em;}a.blog,a.link{   text-decoration: none;  font-weight:bold;  position: relative;  margin-right:4px;}/*應用到後代偽類元素*/a.blog           { color: #900; }a.blog:hover,a.blog:focus     { color: #990; }a.blog:active    { color: #999; }a.blog::after{  width: 0;    height: 0;    border: 0.4em solid transparent;    border-right: none;    content: '';  position:absolute;  right:-6px;  top:2px;}a.blog::after,a.blog:hover::after,a.blog:focus::after,a.blog:active::after{    border-left-color: currentColor;}

應用到動畫元素上的嘗試。

/* 結合動畫應用 */a.link{   color: #900;   animation:go 2s infinite;}a.link::before,a.link::after{  content: '';  width:100%;    height: 2px;    background-color: currentColor;    position:absolute;  left:0;}a.link::before{  top:-4px;}a.link::after{  bottom:-4px;}@keyframes go{  0% {color:#900}  33%{color:#090}  66%{color:#009}}
深入

本文的寫作過程大量參考了以下文章,大家可以仔細閱讀下面文章獲得更深的體會。

  • CSS-Tricks article
  • 張鑫旭的妙文
  • W3C Ref
  • Keeping CSS short with currentColor
  • form-controls-currentcolor-pseudo-elements
  • 劉哇勇的文章
致謝

前端開發whqet,關注前端開發,分享相關資源。csdn專家部落格,王海慶希望能對您有所協助。
本文原文連結,http://blog.csdn.net/whqet/article/details/43761091
歡迎大家訪問獨立部落格http://whqet.github.io

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.