温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

CSS图形绘制方法有哪些

发布时间:2021-11-06 15:00:49 阅读:329 作者:iii 栏目:web开发
前端开发者测试专用服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

本篇内容介绍了“CSS图形绘制方法有哪些”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

1. 正方形

实时渲染效果如下:

相关CSS代码:

#square {
  width100pxheight100px;
  background: red;
}
2. 长方形

效果:

相关CSS代码:

#rectangle {
  width200pxheight100px;
  background: red;
}
这里推荐一下我的前端学习交流扣qun:731771211 ,里面都是学习前端的
3. 正圆

这个显然借助圆角实现,效果如下:

相关CSS代码:

#circle {
  width100pxheight100px;
  background: red;
  border-radius50%
}
4. 椭圆

效果如下:

相比正圆就是尺寸有些不一样,相关CSS代码:

#oval {
  width200pxheight100px;
  background: red;
  border-radius50%;
}
5. 向上三角

效果如下:

相关CSS代码:

#triangle-up {
  width0;
  border-left50px solid transparent;
  border-right50px solid transparent;
  border-bottom100px solid red;
}
6. 向下三角

效果如下:

相关CSS代码:

#triangle-down {
  width0;
  border-left50px solid transparent;
  border-right50px solid transparent;
  border-top100px solid red;
}
7. 方向朝左的三角

效果如下:

相关CSS代码:

#triangle-left {
  width0;
  border-top50px solid transparent;
  border-right100px solid red;
  border-bottom50px solid transparent;
}
8. 方向朝右的三角

效果如下:

相关CSS代码:

#triangle-right {
  width0;
  border-top50px solid transparent;
  border-left100px solid red;
  border-bottom50px solid transparent;
}
这里推荐一下我的前端学习交流扣qun:731771211 ,里面都是学习前端的
9. 左上三角

效果如下:

相关CSS代码:

#triangle-topleft {
  width0;
  border-top100px solid red;
  border-right100px solid transparent;
}

也可以使用一个45°的斜向线性渐变实现,不过兼容性没有使用border实现好。

10. 右上三角

效果如下:

相关CSS代码:

#triangle-topright {
  width0;
  border-top100px solid red;
  border-left100px solid transparent;
}

也可以使用一个45°的斜向线性渐变实现,不过兼容性没有使用border实现好。

11. 左下角三角图形

效果如下:

相关CSS代码:

#triangle-bottomleft {
  width0;
  height0;
  border-bottom100px solid red;
  border-right100px solid transparent;
}

也可以使用渐变绘制。

12. 右下角三角图形

效果如下:

相关CSS代码:

#triangle-bottomright {
  width0;
  height0;
  border-bottom100px solid red;
  border-left100px solid transparent;
}

也可以使用渐变绘制。

13. 弧形尾箭头

效果如下:

完整CSS代码:

#curvedarrow {
  position: relative;
  width0;
  border-top90px solid transparent;
  border-right90px solid red;
  transformrotate(10degtranslateX(100%);
}
#curvedarrow:after {
  content"";
  position: absolute;
  border0 solid transparent;
  border-top30px solid red;
  border-radius200px 0 0 0;
  top: -120pxleft: -90px;
  width120pxheight120px;
  transformrotate(45deg);
}

实际使用用不到这么大的图形,只要把CSS中所有的数值(不包括旋转)全部都缩小10倍就好了。

14. 梯形

效果如下:

相关CSS代码:

#trapezoid {
  border-bottom100px solid red;
  border-left25px solid transparent;
  border-right25px solid transparent;
  width100px;
}
这里推荐一下我的前端学习交流扣qun:731771211 ,里面都是学习前端的
15. 等边四边形

页面渲染效果如下:

可以借助transform属性的skew斜切实现:

#parallelogram {
  width150px;
  height100px;
  transformskew(20deg);
  background: red;
}
16. 六角星

效果如下:

相关CSS代码:

#star-six {
  width0;
  border-left50px solid transparent;
  border-right50px solid transparent;
  border-bottom100px solid red;
  position: relative;
}
#star-six:after {
  border-left50px solid transparent;
  border-right50px solid transparent;
  border-top100px solid red;
  position: absolute;
  content"";
  top30pxleft: -50px;
}
17. 五角星

效果如下:

五角星实现难度要比六角形大得多,CSS代码为:

