资讯专栏INFORMATION COLUMN

前端每日实战:141# 视频演示如何用 CSS 的 Grid 布局创作一枚小狗邮票

Baoyuan / 1760人阅读

摘要:效果预览按下右侧的点击预览按钮可以在当前页面预览,点击链接可以全屏预览。可交互视频此视频是可以交互的,你可以随时暂停视频,编辑视频中的代码。

效果预览

按下右侧的“点击预览”按钮可以在当前页面预览,点击链接可以全屏预览。

https://codepen.io/comehope/pen/BOeEYV

可交互视频

此视频是可以交互的,你可以随时暂停视频,编辑视频中的代码。

请用 chrome, safari, edge 打开观看。

https://scrimba.com/p/pEgDAM/cPQ3vcq

源代码下载

每日前端实战系列的全部源代码请从 github 下载:

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

代码解读

定义 dom,容器表示邮票:

</>复制代码

居中显示:

</>复制代码

  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 元素,子元素分别表示耳朵、头部、眼睛、舌头、身体、尾巴和爪子:

</>复制代码

设置 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. Puppy
  2. comehope
  3. 80

设置标题的文字样式:

</>复制代码

  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. }

大功告成!

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/116983.html

相关文章

  • 前端每日实战141# 视频演示何用 CSS Grid 布局创作一枚小狗邮票

    摘要:效果预览按下右侧的点击预览按钮可以在当前页面预览,点击链接可以全屏预览。可交互视频此视频是可以交互的,你可以随时暂停视频,编辑视频中的代码。 showImg(https://segmentfault.com/img/bVbhqjK?w=400&h=300); 效果预览 按下右侧的点击预览按钮可以在当前页面预览,点击链接可以全屏预览。 https://codepen.io/comehop...

    yintaolaowanzi 评论0 收藏0
  • 前端每日实战 2018 年 9 月份项目汇总(共 26 个项目)

    摘要:过往项目年月份项目汇总共个项目年月份项目汇总共个项目年月份项目汇总共个项目年月份项目汇总共个项目年月份项目汇总共个项目年月份发布的项目前端每日实战专栏每天分解一个前端项目,用视频记录编码过程,再配合详细的代码解读,是学习前端开发的活的参考书 过往项目 2018 年 8 月份项目汇总(共 29 个项目) 2018 年 7 月份项目汇总(共 29 个项目) 2018 年 6 月份项目汇总(...

    lavnFan 评论0 收藏0
  • 前端每日实战 2018 年 9 月份项目汇总(共 26 个项目)

    摘要:过往项目年月份项目汇总共个项目年月份项目汇总共个项目年月份项目汇总共个项目年月份项目汇总共个项目年月份项目汇总共个项目年月份发布的项目前端每日实战专栏每天分解一个前端项目,用视频记录编码过程,再配合详细的代码解读,是学习前端开发的活的参考书 过往项目 2018 年 8 月份项目汇总(共 29 个项目) 2018 年 7 月份项目汇总(共 29 个项目) 2018 年 6 月份项目汇总(...

    whatsns 评论0 收藏0

发表评论

0条评论

Baoyuan

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<