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

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

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

3965247858-5b91e0d8136d8_articlex.png

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

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

body {  margin: 0;  padding: 0;  height: 100vh;  display: flex;  align-items: center;  justify-content: center;}

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

立即学习“前端免费学习笔记(深入)”;

.knot {  box-sizing: border-box;  font-size: 100px;  width: 2em;  height: 1.6em;  background: skyblue;  display: flex;  align-items: center;  justify-content: center;}

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

.box {  position: absolute;  box-sizing: border-box;  width: 1em;  height: 0.4em;  border: var(--b) solid firebrick;  --b: 0.1em;}

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

.knot .box:nth-child(1) {  transform: rotate(45deg) translate(-15%, -38%);  border-radius: 20% 0% 0% 20% / 50% 0 0 50%;}.knot .box:nth-child(2) {  transform: rotate(45deg) translate(15%, 37%);  border-radius: 0% 20% 20% 0% / 0% 50% 50% 0%;}.knot .box:nth-child(3) {  transform: rotate(-45deg) translate(15%, -38%);  border-radius: 0% 20% 20% 0% / 0% 50% 50% 0%;}.knot .box:nth-child(4) {  transform: rotate(-45deg) translate(-15%, 37%);  border-radius: 20% 0% 0% 20% / 50% 0 0 50%;}

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

.knot .box:nth-child(1)::after {  box-sizing: border-box;  content: '';  position: absolute;  width: 0.4em;  height: 0.4em;  border: var(--b) solid firebrick;  border-radius: 50% 50% 50% 0%;  top: -0.4em;  right: -0.4em;}.knot .box:nth-child(2)::after {  box-sizing: border-box;  content: '';  position: absolute;  width: 0.4em;  height: 0.4em;  border: var(--b) solid firebrick;  border-radius: 50% 0% 50% 50%;  top: 0.2em;  right: 0.8em;}

相关推荐:

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

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

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

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/1611449.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
详解css display属性中常见的四个属性值用法(代码实例)
上一篇 2025年12月24日 02:19:26
CSS3中border-radius怎么使用?实例讲解用border-radius画一个圆
下一篇 2025年12月24日 02:19:42

相关推荐

发表回复

登录后才能评论
关注微信