#star-five {
  margin50px 0;
  position: relative;
  color: red;
  width0px;
  border-right100px solid transparent;
  border-bottom70px solid red;
  border-left100px solid transparent;
  transformrotate(35deg);
}
#star-five:before {
  border-bottom80px solid red;
  border-left30px solid transparent;
  border-right30px solid transparent;
  position: absolute;
  top: -45pxleft: -65px;
  content'';
  transformrotate(-35deg);
}
#star-five:after {
  position: absolute;
  color: red;
  top3pxleft: -105px;
  border-right100px solid transparent;
  border-bottom70px solid red;
  border-left100px solid transparent;
  transformrotate(-70deg);
  content'';
}
18. 多边形-五边形

效果如下:

相关CSS代码:

#pentagon {
  position: relative;
  width54px;
  box-sizing: content-box;
  border-width50px 18px 0;
  border-style: solid;
  border-color: red transparent;
}
#pentagon:before {
  content"";
  position: absolute;
  top: -85pxleft: -18px;
  border-width0 45px 35px;
  border-style: solid;
  border-color: transparent transparent red;
}

当然,最容易理解的实现方法是使用clip-path

19. 多边形-六边形

效果如下:

相关CSS代码:

#hexagon {
  width100pxheight55px;
  background: red;
  position: relative;
}
#hexagon:before {
  content"";
  position: absolute;
  top: -25pxleft0;
  border-left50px solid transparent;
  border-right50px solid transparent;
  border-bottom25px solid red;
}
#hexagon:after {
  content"";
  position: absolute;
  bottom: -25pxleft0;
  border-left50px solid transparent;
  border-right50px solid transparent;
  border-top25px solid red;
}
20. 多边形-八边形

效果如下:

相关CSS代码:

#octagon {
  width100px;
  height100px;
  background: red;
  position: relative;
}
#octagon:before {
  content"";
  width100px;
  position: absolute;
  top0left0;
  border-bottom29px solid red;
  border-left29px solid #fff;
  border-right29px solid #fff;
  box-sizing: border-box;
}
#octagon:after {
  content"";
  width100px;
  position: absolute;
  bottom0left0;
  border-top29px solid red;
  border-left29px solid #fff;
  border-right29px solid #fff;
  box-sizing: border-box;
}
21. 爱心❥

实现后的爱心效果:

相关CSS代码:

#heart {
  position: relative;
  width100pxheight90px;
}
#heart:before,
#heart:after {
  position: absolute;
  content"";
  left50pxtop0;
  width50pxheight80px;
  background: red;
  border-radius50px 50px 0 0;
  transformrotate(-45deg);
  transform-origin0 100%;
}
#heart:after {
  left0;
  transformrotate(45deg);
  transform-origin100% 100%;
}
这里推荐一下我的前端学习交流扣qun:731771211 ,里面都是学习前端的

此效果作者是 Nicolas Gallagher .

22. 无限

效果如下:

相关CSS代码:

#infinity {
  position: relative;
  width212pxheight100px;
  box-sizing: content-box;
}
#infinity:before,
#infinity:after {
  content"";
  box-sizing: content-box;
  position: absolute;
  top0left0;
  width60pxheight60px;
  border20px solid red;
  border-radius50px 50px 0 50px;
  transformrotate(-45deg);
}
#infinity:after {
  left: auto; right0;
  border-radius50px 50px 50px 0;
  transformrotate(45deg);
}
23. 菱形方块

效果如下:

相关CSS代码:

#diamond {
  width0;
  height0;
  border50px solid transparent;
  border-bottom-color: red;
  position: relative;
  top: -50px;
}
#diamond:after {
  content'';
  position: absolute;
  left: -50px;
  top50px;
  width0;
  height0;
  border50px solid transparent;
  border-top-color: red;
}
24. 菱形盾构

效果如下:

相关CSS代码:

#diamond-shield {
  width0;
  border50px solid transparent;
  border-bottom20px solid red;
  position: relative;
  top: -50px;
}
#diamond-shield:after {
  content'';
  position: absolute;
  left: -50pxtop20px;
  border50px solid transparent;
  border-top70px solid red;
}
25. 方块菱形-窄

效果如下:

相关CSS代码:

