究竟该用字体图标、图片图标、还是CSS画一个图标?我也不知道。各有千秋吧。本文将介绍如何用css绘制简单的图形,所有测试在chrome58.0完成,如果你不能得到正确结果请到caniuse查一查看看是不是需要什么前缀。

一、基础

<!DOCTYPE html>
<html>
<head>
<title>basic shapes</title>
<style type="text/css">
div{
box-sizing: border-box;
}
.div1{
width: 100px;
height: 100px;
border-top: 50px solid red;
border-right: 50px solid blue;
border-bottom: 50px solid yellow;
border-left: 50px solid green;
} .div2{
width: 100px;
height: 100px;
border-right: 50px solid blue;
border-bottom: 100px solid yellow;
border-left: 50px solid green;
}
.div3{
width: 100px;
height: 100px;
border-right: 25px solid blue;
border-bottom: 100px solid yellow;
border-left: 25px solid green;
}
</style>
</head>
<body>
<div class="div1">div1</div>
<span>border的神奇之处</span>
<hr>
<div class="div2">div2</div>
<span>黄色三角形,底边100px高100px</span>
<hr>
<div class="div3">div3</div>
<span>黄色的等腰梯形,上底50,下底100,高100</span>
</body>
</html>

  上面代码的效果是这样的

  

  border是如何工作的,你可以从上面的结果自行体会。

二、特殊三角形

  

<!DOCTYPE html>
<html>
<head>
<title>三角形</title>
<style type="text/css">
div{
box-sizing: border-box;
margin:20px;
}
.container::after{
content: "";
display: block;
clear: both;
}
.div1{
width: 100px;
height: 0;
border-bottom: 100px solid red;
border-left: 50px solid transparent;
border-right: 50px solid transparent;
float: left;
}
.div2{
width: 0;
height: 100px;
border-bottom: 50px solid transparent;
border-left: 100px solid blue;
border-top: 50px solid transparent;
float: left;
}
.div3{
width: 100px;
height: 0;
border-top: 100px solid yellow;
border-left: 50px solid transparent;
border-right: 50px solid transparent;
float: left;
}
.div4{
width: 0;
height: 100px;
border-bottom: 50px solid transparent;
border-right: 100px solid green;
border-top: 50px solid transparent;
float: left;
}
.div5{
width: 100px;
height: 0;
border-bottom: 86.6px solid red;
border-left: 50px solid transparent;
border-right: 50px solid transparent;
float: left;
}
.div6{
width: 100px;
height: 0;
border-bottom: 100px solid blue;
/*border-left: 50px solid transparent;*/
border-right: 100px solid transparent;
float: left;
}
.div7{
width: 100px;
height: 0;
border-bottom: 100px solid blue;
/*border-left: 50px solid transparent;*/
border-right: 100px solid transparent;
float: left;
transform: rotate(135deg);
transform-origin: 50% 50%;
}
</style>
</head>
<body>
<div class="container">
<h4>等腰三角形</h4>
<div class="div1"></div>
<div class="div2"></div>
<div class="div3"></div>
<div class="div4"></div>
</div>
<div class="container">
<h4>其他特殊三角形</h4>
<div class="div5">等边</div>
<div class="div6">直角</div>
<div class="div7">直角</div>
</div>
</body>
</html>

  

  等腰三角形我做了4个方向的,当然你也可以用旋转来做,就像div7那样。制作三角形比较简单,只要对我前面将的基础加以改造就可以了。

三、特殊四边形

  

