首頁 > web前端 > css教學 > 主體

如何使用CSS的Grid佈局實現小狗郵票(附源碼)

不言
發布: 2018-09-25 16:08:05
原創
2132 人瀏覽過

這篇文章帶給大家的內容是關於如何使用CSS的Grid佈局實現小狗郵票(附源碼),有一定的參考價值,有需要的朋友可以參考一下,希望對你有所幫助。

效果預覽

如何使用CSS的Grid佈局實現小狗郵票(附源碼)

原始碼下載

https://github.com/comehop​​e/front-end-daily -challenges

程式碼解讀

定義dom,容器表示郵票:

<div>
</div>
登入後複製

居中顯示:

body {
    margin: 0;
    height: 100vh;
    display: flex;
    align-items: center;
    justify-content: center;
    background-color: teal;
}
登入後複製

設定容器尺寸:

.stamp {
    position: relative;
    width: 40.5em;
    height: 71em;
    font-size: 6px;
    padding: 5em;
    background-color: white;
}
登入後複製

以重複背景畫出郵票的齒孔:

.stamp {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
}

.stamp::after,
.stamp::before {
    content: '';
    width: 100%;
    height: 100%;
    position: absolute;
    background: radial-gradient(circle, teal 50%, transparent 50%),
    radial-gradient(circle, teal 50%, transparent 50%);
    background-size: 3.5em 3.5em;
}

.stamp::before {
    top: 1.5em;
    background-repeat: repeat-y;
    background-position: -4.5% 0, 104.5% 0;
}

.stamp::after {
    left: 1.5em;
    background-repeat: repeat-x;
    background-position: 0 -2.5%, 0 102.5%;
}
登入後複製

在html 檔案中增加小狗的dom 元素,子元素分別表示耳朵、頭部、眼睛、舌頭、身體、尾巴和爪子:

<div>
    <div>
        <span></span>
        <span></span>
        <span></span>
        <span></span>
        <span></span>
        <span></span>
        <span></span>
    </div>
</div>
登入後複製

設定grid 佈局的行列尺寸:

.puppy {
    display: grid;
    grid-template-columns: 10em 22.5em 8em;
    grid-template-rows: 21em 12.5em 3.75em 22.5em;
    background-color: tan;
    padding: 2em;
    margin-top: -1em;
}
登入後複製

畫出小狗的頭部,跨第1列和第2列、第2行和第3行,是半圓形:

.head {
    grid-column: 1 / 3;
    grid-row: 2 / 4;
    border-bottom-left-radius: calc(12.5em + 3.75em);
    border-bottom-right-radius: calc(12.5em + 3.75em);
    background-color: bisque;
}
登入後複製

用偽元素畫出鼻子,是一個扇形,多餘的部分被隱藏了:

.head {
    position: relative;
    overflow: hidden;
}

.head::before {
    content: '';
    position: absolute;
    width: 7em;
    height: 7em;
    border-bottom-right-radius: 100%;
    background-color: sienna;
}
登入後複製

畫出半圓形的眼暈:

.eyes {
    grid-column: 2;
    grid-row: 2;
    justify-self: end;
    position: relative;
    height: 10.5em;
    width: 21em;
    border-radius: 0 0 10.5em 10.5em;
    background-color: sienna;
}
登入後複製

用徑向漸層畫出眼珠:

.eyes {
    background-image: radial-gradient(
        circle at 37% 33%,
        black 1.4em,
        transparent 1.4em
    );
}
登入後複製

畫出半圓形的耳朵:

.ear {
    grid-column: 2;
    grid-row: 1;
    justify-self: end;
    width: 10.5em;
    border-radius: 21em 0 0 21em;
    background-color: sienna;
}
登入後複製

畫出扇形的舌頭:

.tongue {
    grid-column: 1;
    grid-row: 3;
    width: 5.5em;
    height: 5.5em;
    background-color: indianred;
    border-bottom-left-radius: 100%;
}
登入後複製

畫出扇形的身體:

.body {
    grid-column: 2;
    grid-row: 4;
    background-color: sienna;
    border-top-left-radius: 100%;
}
登入後複製

用偽元素,透過陰影畫出中蹲著的腿:

.body {
    position: relative;
    overflow: hidden;
}

.body::after {
    content: '';
    position: absolute;
    height: 50%;
    width: 100%;
    border-radius: 11.25em 11.25em 0 0;
    box-shadow: 2em 0 4em rgba(0, 0, 0, 0.3);
    bottom: 0;
}
登入後複製

畫出半圓形的尾巴:

.tail {
    grid-column: 1;
    grid-row: 4;
    justify-self: end;
    align-self: end;
    height: 17.5em;
    width: 8.75em;
    background-color: bisque;
    border-radius: 17.5em 0 0 17.5em;
}
登入後複製

畫出半圓形的小爪子:

.foot {
    grid-column: 3;
    grid-row: 4;
    align-self: end;
    height: 4em;
    background-color: bisque;
    border-radius: 4em 4em 0 0;
}
登入後複製

在dom 中再增加一些文本,包括標題、作者和麵值:

<div>
    <div>
        <!-- 略 -->
    </div>
    <p>
        <span>Puppy</span>
        <span>comehope</span>
        <span>80</span>
    </p>
</div>
登入後複製

設定標題的文字樣式:

.text {
    position: relative;
    width: calc(100% + 2em * 2);
    height: 6em;
    font-family: sans-serif;
}

.text .title {
    position: absolute;
    font-size: 6em;
    font-weight: bold;
    color: sienna;
}
登入後複製

設定作者的文字樣式:

.text .author {
    position: absolute;
    font-size: 3em;
    bottom: -1.2em;
    color: dimgray;
}
登入後複製

設定面值的文字樣式:

.text .face-value {
    position: absolute;
    font-size: 14em;
    right: 0;
    line-height: 0.9em;
    color: darkcyan;
}
登入後複製

大功告成!

以上是如何使用CSS的Grid佈局實現小狗郵票(附源碼)的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
作者最新文章
最新問題
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!