#diamond-narrow {
  width0;
  border50px solid transparent;
  border-bottom70px solid red;
  position: relative;
  top: -50px;
}
#diamond-narrow:after {
  content'';
  position: absolute;
  left: -50pxtop70px;
  border50px solid transparent;
  border-top70px solid red;
}

此效果作者是 Joseph Silber .

26. 切割菱形-钻石般

学会了这个,求婚时候直接写几行CSS送给女朋友就好了,可以省很多钱。

值钱的CSS代码:

#cut-diamond {
  border-style: solid;
  border-color: transparent transparent red transparent;
  border-width0 25px 25px 25px;
  width50px;
  box-sizing: content-box;
  position: relative;
  margin20px 0 50px 0;
}
#cut-diamond:after {
  content"";
  position: absolute;
  top25pxleft: -25px;
  border-style: solid;
  border-color: red transparent transparent transparent;
  border-width70px 50px 0 50px;
}
27. 鸡蛋形状

效果不错:

实现也很简单,就一个border-radius就可以了:

#egg {
  display: block;
  width126px;
  height180px;
  background-color: red;
  border-radius50% 50% 50% 50% / 60% 60% 40% 40%;
}
28. 吃豆人

效果如下:

相关CSS代码:

#pacman {
  width0pxheight0px;
  border-right60px solid transparent;
  border-top60px solid red;
  border-left60px solid red;
  border-bottom60px solid red;
  border-top-left-radius60px;
  border-top-right-radius60px;
  border-bottom-left-radius60px;
  border-bottom-right-radius60px;
}
29. 带尖角的说话泡泡

效果如下:

相关CSS代码:

#talkbubble {
  width120pxheight80px;
  background: red;
  position: relative;
  border-radius10px;
}
#talkbubble:before {
  content"";
  position: absolute;
  right100%top26px;
  border-top13px solid transparent;
  border-right26px solid red;
  border-bottom13px solid transparent;
}
30. 十二星

效果如下:

相关CSS代码:

#burst-12 {
  background: red;
  width80px;
  height80px;
  position: relative;
  text-align: center;
}
#burst-12:before,
#burst-12:after {
  content"";
  position: absolute;
  top0;
  left0;
  height80px;
  width80px;
  background: red;
}
#burst-12:before {
  transformrotate(30deg);
}
#burst-12:after {
  transformrotate(60deg);
}
这里推荐一下我的前端学习交流扣qun:731771211 ,里面都是学习前端的

这个效果的作者是Alan Johnson。

31. 八角形

效果如下:

相关CSS代码:

#burst-8 {
  background: red;
  width80pxheight80px;
  position: relative;
  text-align: center;
  transformrotate(20deg);
}
#burst-8:before {
  content"";
  position: absolute;
  top0left0;
  height80pxwidth80px;
  background: red;
  transformrotate(135deg);
}
32. 阴阳八卦

效果如下:

相关CSS代码:

#yin-yang {
  width96pxheight48px;
  background#eee;
  border-color: red;
  border-style: solid;
  border-width2px 2px 50px 2px;
  border-radius100%;
  position: relative;
}
#yin-yang:before {
  content"";
  position: absolute;
  top50%left0;
  background#fff;
  border18px solid red;
  border-radius100%;
  width12pxheight12px;
}
#yin-yang:after {
  content"";
  position: absolute;
  top50%;
  left50%;
  background: red;
  border18px solid #eee;
  border-radius100%;
  width12px;
  height12px;
}
33. 徽章缎带

效果如下:

相关CSS代码:

#badge-ribbon {
  position: relative;
  background: red;
  height100pxwidth100px;
  border-radius50px;
}
#badge-ribbon::before,
#badge-ribbon::after {
  content'';
  position: absolute;
  border-bottom70px solid red;
  border-left40px solid transparent;
  border-right40px solid transparent;
  top70pxleft: -10px;
  transformrotate(-140deg);
}
#badge-ribbon::after {
  left: auto;
  right: -10px;
  transformrotate(140deg);
}
34. bilibili电视机

效果如下:

相关CSS代码:

#tv {
  position: relative;
  width200pxheight150px;
  margin20px 0;
  background: red;
  border-radius50% / 10%;
  color: white;
  text-align: center;
  text-indent: .1em;
}
#tv:before {
  content'';
  position: absolute;
  top10%bottom10%right: -5%left: -5%;
  background: inherit;
  border-radius5% / 50%;
}
35. V形线条

