资讯专栏INFORMATION COLUMN

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

NicolasHe / 752人阅读

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

效果预览

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

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

可交互视频

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

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

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

源代码下载

每日前端实战系列的全部源代码请从 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: 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 元素,子元素分别表示头部、喙、身体、尾巴、腿、爪子、太阳、桔子:

</>复制代码

设置 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. Rooster
  2. comehope
  3. 120

设置标题的文字样式:

</>复制代码

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

大功告成!

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

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

相关文章

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

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

    piglei 评论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条评论

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