当前位置:Gxlcms > css > 如何使用css实现中国结的效果(代码)

如何使用css实现中国结的效果(代码)

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

这篇文章给大家带来的内容是关于如何使用css实现中国结的效果(代码) ,有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。

今天跟大家分享一个用 css 画中国结的教程。
最终效果如下:

3965247858-5b91e0d8136d8_articlex.png

首先,我们定义好画中国结需要的结构:

  1. <div class="knot">
  2. <span class="box"></span>
  3. <span class="box"></span>
  4. <span class="box"></span>
  5. <span class="box"></span>
  6. </div>

然后开始写样式,让中国结居中显示:

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

设置装中国结的容器样式:

  1. .knot {
  2. box-sizing: border-box;
  3. font-size: 100px;
  4. width: 2em;
  5. height: 1.6em;
  6. background: skyblue;
  7. display: flex;
  8. align-items: center;
  9. justify-content: center;
  10. }

我把中国结的基础样式拆分成4个长方形,首先来定义长方形的基础样式:

  1. .box {
  2. position: absolute;
  3. box-sizing: border-box;
  4. width: 1em;
  5. height: 0.4em;
  6. border: var(--b) solid firebrick;
  7. --b: 0.1em;
  8. }

然后我们来调整每一个长方形的样式,把它们组合成结的基础样子:

  1. .knot .box:nth-child(1) {
  2. transform: rotate(45deg) translate(-15%, -38%);
  3. border-radius: 20% 0% 0% 20% / 50% 0 0 50%;
  4. }
  5. .knot .box:nth-child(2) {
  6. transform: rotate(45deg) translate(15%, 37%);
  7. border-radius: 0% 20% 20% 0% / 0% 50% 50% 0%;
  8. }
  9. .knot .box:nth-child(3) {
  10. transform: rotate(-45deg) translate(15%, -38%);
  11. border-radius: 0% 20% 20% 0% / 0% 50% 50% 0%;
  12. }
  13. .knot .box:nth-child(4) {
  14. transform: rotate(-45deg) translate(-15%, 37%);
  15. border-radius: 20% 0% 0% 20% / 50% 0 0 50%;
  16. }

最后,我们利用第一个和第二个长方形的伪元素来画出余下的那两个小圆圈:

  1. .knot .box:nth-child(1)::after {
  2. box-sizing: border-box;
  3. content: '';
  4. position: absolute;
  5. width: 0.4em;
  6. height: 0.4em;
  7. border: var(--b) solid firebrick;
  8. border-radius: 50% 50% 50% 0%;
  9. top: -0.4em;
  10. right: -0.4em;
  11. }
  12. .knot .box:nth-child(2)::after {
  13. box-sizing: border-box;
  14. content: '';
  15. position: absolute;
  16. width: 0.4em;
  17. height: 0.4em;
  18. border: var(--b) solid firebrick;
  19. border-radius: 50% 0% 50% 50%;
  20. top: 0.2em;
  21. right: 0.8em;
  22. }

相关推荐:

如何使用纯CSS实现一个圆环旋转错觉的动画效果(附源码)

如何使用纯CSS实现太阳和地球和月亮的运转模型动画

以上就是如何使用css实现中国结的效果(代码)的详细内容,更多请关注Gxl网其它相关文章!

人气教程排行