<!DOCTYPE html>
<html>
<head>
<title>四边形</title>
<style type="text/css">
div{
box-sizing: border-box;
margin:20px;
}
.container::after{
content: "";
display: block;
clear: both;
}
.div1{
background: transparent;
width: 0;
height: 100px;
border-bottom: 50px solid transparent;
border-right: 100px solid green;
border-top: 50px solid transparent;
float: left;
position: relative;
box-sizing: border-box; }
.div1::after{
content: "";
position: absolute;
top: -50px;
left: 100px;
background: transparent;
width: 0;
height: 100px;
border-bottom: 50px solid transparent;
border-left: 100px solid green;
border-top: 50px solid transparent;
float: left;
position: relative;
box-sizing: border-box;
} .div2{
background: transparent;
width: 80px;
height: 0;
border-bottom: 50px solid green;
border-right: 40px solid transparent;
border-left: 40px solid transparent;
float: left;
position: relative;
box-sizing: border-box;
margin-left: 100px;
}
.div2::after{
content: "";
position: absolute;
top: 50px;
left: -40px;
width: 80px;
height: 0;
border-top: 50px solid green;
border-right: 40px solid transparent;
border-left: 40px solid transparent;
box-sizing: border-box;
}
.div5{
width: 100px;
height: 0;
border-bottom: 100px solid green;
border-left: 25px solid transparent;
border-right: 25px solid transparent;
float: left;
}
.div6{
width: 100px;
height: 0;
border-bottom: 100px solid green;
border-right: 40px solid transparent;
float: left;
}
.div7{
width: 100px;
height: 100px;
background: green;
transform: skew(30deg,0);
float: left;
}
</style>
</head>
<body>
<div class="container">
<h4>菱形</h4>
<div class="div1"></div>
<div class="div2"></div>
</div>
<div class="container">
<h4>梯形和平行四边形</h4>
<div class="div5">等腰</div>
<div class="div6">直角</div>
<div class="div7">平行四边形</div>
</div>
</body>
</html>

四、圆和椭圆

  

<!DOCTYPE html>
<html>
<head>
<title>圆和椭圆</title>
<style type="text/css">
.div1{
width: 50px;
height: 50px;
border-radius: 25px;
box-sizing: border-box;
background: red;
float: left;
}
.div2 {
width: 100px;
height: 80px;
background: green;
border-radius: 50px / 40px;
float: left;
}
</style>
</head>
<body>
<div class="div1"></div>
<div class="div2"></div>
</body>
</html>

  科普一下:http://www.css88.com/book/css/properties/border/border-radius.htm

  以前都只用一个参参数的,所以写博客可以帮助查漏补缺。

五、复杂一点的图形

  1、正多边形

  

<!DOCTYPE html>
<html>
<head>
<title>正多边形</title>
<style type="text/css">
.five {
top: 50px;
position: relative;
width: 54px;
border-width: 50px 18px 0;
border-style: solid;
border-color: red transparent;
}
.five::before {
content: "";
position: absolute;
height: 0;
width: 0;
top: -85px;
left: -18px;
border-width: 0 45px 35px;
border-style: solid;
border-color: transparent transparent green;
}
.six{
width: 100px;
height: 0px;
border-width:0px 18.59px 60px;
border-style: solid;
border-color: transparent transparent red;
margin-top: 60px;
position: relative;
}
.six::after{
content: "";
width: 100px;
height: 0px;
border-width:60px 18.59px 0px;
border-style: solid;
border-color: green transparent transparent;
position: absolute;
top: 60px;
left: -18.59px;
}
</style>
</head>
<body>
<div class="five"></div>
<div class="six"></div>
</body>
</html>

  

  正五边形是借鉴的大神的(不知道是不是正的,我没算),思路是做一个梯形,然后拼接一个三角形,正六边形是我根据他的思路用两个梯形拼接的,如果我没有计算错误这应该是一个正六边形(怎么有点不像呢),计算目的是保证,腰和短底长相等。我刻意用不同颜色表示了拆分情况,便于理解。正 八变形怎么做?只需要拆解成两个梯形和一个矩形就好做了。正n边形怎么做,也是把它看作基本图形的组合,只不过可能单靠两个伪元素已经不够用了。

  2、大大的爱心送给你

  

