還在爲上面這樣格式各樣的商城卡券的樣式而發愁?CSS 不熟,canvas 太難,怎麼辦?css
用 CSS 寫一個商城卡券須要幾步?git
一共須要三步:github
咱們來準備一下基礎知識canvas
background: radial-gradient(shape size at position, start-color, ..., last-color); 複製代碼
值 | 描述 |
---|---|
shape | 肯定圓的類型: ellipse (默認): 指定橢圓形的徑向漸變。 circle :指定圓形的徑向漸變 |
size | 定義漸變的大小 |
position | 定義漸變的位置 |
這樣,咱們能很容易寫出一個居中的圓形背景圖瀏覽器
.center-circle { width: 100px; height: 100px; background: radial-gradient(circle at 50px 50px, transparent 10px, #00adb5 0); } 複製代碼
background: linear-gradient(direction, color-stop1, color-stop2, ...); 複製代碼
值 | 描述 |
---|---|
direction | 用角度值指定漸變的方向(或角度) |
color-stop1, color-stop2,... | 用於指定漸變的起止顏色 |
咱們不須要知道具體的漸變過程,只要寫一個簡單的,寫一個使用漸變屬性而不漸變背景圖便可:markdown
.linear-gradient { width: 100px; height: 100px; background: linear-gradient(to right, #00adb5, #00adb5); } 複製代碼
background
是能夠設置多個圖片的,遵循background: [background-color] [background-image] [background-repeat] [background-attachment] [background-position] / [ background-size] [background-origin] [background-clip];
使用,
隔開便可。oop
只要把上述中心圓示例的圓形位置定位在左側便可學習
.left-circle{ width: 100px; height: 100px; position: relative; background: radial-gradient(circle at 0 50px, transparent 10px, #00adb5 0) top left/100px 100% no-repeat; } 複製代碼
你可還記得 background
是有 repeat
屬性嗎?也就是說咱們只要設置一部分樣式,再使用 repeat
便可,看下圖片,這不就是一個不漸變的 linear-gradient
和 radial-gradient
的組合嗎,再借助僞類,咱們便可寫出來了。測試
.hollow-circles { width: 300px; height: 100px; position: relative; background: #00adb5; margin-bottom: 10px; } .hollow-circles::after { content: ''; position: absolute; height: 5px; width:100%; left: 0; bottom: -5px; background-image: linear-gradient(to right, #00adb5 5px, transparent 5px, transparent), radial-gradient(10px circle at 10px 5px, transparent 5px, #00adb5 5px); background-size: 15px 5px; } 複製代碼
看見很簡單,不就是剛纔那個圓再畫一個嗎,可是要考慮到兩側的顏色是不一樣的,因此咱們須要畫四個背景圖才行,將每一個圓定位在方形的各個角落,而後組合在一塊兒便可。spa
.two-circles { width: 300px; height: 100px; background: radial-gradient(circle at right top, transparent 10px, #00adb5 0) top left / 60px 51% no-repeat, radial-gradient(circle at right bottom, transparent 10px, #00adb5 0) bottom left /60px 51% no-repeat, radial-gradient(circle at left top, transparent 10px, #eeeeee 0) top right /240px 51% no-repeat, radial-gradient(circle at left bottom, transparent 10px, #eeeeee 0) bottom right /240px 51% no-repeat; } 複製代碼
當咱們看到一個新東西時,不要抵觸,試着去分析一下,有可能他只是咱們之前所學的變體而已,僅僅是對咱們已有知識的從新組合和適當改造,當咱們搞定了的時候,也許才發現,這也不過如此,然而假若當初懷着抵觸拒絕,卻只能是一點進步沒有了。
最後的最後,這個網址歡迎 star ~,更歡迎 issue ~。