CSS3画三角形原理
1、首先看一下画出一个下三角形完整的代码及效果图
#trangle1-up{
width:0;
height:0;
border-left:50px solid transparent;
border-right:50px solid transparent;
border-bottom:100px solid blue;
}
<div id="trangle1-up">
</div>
chrome截图
2、接下来看一下原理
首先我们知道:
#test{
width: 100px;
height: 100px;
border:2px solid blue;
}
上面这段代码会产生如下的效果:
这个应该没什么疑问。
如果我们继续增大边框的宽度并且使边框各个边的颜色不一致:
#test{
width: 100px;
height: 100px;
border:20px solid;
border-color:blue black red yellow;
}
那么会产生如下效果:
那么,试想一下,如果我们把边框的宽度设置成50px呢?会产生什么效果?试验一下
#test{
width: 100px;
height: 100px;
border:50px solid;
border-color:blue black red yellow;
}
有人会问了为什么中间还有一块空白的区域呢?边框不是应该完全填充矩形区域了吗?
其实不然,这个设计到W3C的标准盒子模型的概念,可以参考一下http://www.jb51.net/css/12199.html
请仔细看图中,什么叫height?什么叫width?height和width包含边框border吗?当你明白了这些,我们再继续往下看。
。。。。。。。。。
现在我们把width和height设置为0:
#test{
width: 0px;
height: 0px;
border:50px solid;
border-color:blue black red yellow;
}
酱紫,中间就不再有空白的区域了,即content区域为0了;吃完饭回来再写。。。。
好了,到了这里,那么怎么画一个三角形呢?
#test{
width: 0px;
height: 0px;
border-left:50px solid transparent;
border-right:50px solid transparent;
border-bottom:100px solid blue;
}
酱紫咯,这不就是个三角形了吗?哈哈哈
其中,上边界是0px,所以下边界会向上延升100px,左右各50px。这是上三角,下三角也很好做:
#test{
width: 0px;
height: 0px;
border-left:50px solid transparent;
border-right:50px solid transparent;
border-top:100px solid blue;
}
左右三角就自己搞咯。。。。
最后来个合体的:
#test{
width:100px;
height:100px;
margin-left:100px;
position:absolute;
background:#F63;}
#test-up{
width: 0px;
height: 0px;
margin-left:100px;
border-left:50px solid transparent;
border-right:50px solid transparent;
border-bottom:100px solid blue;
}
#test-right{
width: 0px;
height: 0px;
position:absolute;
margin-left:200px;
border-left:100px solid blue;
border-bottom:50px solid transparent;
border-top:50px solid transparent;
}
#test-bottom{
width: 0px;
height: 0px;
position:absolute;
margin-left:100px;
margin-top:100px;
border-left:50px solid transparent;
border-right:50px solid transparent;
border-top:100px solid blue;
}
#test-left{
width: 0px;
height: 0px;
border-bottom:50px solid transparent;
border-right:100px solid blue;
border-top:50px solid transparent;
}
<div id="test-up">
</div>
<div id="test">
</div> <div id="test-right">
</div> <div id="test-bottom">
</div> <div id="test-left">
</div>
接下来再上一个图形及其CSS代码:
图形长这样:
怎么实现?答案是需要借助伪类,使用方法是在一个黑色的右三角形上叠加一个全白的稍小一点的右三角形,叠加的顺序就得通过after和before伪类来控制,
注意代码中的after和before伪类中的颜色不可弄错,否则右三角形会是全黑的,代码如下:
#demo {
width: 100px;
height: 100px;
border: 2px #000 solid;
background-color: #fff;
position: relative;
} #demo:before {
content: ""; /*这一行必须有,否则这个特效不会显示*/
border-top: 12px transparent solid;
border-bottom: 12px transparent solid;
border-left: 12px #000 solid; /*纯黑色*/
position: absolute;
/*right: -12px;*/
margin-left:102px;
margin-top: 18px;
} #demo:after {
content: "";
border-top: 12px transparent solid;
border-bottom: 12px transparent solid;
border-left: 12px #fff solid; /*纯白色*/
position: absolute;
/*right: -10px;*/
margin-left:100px;
margin-top:18px;
/* top: 20px;*/
}
<div id="demo">
</div>
CSS3画三角形原理的更多相关文章
- css3画三角形的原理
以前用过css3画过下拉菜单里文字后面的“下拉三角符号”,类似于下面这张图片文字后面三角符号的效果 下面是一个很简单的向上的三角形代码 #triangle-up { width: 0; height: ...
- css画三角形原理解析
<div id="div1"></div><div id="div2"></div><div id=&qu ...
- css3 画三角形
/*箭头向上*/ .arrow-up { width:0; height:0; border-left:20px solid transparent; border-right:20px solid ...
- 纯CSS3画出小黄人并实现动画效果
前言 前两天我刚发布了一篇CSS3实现小黄人动画的博客,但是实现的CSS3动画是基于我在站酷网找到的一张小黄人的jpg格式图片,并自己用PS抠出需要实现动画的部分,最后才完成的动画效果.但是,其实我的 ...
- 菱形实现气泡Bubble,菱形画箭头,菱形画三角形
菱形实现气泡Bubble,菱形画箭头,菱形画三角形 >>>>>>>>>>>>>>>>>>&g ...
- CSS画三角形引发的一些思考
今天刷知乎时看到了一个问题,有谁能详细讲一下css如何画出一个三角形?怎么想都想不懂? - 知乎.很巧,刚入前端坑的我前不久也遇到过这个问题,今天再来谈一谈这个问题则是因为知乎的一些答案引发了我的 ...
- 使用CSS3画出一个叮当猫
刚学习了这个案例,然后觉得比较好玩,就练习了一下.然后发现其实也不难,如果你经常使用PS或者Flash的话,应该就会知道画个叮当猫是很容易的事,至少我是这么觉得.但是,用CSS3画出来确实是第一次接触 ...
- css伪元素before/after和画三角形的搭配应用
想要实现的效果如下: 第一步:如何用css画出三角形? /* css画三角形 */ .sanjiao{ ; border-top:40px solid red; border-bottom:40px ...
- 如何使用CSS3画出一个叮当猫
刚学习了这个案例,然后觉得比较好玩,就练习了一下.然后发现其实也不难,如果你经常使用PS或者Flash的话,应该就会知道画个叮当猫是很容易 的事,至少我是这么觉得.但是,用CSS3画出来确实是第一次接 ...
随机推荐
- scrollLeft,scrollWidth,clientWidth,offsetWidth 可实现导航栏固定不动(冻结)的效果
HTML精确定位:scrollLeft,scrollWidth,clientWidth,offsetWidth scrollHeight: 获取对象的滚动高度. scrollLeft:设置或获取位 ...
- 关于Spring中AOP的理解
AOP简介[理解][重点] 1.AOP(Aspect Oriented Programing)面向切面/方面编程 2.AOP隶属软件工程的范畴,指导开发人员如何制作开发软件,进行结构设计 3.AOP联 ...
- oracle中的 exists 和 in 的效率问题
1) select * from T1 where exists(select * from T2 where T1.a=T2.a) ; T1数据量小而T2数据量非常大时,T1<<T2 时 ...
- centos find
首先你要确定你的软件是什么方式安装?如果不确定,你可知道你的软件名字,用find查找一下在哪个目录find / -name softname
- hihocoder 1186
1186 : Coordinates 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 Give you two integers P and Q. Let all div ...
- android之模拟器访问外网设置
一.确定adb可用 1.1 确认adb环境变量 1.2 命令行cmd 执行adb shell启动模拟器的命令行 二.DNS确认 2.1 确定DNS是否为电脑的DNS 2.2 修改DNS地址 三.效果图
- MySQL flush privileges 명령어
INSERT나 UPDATE, DELETE문을 이용해서 MySQL의 사용자를 추가,삭제하거나, 사용자 권한 등을 변경하였을 때, MySQL에 변경사항을 적용하기 위해서 사용하는 명령 ...
- Microsoft JScript 运行时错误: Sys.WebForms.PageRequestManagerParserErrorException无法分析从服务器收到的消息。之所以出现此错误,
Microsoft JScript 运行时错误: Sys.WebForms.PageRequestManagerParserErrorException: 无法分析从服务器收到的消息.之所以出现此错误 ...
- jenkins离线安装git插件
jenkins没有默认安装git,当jenkins无法连接外网的话,安装git插件就是一件很麻烦的事,需要自己去下载插件: 往下拉 这边的插件就是需要自己去下载了,在bing下搜索jenkins gi ...
- JavaScript高级程序设计(三):基本概念:数据类型
特别注意:ECMAScript是区分大小写的. 一.变量 1.ECMAScript的变量是松散型的.所谓松散型就是可以用来保存任何类型的数据.即每个变量仅仅是一个用于保存值的占位符而已.定义变量时要使 ...