.aixin{
border-width: 200px 150px 0;
border-style: solid;;
border-color: red transparent;
width: 0px;
position: relative;
top: 200px;
left: 100px;
}
.aixin::before{
content: "";
width: 180px;
height: 180px;
display: block;
border-radius: 90px;
background: red;
position: absolute;
top: -357px;
left: -180px;
}
.aixin:after{
content: "";
width: 180px;
height: 180px;
display: block;
border-radius: 90px;
background: red;
position: absolute;
top: -357px;
left:;
}

  数据没算,调整出来的。人要实心啊,搞个空心太不像话了,你自己去弄吧。两个圈圈加个三角形就可以了,原来爱这么简单。    

  3、对话框

  

<!DOCTYPE html>
<html>
<head>
<title>对话框</title>
<style type="text/css">
.div1{
width: 300px;
height: 100px;
border: 1px solid #97FFFF;
position: relative;
left: 100px;
border-radius: 10px;
}
.div1::after,.div1::before{
content: ""; display: block;
width: 0;
height: 0;
border-style: solid;
border-color:transparent #97FFFF transparent transparent;
position: absolute;
top: 30px;
left: -50px;
}
.div1::after{
border-width: 21px 50px 21px 0;
}
.div1::before{
top: 31px;
border-width: 20px 50px 20px 0;
border-color: transparent white transparent transparent;
z-index: 1000;
left: -49px;
} .div2{
width: 300px;
height: 100px;
border: 1px solid #97FFFF;
position: relative;
left: 100px;
border-radius: 10px;
margin-top: 20px;
}
.div2::after,.div2::before{
content: ""; display: block;
width: 0;
height: 0;
border-style: solid;
border-color:transparent transparent transparent #97FFFF;
position: absolute;
top: 30px;
left: 300px;
}
.div2::after{
border-width: 21px 0px 21px 50px;
}
.div2::before{
top: 31px;
border-width: 20px 0px 20px 50px;
border-color: transparent transparent transparent white;
z-index: 1000;
left: 299px;
} </style>
</head>
<body>
<div class="div1">
</div> <div class="div2"></div> </body>
</html>

  

六、小结一下

  用CSS还是画出来很多东西的,对于稍微复杂点的图形用拼接或者裁剪的方法实现,有时可能会需要一些简单计算。本文所提到的只是九牛一毛,早有一些朋友做出了许多非常炫酷的图形,你可以去搜搜看。就写到这里,留几个大神的作品,以备不时只需。

参考:

