当前位置:Gxlcms > css > 如何使用CSS的Grid布局实现小鸡邮票(附代码)

如何使用CSS的Grid布局实现小鸡邮票(附代码)

时间:2021-07-01 10:21:17 帮助过:13人阅读

本篇文章给大家带来的内容是关于如何使用CSS的Grid布局实现小鸡邮票(附代码),有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。

效果预览

1823763689-5ba9be40dcb54_articlex.png

源代码下载

https://github.com/comehope/front-end-daily-challenges

代码解读

定义 dom,容器表示邮票:

  1. <div class="stamp">
  2. </div>

居中显示:

  1. body {
  2. margin: 0;
  3. height: 100vh;
  4. display: flex;
  5. align-items: center;
  6. justify-content: center;
  7. background-color: teal;
  8. }

设置容器尺寸:

  1. .stamp {
  2. position: relative;
  3. width: 57em;
  4. height: 71em;
  5. font-size: 5px;
  6. padding: 5em;
  7. background-color: white;
  8. }

用重复背景绘制出邮票的齿孔:

  1. .stamp {
  2. display: flex;
  3. flex-direction: column;
  4. align-items: center;
  5. justify-content: center;
  6. }
  7. .stamp::after,
  8. .stamp::before {
  9. content: '';
  10. width: 100%;
  11. height: 100%;
  12. position: absolute;
  13. background:
  14. radial-gradient(circle, teal 50%, transparent 50%),
  15. radial-gradient(circle, teal 50%, transparent 50%);
  16. background-size: 3.5em 3.5em;
  17. }
  18. .stamp::before {
  19. top: 1.5em;
  20. background-repeat: repeat-y;
  21. background-position: -3% 0, 103% 0;
  22. }
  23. .stamp::after {
  24. left: 1.5em;
  25. background-repeat: repeat-x;
  26. background-position: 0 -2.5%, 0 102.5%;
  27. }

在 html 文件中增加小鸡的 dom 元素,子元素分别表示头部、喙、身体、尾巴、腿、爪子、太阳、桔子:

  1. <div class="stamp">
  2. <div class="rooster">
  3. <span class="head"></span>
  4. <span class="beak"></span>
  5. <span class="body"></span>
  6. <span class="tail"></span>
  7. <span class="leg"></span>
  8. <span class="foot"></span>
  9. <span class="sun"></span>
  10. <span class="orange-stuff"></span>
  11. </div>
  12. </div>

设置 grid 布局的行列尺寸:

  1. .rooster {
  2. display: grid;
  3. grid-template-columns: 22.5em 13em 1.75em 14.5em 4.5em;
  4. grid-template-rows: 12.5em 14.5em 15em 8em 5.5em;
  5. background-color: wheat;
  6. padding: 2em;
  7. margin-top: -2em;
  8. }

画出扇形的头部:

  1. .head {
  2. grid-column: 4;
  3. grid-row: 2;
  4. background-color: burlywood;
  5. border-top-left-radius: 100%;
  6. }

画出小鸡的眼睛和脸上的红晕:

  1. .head {
  2. position: relative;
  3. }
  4. .head::after {
  5. content: '';
  6. position: absolute;
  7. width: 2.8em;
  8. height: 2.8em;
  9. border-radius: 50%;
  10. background-color: black;
  11. right: 30%;
  12. box-shadow: 2em 4em 4em rgba(255, 100, 0, 0.5);
  13. }

画出扇形的喙:

  1. .beak {
  2. grid-column: 5;
  3. grid-row: 2;
  4. height: 4.5em;
  5. background-color: darkorange;
  6. border-bottom-right-radius: 100%;
  7. }

画出半圆形的身体:

  1. .body {
  2. grid-column: 2 / 5;
  3. grid-row: 3;
  4. width: 30em;
  5. background-color: saddlebrown;
  6. border-radius: 0 0 15em 15em;
  7. }

用伪元素,通过阴影画出翅膀:

  1. .body {
  2. position: relative;
  3. overflow: hidden;
  4. }
  5. .body::after {
  6. content: '';
  7. position: absolute;
  8. width: 20em;
  9. height: 10em;
  10. border-radius: inherit;
  11. box-shadow: 4em 2em 4em rgba(0, 0, 0, 0.3);
  12. left: calc((30em - 20em) / 2);
  13. }

画出扇形的尾巴:

  1. .tail {
  2. grid-column: 1;
  3. grid-row: 1 / 3;
  4. height: 22.5em;
  5. background-color: burlywood;
  6. align-self: end;
  7. border-top-left-radius: 100%;
  8. }

画出扇形的腿:

  1. .leg {
  2. grid-column: 4;
  3. grid-row: 4;
  4. width: 8em;
  5. background-color: burlywood;
  6. border-bottom-right-radius: 100%;
  7. }

画出扇形的小爪子:

  1. .foot {
  2. grid-column: 4;
  3. grid-row: 5;
  4. width: 5.5em;
  5. background-color: darkorange;
  6. border-top-right-radius: 100%;
  7. }

画出半圆形的太阳:

  1. .sun {
  2. grid-column: 3 / 5;
  3. grid-row: 1;
  4. width: 17em;
  5. --h: calc(17em / 2);
  6. height: var(--h);
  7. background-color: darkorange;
  8. border-radius: 0 0 var(--h) var(--h);
  9. }

画出圆形的桔子和半圆形的叶片,注意此处叶片的画法与前面画半圆形的画法不同:

  1. .orange-stuff {
  2. grid-column: 1;
  3. grid-row: 3 / 6;
  4. width: 16em;
  5. height: 16em;
  6. background-color: darkorange;
  7. align-self: end;
  8. justify-self: end;
  9. border-radius: 50%;
  10. position: relative;
  11. }
  12. .orange-stuff::before {
  13. content: '';
  14. position: absolute;
  15. width: 8em;
  16. height: 8em;
  17. background: linear-gradient(45deg, transparent 50%, saddlebrown 50%);
  18. border-radius: 50%;
  19. top: -6.8em;
  20. left: 10%;
  21. }

在 dom 中再增加一些文本,包括标题、作者和面值:

  1. <div class="stamp">
  2. <div class="puppy">
  3. <!-- 略 -->
  4. </div>
  5. <p class="text">
  6. <span class="title">Rooster</span>
  7. <span class="author">comehope</span>
  8. <span class="face-value">120</span>
  9. </p>
  10. </div>

设置标题的文字样式:

  1. .text {
  2. position: relative;
  3. width: calc(100% + 2em * 2);
  4. height: 6em;
  5. font-family: sans-serif;
  6. }
  7. .text .title {
  8. position: absolute;
  9. font-size: 6em;
  10. font-weight: bold;
  11. color: brown;
  12. }

设置作者的文字样式:

  1. .text .author {
  2. position: absolute;
  3. font-size: 3em;
  4. bottom: -1.2em;
  5. color: dimgray;
  6. }

设置面值的文字样式:

  1. .text .face-value {
  2. position: absolute;
  3. font-size: 14em;
  4. right: 0;
  5. line-height: 0.9em;
  6. color: darkcyan;
  7. }

大功告成!

以上就是如何使用CSS的Grid布局实现小鸡邮票(附代码)的详细内容,更多请关注Gxl网其它相关文章!

人气教程排行