今天跟大家分享一個用 css 畫中國結(jié)的教程。
最終效果如下:
首先,我們定義好畫中國結(jié)需要的結(jié)構(gòu):
<div class="knot"> <span class="box"></span> <span class="box"></span> <span class="box"></span> <span class="box"></span> </div>
然后開始寫樣式,讓中國結(jié)居中顯示:
body { margin: 0; padding: 0; height: 100vh; display: flex; align-items: center; justify-content: center; }
設(shè)置裝中國結(jié)的容器樣式:
.knot { box-sizing: border-box; font-size: 100px; width: 2em; height: 1.6em; background: skyblue; display: flex; align-items: center; justify-content: center; }
我把中國結(jié)的基礎(chǔ)樣式拆分成4個長方形,首先來定義長方形的基礎(chǔ)樣式:
.box { position: absolute; box-sizing: border-box; width: 1em; height: 0.4em; border: var(--b) solid firebrick; --b: 0.1em; }
然后我們來調(diào)整每一個長方形的樣式,把它們組合成結(jié)的基礎(chǔ)樣子:
.knot .box:nth-child(1) { transform: rotate(45deg) translate(-15%, -38%); border-radius: 20% 0% 0% 20% / 50% 0 0 50%; } .knot .box:nth-child(2) { transform: rotate(45deg) translate(15%, 37%); border-radius: 0% 20% 20% 0% / 0% 50% 50% 0%; } .knot .box:nth-child(3) { transform: rotate(-45deg) translate(15%, -38%); border-radius: 0% 20% 20% 0% / 0% 50% 50% 0%; } .knot .box:nth-child(4) { transform: rotate(-45deg) translate(-15%, 37%); border-radius: 20% 0% 0% 20% / 50% 0 0 50%; }
最后,我們利用第一個和第二個長方形的偽元素來畫出余下的那兩個小圓圈:
.knot .box:nth-child(1)::after { box-sizing: border-box; content: ''; position: absolute; width: 0.4em; height: 0.4em; border: var(--b) solid firebrick; border-radius: 50% 50% 50% 0%; top: -0.4em; right: -0.4em; } .knot .box:nth-child(2)::after { box-sizing: border-box; content: ''; position: absolute; width: 0.4em; height: 0.4em; border: var(--b) solid firebrick; border-radius: 50% 0% 50% 50%; top: 0.2em; right: 0.8em; }
聲明:本網(wǎng)頁內(nèi)容旨在傳播知識,若有侵權(quán)等問題請及時與本網(wǎng)聯(lián)系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com