、今天記錄下CSS漸變色(linear-gradient)使用。
二、代碼部分
<!DOCTYPE html> <head> <meta charset="utf-8"> </head> <style> .xa-linear-gradient{ background: linear-gradient(red,green); } </style> <body> <button class="xa-linear-gradient">按鈕漸變色</button> </body> </html>
三、效果圖
按鈕漸變色
家應該都知道,在進行網頁編程的時候有很多時候都會用到漸變色,但是CSS2處理漸變色比較困難,CSS3就比較方便了,所以今天蘇蘇老師就教大家用CSS3實現漸變色功能~
有自信的女性高管的復合形象,雙臂交叉
CSS3 漸變(Gradients)
CSS3 漸變(gradients)可以讓你在兩個或多個指定的顏色之間顯示平穩的過渡。
以前,你必須使用圖像來實現這些效果。但是,通過使用 CSS3 漸變(gradients),你可以減少下載的時間和寬帶的使用。此外,漸變效果的元素在放大時看起來效果更好,因為漸變(gradient)是由瀏覽器生成的。
CSS3 定義了兩種類型的漸變:
線性漸變(Linear Gradients)- 向下/向上/向左/向右/對角方向
徑向漸變(Radial Gradients)- 由它們的中心定義
CSS3 線性漸變
為了創建一個線性漸變,你必須至少定義兩種顏色結點。顏色結點即你想要呈現平穩過渡的顏色。同時,你也可以設置一個起點和一個方向(或一個角度)。
語法
background: linear-gradient(direction, color-stop1, color-stop2, ...);
線性漸變 - 從上到下
下面的實例演示了從頂部開始的線性漸變。起點是紅色,慢慢過渡到藍色:
從上到下的線性漸變代碼
#grad {
background: -webkit-linear-gradient(red, blue); /* Safari 5.1 - 6.0 */
background: -o-linear-gradient(red, blue); /* Opera 11.1 - 12.0 */
background: -moz-linear-gradient(red, blue); /* Firefox 3.6 - 15 */
background: linear-gradient(red, blue); /* 標準的語法 */
}
線性漸變 - 從左到右
下面的實例演示了從左邊開始的線性漸變。起點是紅色,慢慢過渡到藍色:
代碼如下
#grad {
background: -webkit-linear-gradient(left, red , blue); /* Safari 5.1 - 6.0 */
background: -o-linear-gradient(right, red, blue); /* Opera 11.1 - 12.0 */
background: -moz-linear-gradient(right, red, blue); /* Firefox 3.6 - 15 */
background: linear-gradient(to right, red , blue); /* 標準的語法 */
}
線性漸變 - 對角
你可以通過指定水平和垂直的起始位置來制作一個對角漸變。
下面的實例演示了從左上角開始(到右下角)的線性漸變。起點是紅色,慢慢過渡到藍色:
從左上角到右下角的線性漸變代碼
#grad {
background: -webkit-linear-gradient(left top, red , blue); /* Safari 5.1 - 6.0 */
background: -o-linear-gradient(bottom right, red, blue); /* Opera 11.1 - 12.0 */
background: -moz-linear-gradient(bottom right, red, blue); /* Firefox 3.6 - 15 */
background: linear-gradient(to bottom right, red , blue); /* 標準的語法 */
}
使用角度
如果你想要在漸變的方向上做更多的控制,你可以定義一個角度,而不用預定義方向(to bottom、to top、to right、to left、to bottom right,等等)。
語法
background: linear-gradient(angle, color-stop1, color-stop2);
角度是指水平線和漸變線之間的角度,逆時針方向計算。換句話說,0deg 將創建一個從下到上的漸變,90deg 將創建一個從左到右的漸變。
但是,請注意很多瀏覽器(Chrome,Safari,fiefox等)的使用了舊的標準,即 0deg 將創建一個從左到右的漸變,90deg 將創建一個從下到上的漸變。換算公式 90 - x = y 其中 x 為標準角度,y為非標準角度。
下面的實例演示了如何在線性漸變上使用角度:
帶有指定的角度的線性漸變代碼
#grad {
background: -webkit-linear-gradient(180deg, red, blue); /* Safari 5.1 - 6.0 */
background: -o-linear-gradient(180deg, red, blue); /* Opera 11.1 - 12.0 */
background: -moz-linear-gradient(180deg, red, blue); /* Firefox 3.6 - 15 */ background: linear-gradient(180deg, red, blue); /* 標準的語法 */
}
使用多個顏色結點
下面的實例演示了如何設置多個顏色結點:
多個顏色結點的從上到下的線性漸變代碼
#grad {
background: -webkit-linear-gradient(red, green, blue); /* Safari 5.1 - 6.0 */
background: -o-linear-gradient(red, green, blue); /* Opera 11.1 - 12.0 */
background: -moz-linear-gradient(red, green, blue); /* Firefox 3.6 - 15 */
background: linear-gradient(red, green, blue); /* 標準的語法 */
}
下面的實例演示了如何創建一個帶有彩虹顏色和文本的線性漸變:
#grad {
/* Safari 5.1 - 6.0 */
background: -webkit-linear-gradient(left,red,orange,yellow,green,blue,indigo,violet);
/* Opera 11.1 - 12.0 */
background: -o-linear-gradient(left,red,orange,yellow,green,blue,indigo,violet);
/* Firefox 3.6 - 15 */
background: -moz-linear-gradient(left,red,orange,yellow,green,blue,indigo,violet);
/* 標準的語法 */
background: linear-gradient(to right, red,orange,yellow,green,blue,indigo,violet);
}
使用透明度(transparent)
為了添加透明度,我們使用 rgba() 函數來定義顏色結點。rgba() 函數中的最后一個參數可以是從 0 到 1 的值,它定義了顏色的透明度:0 表示完全透明,1 表示完全不透明。
下面的實例演示了從左邊開始的線性漸變。起點是完全透明,慢慢過渡到完全不透明的紅色:
從左到右的線性漸變,帶有透明度代碼
#grad {
background: -webkit-linear-gradient(left,rgba(255,0,0,0),rgba(255,0,0,1)); /* Safari 5.1 - 6 */
background: -o-linear-gradient(right,rgba(255,0,0,0),rgba(255,0,0,1)); /* Opera 11.1 - 12*/
background: -moz-linear-gradient(right,rgba(255,0,0,0),rgba(255,0,0,1)); /* Firefox 3.6 - 15*/
background: linear-gradient(to right, rgba(255,0,0,0), rgba(255,0,0,1)); /* 標準的語法 */
}
重復的線性漸變
repeating-linear-gradient() 函數用于重復線性漸變:
代碼如下
#grad {
/* Safari 5.1 - 6.0 */
background: -webkit-repeating-linear-gradient(red, yellow 10%, green 20%);
/* Opera 11.1 - 12.0 */
background: -o-repeating-linear-gradient(red, yellow 10%, green 20%);
/* Firefox 3.6 - 15 */
background: -moz-repeating-linear-gradient(red, yellow 10%, green 20%);
/* 標準的語法 */
background: repeating-linear-gradient(red, yellow 10%, green 20%);
}
今天就教到這里了,大家都會了嗎?祝大家國慶快樂~
有什么問題和建議可以私信小編:"666"
者:IT智云編程
鏈接:https://www.jianshu.com/p/4fa116fc4653
在web前端開發過程中,UI設計師經常會設計一些帶漸變文字的設計圖,在以前我們只能用png的圖片來代替文字,今天可以實現使用純CSS實現漸變文字了。下面就介紹3中實現方式供大家參考!
基礎樣式:
.gradient-text{text-align: left;text-indent:30px;line-height: 50px;font-size:40px;font-weight:bolder; position: relative; }
第一種方法,使用 background-cli、 text-fill-color:
.gradient-text-one{ background-image:-webkit-linear-gradient(bottom,red,#fd8403,yellow); -webkit-background-clip:text; -webkit-text-fill-color:transparent; }
說明 :
background: -webkit-linear-gradient(...) 為文本元素提供漸變背景。
webkit-text-fill-color: transparent 使用透明顏色填充文本。
webkit-background-clip: text 用文本剪輯背景,用漸變背景作為顏色填充文本。
第二種方法,使用 mask-image:
.gradient-text-two{ color:red; } .gradient-text-two[data-content]::after{ content:attr(data-content); display: block; position:absolute; color:yellow; left:0; top:0; z-index:2; -webkit-mask-image:-webkit-gradient(linear, 0 0, 0 bottom, from(yellow), to(rgba(0, 0, 255, 0))); }
說明:
mask-image 和 background-image 一樣,不僅可以取值是 圖片路徑,也可以是漸變色。
第三種方法,使用 linearGradient、fill:
.gradient-text-three{ fill:url(#SVGID_1_); font-size:40px; font-weight:bolder; } <svg viewBoxs="0 0 500 300" class="svgBox"> <defs> <linearGradient id="SVGID_1_" gradientUnits="userSpaceOnUse" x1="0" y1="10" x2="0" y2="50"> <stop offset="0" style="stop-color:yellow"/> <stop offset="0.5" style="stop-color:#fd8403"/> <stop offset="1" style="stop-color:red"/> </linearGradient> </defs> <text text-anchor="middle" class="gradient-text-three" x="110px" y="30%">花信年華</text> </svg>
說明:
在SVG中,有兩種主要的漸變類型:
線性漸變(linearGradient)
放射性漸變(radialGradient)
SVG中的漸變不僅可以用于填充圖形元素,還可以填充文本元素
dom示例:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"> <title>CSS3漸變字體</title> <link rel="stylesheet" > <script src="https://cdn.bootcss.com/jquery/2.1.1/jquery.min.js"></script> <script src="https://cdn.bootcss.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> <style type="text/css"> *{margin:0;padding:0;} body,html{width:100%;height:100%;} .wrapper{width:80%;margin:0 auto;margin-top:30px;} .gradient-text{text-align: left;text-indent:30px;line-height: 50px;font-size:40px;font-weight:bolder; position: relative; } .gradient-text-one{ background-image:-webkit-linear-gradient(bottom,red,#fd8403,yellow); -webkit-background-clip:text; -webkit-text-fill-color:transparent; } .gradient-text-two{ color:red; } .gradient-text-two[data-content]::after{ content:attr(data-content); display: block; position:absolute; color:yellow; left:0; top:0; z-index:2; -webkit-mask-image:-webkit-gradient(linear, 0 0, 0 bottom, from(yellow), to(rgba(0, 0, 255, 0))); } .gradient-text-three{ fill:url(#SVGID_1_); font-size:40px; font-weight:bolder; } </style> </head> <body> <section class="wrapper"> <div class="panel panel-info"> <div class="panel-heading"> <h3 class="panel-title">方法1. background-clip + text-fill-color</h3> </div> <div class="panel-body"> <h3 class="gradient-text gradient-text-one">花樣年華</h3> </div> </div> <div class="panel panel-warning"> <div class="panel-heading"> <h3 class="panel-title">方法2. mask-image</h3> </div> <div class="panel-body"> <h3 class="gradient-text gradient-text-two" data-content="豆蔻年華">豆蔻年華</h3> </div> </div> <div class="panel panel-danger"> <div class="panel-heading"> <h3 class="panel-title">方法3. svg linearGradient</h3> </div> <div class="panel-body"> <svg viewBoxs="0 0 500 300" class="svgBox"> <defs> <linearGradient id="SVGID_1_" gradientUnits="userSpaceOnUse" x1="0" y1="10" x2="0" y2="50"> <stop offset="0" style="stop-color:yellow"/> <stop offset="0.5" style="stop-color:#fd8403"/> <stop offset="1" style="stop-color:red"/> </linearGradient> </defs> <text text-anchor="middle" class="gradient-text-three" x="110px" y="30%">花信年華</text> </svg> </div> </div> </section> </body> </html>
效果:
這里推薦一下我的前端技術分享群:731771211,里面都是學習前端的,如果你想制作酷炫的網頁,想學習編程。自己整理了一份2018最全面前端學習資料,從最基礎的HTML+CSS+JS【炫酷特效,游戲,插件封裝,設計模式】到移動端HTML5的項目實戰的學習資料都有整理,送給每一位前端小伙伴,有想學習web前端的,或是轉行,或是大學生,還有工作中想提升自己能力的,正在學習的小伙伴歡迎加入學習。
*請認真填寫需求信息,我們會在24小時內與您取得聯系。