這篇文章將為大家詳細(xì)講解有關(guān)CSS中怎么使用徑向漸變實(shí)現(xiàn)卡券效果,小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。
創(chuàng)新互聯(lián)建站專(zhuān)注于企業(yè)營(yíng)銷(xiāo)型網(wǎng)站建設(shè)、網(wǎng)站重做改版、開(kāi)陽(yáng)網(wǎng)站定制設(shè)計(jì)、自適應(yīng)品牌網(wǎng)站建設(shè)、HTML5、商城網(wǎng)站建設(shè)、集團(tuán)公司官網(wǎng)建設(shè)、外貿(mào)營(yíng)銷(xiāo)網(wǎng)站建設(shè)、高端網(wǎng)站制作、響應(yīng)式網(wǎng)頁(yè)設(shè)計(jì)等建站業(yè)務(wù),價(jià)格優(yōu)惠性價(jià)比高,為開(kāi)陽(yáng)等各大城市提供網(wǎng)站開(kāi)發(fā)制作服務(wù)。
常見(jiàn)的卡券樣式如下:
使用偽元素實(shí)現(xiàn)(Less 版本)
ticket.less
.ordinary-mixins-ticket-horizontal(@width,@height,@r,@top, @color) { position: relative; box-sizing: border-box; padding: 0 @r; width: @width; height: @height; background-clip: content-box; background-color: @color; &::before { position: absolute; top: 0; left: 0; content: ""; display: block; width: @r + 1px; height: 100%; background: radial-gradient(@r circle at left @top, transparent @r, @color @r + 1px); } &::after { position: absolute; top: 0; right: 0; content: ""; display: block; //這里的 @r + 1px 是為了避免某些百分百比縮放頁(yè)面時(shí),出現(xiàn)空隙 width: @r + 1px; height: 100%; //這里的 @r + 1px 是為了防止出現(xiàn)鋸齒 background: radial-gradient(@r circle at right @top, transparent @r, @color @r + 1px); } } .parent { .ordinary-mixins-ticket-horizontal(200px, 200px, 10px, 150px, #fc3a28); } .child { line-height: 200px; }
App.js
import React from 'react'; import './App.css'; import './ticket.less'; function App() { return (); } export default App;666
升級(jí)版樣式一(Less 版本)
.mixins-ticket-vertical(@width, @height, @r, @left, @l-color, @r-color) { width: @width; height: @height; // @left - 1px 是為了避免某些百分百比縮放頁(yè)面時(shí),出現(xiàn)空隙 // @r + 1px 是為了防止出現(xiàn)鋸齒 // 51% 是為了防止出現(xiàn)元素中間會(huì)有一小段空白區(qū)域的情況 background: radial-gradient(circle at left top, transparent @r, @l-color @r + 1px) @left - 1px top ~'/' 100% 51% no-repeat, radial-gradient(circle at left bottom, transparent @r, @l-color @r + 1px) @left - 1px bottom ~'/' 100% 51% no-repeat, radial-gradient(circle at right top, transparent @r, @r-color @r + 1px ) -(@width - @left) top ~'/' 100% 51% no-repeat, radial-gradient(circle at right bottom , transparent @r, @r-color @r + 1px ) -(@width - @left) bottom ~'/' 100% 51% no-repeat; filter: drop-shadow(2px 2px 2px rgba(0, 0, 0, .2)); } .mixins-ticket-vertical-two(@width, @height, @r, @left, @l-color, @r-color) { width: @width; height: @height; // @left + 1px 是為了避免某些百分百比縮放頁(yè)面時(shí),出現(xiàn)空隙 // @r + 1px 是為了防止出現(xiàn)鋸齒 // 51% 是為了防止出現(xiàn)元素中間會(huì)有一小段空白區(qū)域的情況 background: radial-gradient(circle at left top, transparent @r, @r-color @r + 1px) right top ~'/' (@width - @left) 51% no-repeat, radial-gradient(circle at left bottom, transparent @r, @l-color @r + 1px) right bottom ~'/' (@width - @left) 51% no-repeat, radial-gradient(circle at right top, transparent @r, @r-color @r + 1px) left top ~'/' @left + 1px 51% no-repeat, radial-gradient(circle at right bottom, transparent @r, @l-color @r + 1px) left bottom ~'/' @left + 1px 51% no-repeat; filter: drop-shadow(2px 2px 2px rgba(0, 0, 0, .2)); } .parent { .mixins-ticket-vertical(200px, 200px, 10px, 150px, #fc3a28,#fc3a28); //.mixins-ticket-vertical-two(200px, 200px, 10px, 150px, #fc3a28,#fc3a28); } .child { line-height: 200px; }
升級(jí)版樣式一(Scss 版本)
@mixin mixins-ticket-vertical($width, $height, $r, $left, $l-color, $r-color) { width: $width; height: $height; // $left - 1px 是為了避免某些百分百比縮放頁(yè)面時(shí),出現(xiàn)空隙 // $r + 1px 是為了防止出現(xiàn)鋸齒 // 51% 是為了防止出現(xiàn)元素中間會(huì)有一小段空白區(qū)域的情況 background: radial-gradient(circle at left top, transparent $r, $l-color $r + 1px) $left - 1px top / 100% 51% no-repeat, radial-gradient(circle at left bottom, transparent $r, $l-color $r + 1px) $left - 1px bottom / 100% 51% no-repeat, radial-gradient(circle at right top, transparent $r, $r-color $r + 1px ) -($width - $left) top / 100% 51% no-repeat, radial-gradient(circle at right bottom , transparent $r, $r-color $r + 1px ) -($width - $left) bottom / 100% 51% no-repeat; filter: drop-shadow(2px 2px 2px rgba(0, 0, 0, .2)); } .parent { @include mixins-ticket-vertical(200px, 200px, 10px, 150px, #fc3a28, #fc3a28); } .child { line-height: 200px; }
升級(jí)版樣式二(Less 版本)
.mixins-ticket-horizontal(@width, @height, @r, @top, @l-color, @r-color) { width: @width; height: @height; background: radial-gradient(circle at left top, transparent @r, @l-color @r + 1px) left @top - 1px ~'/' 51% 100% no-repeat, radial-gradient(circle at left bottom, transparent @r, @l-color @r + 1px) left -(@height - @top) ~'/' 51% 100% no-repeat, radial-gradient(circle at right top, transparent @r, @r-color @r + 1px) right @top - 1px ~'/' 51% 100% no-repeat, radial-gradient(circle at right bottom, transparent @r, @r-color @r + 1px) right -(@height - @top) ~'/' 51% 100% no-repeat; filter: drop-shadow(2px 2px 2px rgba(0, 0, 0, .2)); } .mixins-ticket-horizontal-two(@width, @height, @r, @top, @l-color, @r-color) { width: @width; height: @height; background: radial-gradient(circle at left top, transparent @r, @r-color @r + 1px) left bottom ~'/' 51% (@height - @top) no-repeat, radial-gradient(circle at left bottom, transparent @r, @l-color @r + 1px) left top ~'/' 51% @top + 1px no-repeat, radial-gradient(circle at right top, transparent @r, @r-color @r + 1px) right bottom ~'/' 51% (@height - @top) no-repeat, radial-gradient(circle at right bottom, transparent @r, @l-color @r + 1px) right top ~'/' 51% @top + 1px no-repeat; filter: drop-shadow(2px 2px 2px rgba(0, 0, 0, .2)); } .parent { .mixins-ticket-horizontal(200px, 200px, 10px, 150px, #fc3a28,#fc3a28); //.mixins-ticket-horizontal-two(200px, 200px, 10px, 150px, #fc3a28,#fc3a28); } .child { line-height: 200px; }
升級(jí)版樣式三(Less 版本)
.mixins-ticket-horizontal-line(@width, @height, @r, @top, @l-color, @r-color,@border-offset,@border-color) { width: @width; height: @height; background: radial-gradient(circle at left top, transparent @r, @l-color @r + 1px) left @top - 1px ~'/' 51% 100% no-repeat, radial-gradient(circle at left bottom, transparent @r, @l-color @r + 1px) left -(@height - @top) ~'/' 51% 100% no-repeat, radial-gradient(circle at right top, transparent @r, @r-color @r + 1px) right @top - 1px ~'/' 51% 100% no-repeat, radial-gradient(circle at right bottom, transparent @r, @r-color @r + 1px) right -(@height - @top) ~'/' 51% 100% no-repeat; filter: drop-shadow(2px 2px 2px rgba(0, 0, 0, .2)); &::after { position: absolute; left: 0; right: 0; top: @top; margin: auto; content: ''; width: calc(~"100%" - 2*@r - @border-offset); border-top: 1px dashed @border-color; } } .parent { .mixins-ticket-horizontal(200px, 200px, 10px, 150px, #fc3a28,#fc3a28); //.mixins-ticket-horizontal-two(200px, 200px, 10px, 150px, #fc3a28,#fc3a28); } .child { line-height: 200px; }
升級(jí)版樣式四(Less 版本)
.mixins-ticket-vertical-three(@width, @height, @r, @left, @l-color, @r-color,@sm-r,@sm-offset,@sm-color) { width: @width; height: @height; // @left - 1px 是為了避免某些百分百比縮放頁(yè)面時(shí),出現(xiàn)空隙 // @r + 1px 是為了防止出現(xiàn)鋸齒 // 51% 是為了防止出現(xiàn)元素中間會(huì)有一小段空白區(qū)域的情況 background: radial-gradient(circle at left top, transparent @r, @l-color @r + 1px) @left - 1px top ~'/' 100% 51% no-repeat, radial-gradient(circle at left bottom, transparent @r, @l-color @r + 1px) @left - 1px bottom ~'/' 100% 51% no-repeat, radial-gradient(circle at right top, transparent @r, @r-color @r + 1px ) -(@width - @left) top ~'/' 100% 51% no-repeat, radial-gradient(circle at right bottom , transparent @r, @r-color @r + 1px ) -(@width - @left) bottom ~'/' 100% 51% no-repeat; filter: drop-shadow(2px 2px 2px rgba(0, 0, 0, .2)); &::after { content: ''; position: absolute; top: 0; right: -@sm-r; width: @sm-r; height: 100%; background-image: radial-gradient(circle at @sm-r, transparent @sm-r, @r-color @sm-r + 1px); //background-size: @sm-r; background-size: @sm-r @sm-offset; } } .parent { .mixins-ticket-vertical-three(200px, 200px, 10px, 150px, #fc3a28, #fc3a28,5px,20px,#fc3a28); } .child { line-height: 200px; }
注意事項(xiàng)
// ticket.less //background: radial-gradient(circle at top right, transparent @r, @lcolor 0) -(@width - @left) top ~'/' 100% 55% no-repeat; // 將上面的這行代碼拆解如下: //background-image: radial-gradient(circle at top right, transparent @r, @lcolor 0); //background-position:-(@width - @left) top ; //background-size:100% 55% ; //background-repeat: no-repeat; /*注意:這里先是有 50px 的透明區(qū)域,緊接著第二個(gè)區(qū)域設(shè)置了 0 ,可以理解為從這里開(kāi)始重新設(shè)置樣式區(qū)間*/ /*background: radial-gradient(circle at top right, transparent 50px, red 0, #66a8ff 50%);*/ /*在Chrome下,如果兩個(gè)區(qū)域之間顏色直接以 0 偏差過(guò)渡,會(huì)有比較嚴(yán)重的鋸齒*/ /*background: radial-gradient(10px at left,transparent 50%,#F6327C 50%);*/ /*background: radial-gradient(10px at left,transparent 50%,#F6327C 55%);*/ /*加陰影效果*/ /*filter: drop-shadow(2px 2px 2px rgba(0,0,0,.2));*/ /*多個(gè)徑向漸變累加*/ /*background: radial-gradient(50px 100px ellipse, transparent 40px, yellow 41px, red 49px, transparent 50px), radial-gradient(30px circle, red, red 29px, transparent 30px);*/
關(guān)于“CSS中怎么使用徑向漸變實(shí)現(xiàn)卡券效果”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,使各位可以學(xué)到更多知識(shí),如果覺(jué)得文章不錯(cuò),請(qǐng)把它分享出去讓更多的人看到。