CSS绘制简单图形的更多相关文章

  1. 学习笔记:HTML5 Canvas绘制简单图形

    HTML5 Canvas绘制简单图形 1.添加Canvas标签,添加id供js操作. <canvas id="mycanvas" height="700" ...

  2. 摘记 史上最强大的40多个纯CSS绘制的图形(一)

    今天在国外的网站上看到了很多看似简单却又非常强大的纯CSS绘制的图形,里面有最简单的矩形.圆形和三角形,也有各种常见的多边形,甚至是阴阳太极和网站小图标,真的非常强大,分享给大家. Square(正方 ...

  3. 40多个纯CSS绘制的图形

    本文由码农网 – 陈少华原创,转载请看清文末的转载要求. 今天在国外的网站上看到了很多看似简单却又非常强大的纯CSS绘制的图形,里面有最简单的矩形.圆形和三角形,也有各种常见的多边形,甚至是阴阳太极和 ...

  4. CSS 魔法系列:纯 CSS 绘制各种图形《系列六》

    我们的网页因为 CSS 而呈现千变万化的风格.这一看似简单的样式语言在使用中非常灵活,只要你发挥创意就能实现很多比人想象不到的效果.特别是随着 CSS3 的广泛使用,更多新奇的 CSS 作品涌现出来. ...

  5. CSS 魔法系列:纯 CSS 绘制各种图形《系列五》

    我们的网页因为 CSS 而呈现千变万化的风格.这一看似简单的样式语言在使用中非常灵活,只要你发挥创意就能实现很多比人想象不到的效果.特别是随着 CSS3 的广泛使用,更多新奇的 CSS 作品涌现出来. ...

  6. CSS 魔法系列:纯 CSS 绘制基本图形(圆、椭圆等)

    我们的网页因为 CSS 而呈现千变万化的风格.这一看似简单的样式语言在使用中非常灵活,只要你发挥创意就能实现很多比人想象不到的效果.特别是随着 CSS3 的广泛使用,更多新奇的 CSS 作品涌现出来. ...

  7. 史上最强大的40多个纯CSS绘制的图形[转]

    今天在国外的网站上看到了很多看似简单却又非常强大的纯CSS绘制的图形,里面有最简单的矩形.圆形和三角形,也有各种常见的多边形,甚至是阴阳太极和网站小图标,真的非常强大,分享给大家. Square(正方 ...

  8. Java入门:绘制简单图形

    在上一节,我们学习了如何使用swing和awt工具创建一个空的窗口,本节学习如何绘制简单图形. 基本绘图介绍 Java中绘制基本图形,可以使用Java类库中的Graphics类,此类位于java.aw ...

  9. 纯CSS绘制的图形一览

    整理网上一些使用纯CSS绘制的图形示例~~纯属抄袭,哈哈...仅仅是为了自己以后查看! Square(正方形) #square { width: 100px; height: 100px; backg ...

随机推荐

  1. MySQL 5.6 从库开启 crash-safe 功能

    原文:Enabling crash-safe slaves with MySQL 5.6 可以对从库进行配置 crash-safe 功能是 MySQL 5.6 关于复制的一个重大改进.然而,我们注意到 ...

  2. 【WCF】错误处理(二):错误码―—FaultCode

    先来说说SOAP消息中错误消息的包装结构,一条SOAP错误消息的大致形式如下: <s:Fault> <faultcode xmlns:a="me-cust-error&qu ...

  3. QQ好友在线/离线,怎么测试?

    即时通讯是目前internet上最为流行的通讯方式,各种各样的即时通讯软件也层出不穷,那么今天主要针对QQ好友在线状态/QQ群友在线状态功能出发,一起思考其中的实现原理以及我们如何去测试此功能? 当大 ...

  4. 'utf8' codec can't decode byte 0xd1 in position 931: invalid continuation byte解决方法

    有时候,我得到这样的字符œ导致的UnicodeDecodeError错误. 我需要能够使串的UTF-8有或没有这些字符. 在工作中,经常遇到,读取一个文件,或者是从网页获取一个问题,明明看着是gb23 ...

  5. Ext JS 6学习文档–第1章–ExtJS入门指南

    Ext JS 入门指南 前言 本来我是打算自己写一个系列的 ExtJS 6 学习笔记的,因为 ExtJS 6 目前的中文学习资料还很少.google 搜索资料时找到了一本国外牛人写的关于 ExtJS ...

  6. 解读Laravel,看PHP如何实现Facade?

    刚刚开始学Laravel就会接触到路由 Route::get('/', function () { return view('welcome'); }); 后来笔者一本正经的去读过Route类的代码, ...

  7. rpm包相关操作

    1.查找已安装的rpm:rpm -qa|grep ewp2.卸载已安装的rpm: 先切换到虚拟机共享路径,执行卸载命令: rpm -e 已安装rpm包名称3.安装新rpm包:rpm -ivh(更新的话 ...

  8. 更改服务器的SID 加入域控制器提示SID重复

    启动Windows2008.2012进入系统后,打开“CMD窗口”并进入到"C:\windows\system32\sysprep"目录后再输入“sysprep /generali ...

  9. 解决MVC中JsonResult返回 弹出文件下载对话框

    设置一下返回类型为HTML TEXT就可以了 JsonResult json = Json(xxx, JsonRequestBehavior.DenyGet); json.ContentType = ...

  10. 基于Intranet的零件库管理信息系统设计--part02

    昨天建了第一个子表,今天继续. 按照这个一个一个来: 轴承参数查询如下(来源:轴承查询型号网) 照这个来大概就是这么几个属性: 轴承主键,轴承名称,新型号,旧型号,内径,外径,宽度,Cr,Cor(话说 ...