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

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

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

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

效果预览

1958362444-5ba9a3d852436_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: 40.5em;
  4. height: 71em;
  5. font-size: 6px;
  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: radial-gradient(circle, teal 50%, transparent 50%),
  14. radial-gradient(circle, teal 50%, transparent 50%);
  15. background-size: 3.5em 3.5em;
  16. }
  17. .stamp::before {
  18. top: 1.5em;
  19. background-repeat: repeat-y;
  20. background-position: -4.5% 0, 104.5% 0;
  21. }
  22. .stamp::after {
  23. left: 1.5em;
  24. background-repeat: repeat-x;
  25. background-position: 0 -2.5%, 0 102.5%;
  26. }

在 html 文件中增加小狗的 dom 元素,子元素分别表示耳朵、头部、眼睛、舌头、身体、尾巴和爪子:

  1. <div class="stamp">
  2. <div class="puppy">
  3. <span class="ear"></span>
  4. <span class="head"></span>
  5. <span class="eyes"></span>
  6. <span class="tongue"></span>
  7. <span class="body"></span>
  8. <span class="tail"></span>
  9. <span class="foot"></span>
  10. </div>
  11. </div>

设置 grid 布局的行列尺寸:

  1. .puppy {
  2. display: grid;
  3. grid-template-columns: 10em 22.5em 8em;
  4. grid-template-rows: 21em 12.5em 3.75em 22.5em;
  5. background-color: tan;
  6. padding: 2em;
  7. margin-top: -1em;
  8. }

画出小狗的头部,跨第1列和第2列、第2行和第3行,是一个半圆形:

  1. .head {
  2. grid-column: 1 / 3;
  3. grid-row: 2 / 4;
  4. border-bottom-left-radius: calc(12.5em + 3.75em);
  5. border-bottom-right-radius: calc(12.5em + 3.75em);
  6. background-color: bisque;
  7. }

用伪元素画出鼻子,是一个扇形,多余的部分被隐藏了:

  1. .head {
  2. position: relative;
  3. overflow: hidden;
  4. }
  5. .head::before {
  6. content: '';
  7. position: absolute;
  8. width: 7em;
  9. height: 7em;
  10. border-bottom-right-radius: 100%;
  11. background-color: sienna;
  12. }

画出半圆形的眼晕:

  1. .eyes {
  2. grid-column: 2;
  3. grid-row: 2;
  4. justify-self: end;
  5. position: relative;
  6. height: 10.5em;
  7. width: 21em;
  8. border-radius: 0 0 10.5em 10.5em;
  9. background-color: sienna;
  10. }

用径向渐变画出眼珠:

  1. .eyes {
  2. background-image: radial-gradient(
  3. circle at 37% 33%,
  4. black 1.4em,
  5. transparent 1.4em
  6. );
  7. }

画出半圆形的耳朵:

  1. .ear {
  2. grid-column: 2;
  3. grid-row: 1;
  4. justify-self: end;
  5. width: 10.5em;
  6. border-radius: 21em 0 0 21em;
  7. background-color: sienna;
  8. }

画出扇形的舌头:

  1. .tongue {
  2. grid-column: 1;
  3. grid-row: 3;
  4. width: 5.5em;
  5. height: 5.5em;
  6. background-color: indianred;
  7. border-bottom-left-radius: 100%;
  8. }

画出扇形的身体:

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

用伪元素,通过阴影画出中蹲着的腿:

  1. .body {
  2. position: relative;
  3. overflow: hidden;
  4. }
  5. .body::after {
  6. content: '';
  7. position: absolute;
  8. height: 50%;
  9. width: 100%;
  10. border-radius: 11.25em 11.25em 0 0;
  11. box-shadow: 2em 0 4em rgba(0, 0, 0, 0.3);
  12. bottom: 0;
  13. }

画出半圆形的尾巴:

  1. .tail {
  2. grid-column: 1;
  3. grid-row: 4;
  4. justify-self: end;
  5. align-self: end;
  6. height: 17.5em;
  7. width: 8.75em;
  8. background-color: bisque;
  9. border-radius: 17.5em 0 0 17.5em;
  10. }

画出半圆形的小爪子:

  1. .foot {
  2. grid-column: 3;
  3. grid-row: 4;
  4. align-self: end;
  5. height: 4em;
  6. background-color: bisque;
  7. border-radius: 4em 4em 0 0;
  8. }

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

  1. <div class="stamp">
  2. <div class="puppy">
  3. <!-- 略 -->
  4. </div>
  5. <p class="text">
  6. <span class="title">Puppy</span>
  7. <span class="author">comehope</span>
  8. <span class="face-value">80</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: sienna;
  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网其它相关文章!

人气教程排行