效果如下:

相关CSS代码:

#chevron {
  position: relative;
  text-align: center;
  padding12px;
  margin-bottom6px;
  height60pxwidth200px;
}
#chevron:before {
  content'';
  position: absolute;
  top0left0;
  height100%width51%;
  background: red;
  transformskew(0deg6deg);
}
#chevron:after {
  content'';
  position: absolute;
  top0right0;
  height100%width50%;
  background: red;
  transformskew(0deg, -6deg);
}
这里推荐一下我的前端学习交流扣qun:731771211 ,里面都是学习前端的
36. 放大镜

效果如下:

相关CSS代码:

#magnifying-glass {
  font-size10em;
  display: inline-block;
  width0.4emheight0.4em;
  border0.1em solid red;
  position: relative;
  border-radius0.35em;
}
#magnifying-glass:before {
  content"";
  display: inline-block;
  position: absolute;
  right: -0.25embottom: -0.1em;
  border-width0;
  background: red;
  width0.35emheight0.08em;
  transformrotate(45deg);
}
这里推荐一下我的前端学习交流扣qun:731771211 ,里面都是学习前端的

这个实现很不错,单位是em,实用性就很强。

37. 月儿弯弯

月儿弯弯照九州:

相关CSS代码:

#moon {
  width80pxheight80px;
  border-radius50%;
  box-shadow15px 15px 0 0 red;
}
38. 旗帜

效果如下:

相关CSS代码:

#flag {
  width110pxheight56px;
  padding-top15px;
  position: relative;
  background: red;
}
#flag:after {
  content"";
  position: absolute;
  left0bottom0;
  border-bottom13px solid #fff;
  border-left55px solid transparent;
  border-right55px solid transparent;
}
39. 圆锥体

效果如下:

依然是活用圆角border-radius属性:

#cone {
  width0height0;
  border-left70px solid transparent;
  border-right70px solid transparent;
  border-top100px solid red;
  border-radius50%;
}
40. 十字架

效果如下:

可以作为添加按钮,或者添加图标。

相关代码:

#cross {
  background: red;
  width20pxheight100px;
  position: relative;
}
#cross:after {
  background: red;
  content"";
  width100pxheight20px;
  position: absolute;
  left: -40pxtop40px;
}
41. 棒球踏板形状

效果如下:

相关CSS代码:

#base {
  background: red;
  display: inline-block;
  height55pxwidth100px;
  margin-left20px;
  margin-top55px;
  position: relative; 
}
#base:before {
  border-bottom35px solid red;
  border-left50px solid transparent;
  border-right50px solid transparent;
  content"";
  left0top: -35px;
  position: absolute;  
}
42. 长长的指向图形

效果如下:

相关CSS代码:

#pointer {
  width200pxheight40px;
  position: relative;
  background: red;
}
#pointer:after {
  content"";
  position: absolute;
  left0bottom0;
  border-left20px solid white;
  border-top20px solid transparent;
  border-bottom20px solid transparent;
}
#pointer:before {
  content"";
  position: absolute;
  right: -20pxbottom0;
  border-left20px solid red;
  border-top20px solid transparent;
  border-bottom20px solid transparent;
}
这里推荐一下我的前端学习交流扣qun:731771211 ,里面都是学习前端的
43. 锁

效果如下:

相关CSS代码:

#lock {
  font-size8px;
  position: relative;
  width18emheight13em;
  border-radius2em;
  top10em;
  box-sizing: border-box;
  border3.5em solid red;
  border-right-width7.5em;
  border-left-width7.5em;
  margin0 0 6rem 0;
}
#lock:before {
  content"";
  box-sizing: border-box;
  position: absolute;
  border2.5em solid red;
  width14emheight12em;
  left50%;
  margin-left: -7emtop: -12em;
  border-top-left-radius7em;
  border-top-right-radius7em;
}
#lock:after {
  content"";
  box-sizing: border-box;
  position: absolute;
  border1em solid red;
  width5emheight8em;
  border-radius2.5em;
  left50%top: -1em;
  margin-left: -2.5em;
}

“CSS图形绘制方法有哪些”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注亿速云网站,小编将为大家输出更多高质量的实用文章!

亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

原文链接:http://blog.itpub.net/69901074/viewspace-2645360/

css
AI

开发者交流群×