博客已迁移至http://lwzhang.github.io。
纯CSS实现一个微信logo并不难,难的是怎样用最少的 html 标签实现。我一直在想怎样用一个标签就能实现,最后还是没想出来,就只好用两个标签了。
首先需要两个标签元素:
登录后复制
先画个背景:
.bg { width: 300px; height: 300px; background-color: #08c406; border-radius: 10px; position: relative; }
登录后复制
再画个大的椭圆:
立即学习“前端免费学习笔记(深入)”;
.inner { width: 180px; height: 150px; border-radius: 50%; background-color: #fff; position: absolute; top: 60px; left: 35px; }
登录后复制
小的椭圆利用 .inner 的 ::before 伪元素实现:
&::before { content: ""; width: 140px; height: 120px; border-radius: 50%; background-color: #fff; position: absolute; top: 60px; left: 90px; border: 2px solid #08c406; }
登录后复制
下图时现在的结果:
里面的四个圆怎么画呢?可以利用CSS3的 box-shadow 属性实现,一般重复性的东西都会用这个属性,因为它可以制造出无数个一模一样的东西出来。
利用 .bg 的 ::before 伪元素实现这些圆:
&::before { content: ""; position: absolute; width: 16px; height: 16px; border-radius: 50%; background-color: #08c406; top: 150px; left: 155px; z-index: 2; box-shadow: 70px 0 #08c406, -70px -50px 0 2px #08c406, 0 -50px 0 2px #08c406; }
登录后复制
::before 本身会实现一个圆(一个小圆),然后利用 box-shadow 属性实现其它的三个圆。
来看看现在的效果:
现在就剩下两个角了,想想还有哪些东西没用上?还有两个伪元素,分别是 .bg 的 ::after 和 .inner 的 ::after ,刚好可以实现两个角。
这两个角其实就是平常的小三角,然后再旋转个 45 度, CSS 实现小三角太常见了:
.bg::after { content: ""; border-width: 30px 12px; border-style: solid; border-color: #fff transparent transparent transparent; position: absolute; top: 182px; left: 50px; transform: rotate(45deg); } .inner::after { content: ""; border-width: 30px 10px; border-style: solid; border-color: #fff transparent transparent transparent; position: absolute; top: 155px; left: 200px; transform: rotate(-45deg); }
登录后复制
最终效果:
全部 css 代码:
@mixin pos($left, $top) { position: absolute; left: $left; top: $top; } .bg { width: 300px; height: 300px; background-color: #08c406; border-radius: 10px; position: relative; &::before { @include pos(155px, 150px); content: ""; width: 16px; height: 16px; border-radius: 50%; background-color: #08c406; z-index: 2; box-shadow: 70px 0 #08c406, -70px -50px 0 2px #08c406, 0 -50px 0 2px #08c406; } &::after { @include pos(50px, 182px); content: ""; border-width: 30px 12px; border-style: solid; border-color: #fff transparent transparent transparent; transform: rotate(45deg); } .inner { width: 180px; height: 150px; border-radius: 50%; background-color: #fff; @include pos(35px, 60px); &::before { @include pos(90px, 60px); content: ""; width: 140px; height: 120px; border-radius: 50%; background-color: #fff; border: 2px solid #08c406; } &::after { @include pos(200px, 155px); content: ""; border-width: 30px 10px; border-style: solid; border-color: #fff transparent transparent transparent; transform: rotate(-45deg); } } }
登录后复制
画这个logo最难的地方应该就是实现四个小圆的时候,因为 CSS3 不太熟的人可能不会想到利用 box-shadow 去实现。
大家还有其它的方法实现微信logo吗?有没有一个标签就能实现的?欢迎留下你的实现方式。
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至253000106@qq.com举报,一经查实,本站将立刻删除。
发布者:PHP中文网,转转请注明出处:https://www.chuangxiangniao.com/p/3111086.html