CSS三角箭头应用实践
用CSS来制作无图片带箭头的DIV方框(此代码比较适合追求纯代码者)
这类效果也可以用图片背景来实现,代码会更简洁
CSS代码:
- div.container{position:absolute;
- top:30px;
- left:40px;
- font-size: 9pt;
- display:block;
- height:100px;
- width:200px;
- background-color:transparent;
- *border:1px solid #666;
- }
- s{
- position:absolute;
- top:-20px;
- *top:-22px;
- left:20px;
- display:block;
- height:0;
- width:0;
- font-size: 0;
- line-height: 0;
- border-color:transparent transparent #666 transparent;
- border-style:dashed dashed solid dashed;
- border-width:10px;
- }
- i{position:absolute;
- top:-9px;
- *top:-9px;
- left:-10px;
- display:block;
- height:0;
- width:0;
- font-size: 0;
- line-height: 0;
- border-color:transparent transparent #fff transparent;
- border-style:dashed dashed solid dashed;
- border-width:10px;
- }
- .content{
- border:1px solid #666;
- -moz-border-radius:3px;
- -webkit-border-radius:3px;
- position:absolute;
- background-color:#fff;
- width:100%;
- height:100%;
- padding:5px;
- *top:-2px;
- *border-top:1px solid #666;
- *border-top:1px solid #666;
- *border-left:none;
- *border-right:none;
- *height:102px;
- box-shadow: 3px 3px 4px #999;
- -moz-box-shadow: 3px 3px 4px #999;
- -webkit-box-shadow: 3px 3px 4px #999;
- /* For IE 5.5 - 7 */
- filter: progid:DXImageTransform.Microsoft.Shadow(Strength=4, Direction=135, Color='#999999');
- /* For IE 8 */
- -ms-filter: "progid:DXImageTransform.Microsoft.Shadow(Strength=4, Direction=135, Color='#999999')";
- }
HTML代码:
- <div class="container">
- <div class="content">
- <br>这是框中的文字,可动态显示。高度自动增加,应该不错吧^_^
- </div>
- <s>
- <i></i>
- </s>
- </div>
效果图:
这次介绍下CSS制作三角箭头
通常,我们做上图那个三角形,一般都是做张图,而且需要两张,因为一般都是下拉菜单的效果,需要有个hover的样式,箭头是反的。那是不是有更好的办法呢,毕竟要用两张图片来解决这么一个小问题太浪费资源了,于是,下面我要用纯CSS的方法来解决这一问题,用到的只需css的一个属性,就是border-width
我们先来看个样式,如果设置元素边框,会怎么样:
似乎看不出什么,让我给四个边框加上不同的颜色吧再看看吧:
是不是发现了些什么?对,让我们把中间的文字去掉吧:
这样,就出现4个三角形了,然后我们如果需要顶部那个三角形,只需要将border的left、right、bottom设置成背景色就行了:
这样,我们需要的三角形就出现了,并且可以设置4个不同方向的了:
样式代码很简单,就几句话:
- float: left;
- border-style: solid; border-width: 10px;
- border-color: #000 #ccc #ccc #ccc;
- height: 0;
- width: 0;
- font-size: 0;
实际应用
比如我们要使用向下的箭头(兼容IE6写法)
HTML代码:
- <div class="demo"><span class="bottom-arrow"></span></div>
CSS代码:
- .demo{position:relative}
- .bottombottom-arrow{
- position:absolute;
- top:10px;
- left:0px;
- border-style:solid;
- border-width:100px;
- border-color:#000000 transparent transparent transparent;/*上边框设置想要的颜色*/
- height:0;
- width:0;
- font-size:0;
- _border-color:#000000 tomato tomato tomato ; /*边框透明色 For IE6-*/
- _filter:chroma(color=tomato);/*边框透明色 For IE6-*/
- }
说明:
这里的演示是用了边框透明色,如果你用不到透明的话,直接设置border-color为你需要的颜色就可以了,免去为了兼容IE6的透明边框滤镜写法
Chroma属性可以设置一个对象中指定的颜色为透明色,它的表达式如下:
- Filter:Chroma(color=color)
这个属性的表达式是不是很简单,它只有一个参数。只需把您想要指定透明的颜色用Color参数设置出来就可以了。
相关文章
- 本篇文章主要介绍了CSS3 用border写 空心三角箭头 (两种写法),非常具有实用价值,需要的朋友可以参考下2017-09-29
- 本文给大家介绍css代码去掉select的下拉箭头样式的方法,代码简单易懂,需要的朋友可以参考下2016-09-02
- 这篇文章主要为大家详细介绍了纯CSS绘制三角形箭头图案技术,如何使用:before和:after绘制CSS三角形,感兴趣的小伙伴们可以参考一下2016-06-28
- css3功能非常强大,之前需要图片完成的icon,现在我们只需要几段css代码就可以实现此功能。下面给大家分享纯css制作的圆,椭圆,三角形箭头图标,非常使用,需要的朋友参考2016-03-30
- 这篇文章主要为大家介绍了仿新浪微博箭头的css写法,涉及字体与样式的实现方法,是非常简单实用的css技巧,需要的朋友可以参考下2014-12-13
- 最近我想修改一下这个网站,我想在上面放置一个提示框。这是很容易,但我想让提示框上有一个三角形的箭头。可是,一想到这需要使用图片,并且各种颜色,各种方向的箭头要准2014-05-07
- 准备添加tooltips提示信息效果.实现很容易,但我想要让提示功能具有三角形的指示图标,本文两种实现方式: 使用或不使用 before 和 :after 伪元素,示例如下,有此需求的朋2013-08-09
- 之前做箭头一直都是用图片,也尝试过用新浪微博用的那种特殊符号“◆”,不过宽高,三角形的大小等都不太好控制,后来发现可以直接用css绘制,非css3,使用常规的border进2012-06-27
- CSS模拟小箭头的实现代码,需要的朋友可以参考下。2012-01-21
- 这篇文章主要介绍了使用css实现任意大小、任意方向和任意角度的箭头示例的相关资料,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧2018-03-05
最新评论