Warning: error_log(/data/www/wwwroot/hmttv.cn/caches/error_log.php): failed to open stream: Permission denied in /data/www/wwwroot/hmttv.cn/phpcms/libs/functions/global.func.php on line 537 Warning: error_log(/data/www/wwwroot/hmttv.cn/caches/error_log.php): failed to open stream: Permission denied in /data/www/wwwroot/hmttv.cn/phpcms/libs/functions/global.func.php on line 537
、今天記錄下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>
三、效果圖
按鈕漸變色
家應(yīng)該都知道,在進(jìn)行網(wǎng)頁編程的時候有很多時候都會用到漸變色,但是CSS2處理漸變色比較困難,CSS3就比較方便了,所以今天蘇蘇老師就教大家用CSS3實(shí)現(xiàn)漸變色功能~
有自信的女性高管的復(fù)合形象,雙臂交叉
CSS3 漸變(Gradients)
CSS3 漸變(gradients)可以讓你在兩個或多個指定的顏色之間顯示平穩(wěn)的過渡。
以前,你必須使用圖像來實(shí)現(xiàn)這些效果。但是,通過使用 CSS3 漸變(gradients),你可以減少下載的時間和寬帶的使用。此外,漸變效果的元素在放大時看起來效果更好,因?yàn)闈u變(gradient)是由瀏覽器生成的。
CSS3 定義了兩種類型的漸變:
線性漸變(Linear Gradients)- 向下/向上/向左/向右/對角方向
徑向漸變(Radial Gradients)- 由它們的中心定義
CSS3 線性漸變
為了創(chuàng)建一個線性漸變,你必須至少定義兩種顏色結(jié)點(diǎn)。顏色結(jié)點(diǎn)即你想要呈現(xiàn)平穩(wěn)過渡的顏色。同時,你也可以設(shè)置一個起點(diǎn)和一個方向(或一個角度)。
語法
background: linear-gradient(direction, color-stop1, color-stop2, ...);
線性漸變 - 從上到下
下面的實(shí)例演示了從頂部開始的線性漸變。起點(diǎn)是紅色,慢慢過渡到藍(lán)色:
從上到下的線性漸變代碼
#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); /* 標(biāo)準(zhǔn)的語法 */
}
線性漸變 - 從左到右
下面的實(shí)例演示了從左邊開始的線性漸變。起點(diǎn)是紅色,慢慢過渡到藍(lán)色:
代碼如下
#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); /* 標(biāo)準(zhǔn)的語法 */
}
線性漸變 - 對角
你可以通過指定水平和垂直的起始位置來制作一個對角漸變。
下面的實(shí)例演示了從左上角開始(到右下角)的線性漸變。起點(diǎn)是紅色,慢慢過渡到藍(lán)色:
從左上角到右下角的線性漸變代碼
#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); /* 標(biāo)準(zhǔn)的語法 */
}
使用角度
如果你想要在漸變的方向上做更多的控制,你可以定義一個角度,而不用預(yù)定義方向(to bottom、to top、to right、to left、to bottom right,等等)。
語法
background: linear-gradient(angle, color-stop1, color-stop2);
角度是指水平線和漸變線之間的角度,逆時針方向計(jì)算。換句話說,0deg 將創(chuàng)建一個從下到上的漸變,90deg 將創(chuàng)建一個從左到右的漸變。
但是,請注意很多瀏覽器(Chrome,Safari,fiefox等)的使用了舊的標(biāo)準(zhǔn),即 0deg 將創(chuàng)建一個從左到右的漸變,90deg 將創(chuàng)建一個從下到上的漸變。換算公式 90 - x = y 其中 x 為標(biāo)準(zhǔn)角度,y為非標(biāo)準(zhǔn)角度。
下面的實(shí)例演示了如何在線性漸變上使用角度:
帶有指定的角度的線性漸變代碼
#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); /* 標(biāo)準(zhǔn)的語法 */
}
使用多個顏色結(jié)點(diǎn)
下面的實(shí)例演示了如何設(shè)置多個顏色結(jié)點(diǎn):
多個顏色結(jié)點(diǎn)的從上到下的線性漸變代碼
#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); /* 標(biāo)準(zhǔn)的語法 */
}
下面的實(shí)例演示了如何創(chuàng)建一個帶有彩虹顏色和文本的線性漸變:
#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);
/* 標(biāo)準(zhǔn)的語法 */
background: linear-gradient(to right, red,orange,yellow,green,blue,indigo,violet);
}
使用透明度(transparent)
為了添加透明度,我們使用 rgba() 函數(shù)來定義顏色結(jié)點(diǎn)。rgba() 函數(shù)中的最后一個參數(shù)可以是從 0 到 1 的值,它定義了顏色的透明度:0 表示完全透明,1 表示完全不透明。
下面的實(shí)例演示了從左邊開始的線性漸變。起點(diǎn)是完全透明,慢慢過渡到完全不透明的紅色:
從左到右的線性漸變,帶有透明度代碼
#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)); /* 標(biāo)準(zhǔn)的語法 */
}
重復(fù)的線性漸變
repeating-linear-gradient() 函數(shù)用于重復(fù)線性漸變:
代碼如下
#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%);
/* 標(biāo)準(zhǔn)的語法 */
background: repeating-linear-gradient(red, yellow 10%, green 20%);
}
今天就教到這里了,大家都會了嗎?祝大家國慶快樂~
有什么問題和建議可以私信小編:"666"
者:IT智云編程
鏈接:https://www.jianshu.com/p/4fa116fc4653
在web前端開發(fā)過程中,UI設(shè)計(jì)師經(jīng)常會設(shè)計(jì)一些帶漸變文字的設(shè)計(jì)圖,在以前我們只能用png的圖片來代替文字,今天可以實(shí)現(xiàn)使用純CSS實(shí)現(xiàn)漸變文字了。下面就介紹3中實(shí)現(xiàn)方式供大家參考!
基礎(chǔ)樣式:
.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>
效果:
這里推薦一下我的前端技術(shù)分享群:731771211,里面都是學(xué)習(xí)前端的,如果你想制作酷炫的網(wǎng)頁,想學(xué)習(xí)編程。自己整理了一份2018最全面前端學(xué)習(xí)資料,從最基礎(chǔ)的HTML+CSS+JS【炫酷特效,游戲,插件封裝,設(shè)計(jì)模式】到移動端HTML5的項(xiàng)目實(shí)戰(zhàn)的學(xué)習(xí)資料都有整理,送給每一位前端小伙伴,有想學(xué)習(xí)web前端的,或是轉(zhuǎn)行,或是大學(xué)生,還有工作中想提升自己能力的,正在學(xué)習(xí)的小伙伴歡迎加入學(xué)習(xí)。
*請認(rèn)真填寫需求信息,我們會在24小時內(nèi)與您取得聯(lián)系。