CSS3实现三角形

<div class="arrow-up">
     <!--向上的三角-->
</div>
<div class="arrow-down">
    <!--向下的三角-->
</div>
<div class="arrow-left">
    <!--向左的三角-->
</div>
<div class="arrow-right">
    <!--向右的三角-->
</div>

下面用CSS3分别实现向上、下、左、右的三角形

/*箭头向上*/
.arrow-up {
    width:0;
    height:0;
    border-left:30px solid transparent;
    border-right:30px solid transparent;
    border-bottom:30px solid #fff;
}

 /*箭头向下*/
.arrow-down {
    width:0;
    height:0;
    border-left:20px solid transparent;
    border-right:20px solid transparent;
    border-top:20px solid #0066cc;
}
/*箭头向左*/
.arrow-left {
    width:0;
    height:0;
    border-top:30px solid transparent;
    border-bottom:30px solid transparent;
    border-right:30px solid yellow;
}

/*箭头向右*/
.arrow-right {
    width:0;
    height:0;
    border-top:50px solid transparent;
    border-bottom: 50px solid transparent;
    border-left: 50px solid green;
}

好了原理我们了解了,那么我们就实战一下吧,来实现带小三角形的div框。

首先,写出html代码:

<div class="entry">
    <div class="entry-trangle"><!--本Div只来实现三角形,无其他作用--></div>
    hello,我出生了<br/>
    hello,我出生了<br/>
    hello,我出生了<br/>
    hello,我出生了<br/>
</div>

挂载有类"entry-trangle"的div只用来实现小三角形。对这个div用css3的transparent实现三角形,然后绝对定位、设置z-index:-1;、margin-left:-19px;,看下面css代码:

<style type="text/css">
    *{margin:0;padding:0;}
    body{
        background:#666;
        font:14px/20px "Microsoft YaHei";
    }
    .entry{
        margin:0 auto;
        margin-top:20px;
        width:280px;
        background:#fff;
        padding:10px;

        /*设置圆角*/
        -webkit-border-radius:5px;
        -moz-border-radius:5px;
        border-radius:5px;
    }
    .entry-trangle{
        position:absolute;
        margin-left:-19px;
        width:0;
        height:0;
        border-top:10px solid transparent;
        border-bottom:10px solid transparent;
        border-right:10px solid #fff;
        z-index:-1;
    }
</style>

border-radius:5px;用来实现圆角;绝对定位、z-index:-1;的目的都是使控制小三角形的div在最底层,不影响父级div里面的内容布局。

原文链接:http://blog.aizhet.com/web/4382.html

时间: 2024-12-14 05:45:51

CSS3实现三角形的相关文章

css3画三角形的原理

以前用过css3画过下拉菜单里文字后面的“下拉三角符号”,类似于下面这张图片文字后面三角符号的效果 下面是一个很简单的向上的三角形代码 #triangle-up { width: 0; height: 0; border-left: 50px solid transparent; border-right: 50px solid transparent; border-bottom: 100px solid blue; } 再给相应的div加上对应的class,一个如下的三角形就用css画好了

转 css3画三角形的原理

转自  www.cnblogs.com/huangzhilong/p/5030659.html 下面是一个很简单的向上的三角形代码 #triangle-up { width: 0; height: 0; border-left: 50px solid transparent; border-right: 50px solid transparent; border-bottom: 100px solid blue; } 再给相应的div加上对应的class,一个如下的三角形就用css画好了 当时

石头教你如何用纯CSS3绘制三角形、箭头。

经常在有些网站上看到一些三角图形,但通常这些都是图片,现在石头就教你如何用纯css3技术来绘制三角图形. 下面是具体步骤,把这些看一遍你也就懂得怎样来绘制三角形.箭头了. 1.新建一个元素,随便什么元素,不过我习惯性的会用块元素来做.如果行内元素就display:block它. <div class="box"></div> 2.把它的宽高设置为height:0px; width:0px; 3.设置边框border属性,用来实现三角形. 首先要了解border具

CSS3实现三角形和对话框

这是最终实现的效果,类似于微信对话框的样式. 分析一下这个对话框的结构,由一个小三角形和一个长方形构成.长方形很容易就可以实现,重点是如何用CSS3做出一个小三角形. 一.如何生成一个三角形  总结:三角形是由设置宽度高度为0,由边框构成的正方形,分别设置边框四个边的样式,得到四个三角形拼凑在一起的效果,再设置其他方向上的边框颜色为透明色. 1.首先先做一个正方形,这个正方形不是一般的元素加上背景颜色实现的,而是对一个元素将其长和宽都设置0px,这样就相当于盒子的内容区消失.content:"

怎么利用CSS3绘制三角形

最近三角形挺火,很多地方都能碰到,如网页,微信,或者QQ空间的时间轴等地方都能看到,而且这些并不是图片插入进去的,那就需要用CSS来做了 <p class="bbb">111111111111</p> <br/> <div class="triangle-up"> <!--向上的三角--> </div> <br/> <div class="triangle-down&

css3画三角形

以下是用css3画3角形,效果如下图 <!doctype html> <html> <head> <meta charset="utf-8"> <title>无标题文档</title> <style> span{ display:inline-block; margin-right:10px} .triangle_top{height:0;width:0; border:10px solid; bord

css3 画三角形

/*箭头向下*/ .arrow-up { width:0; height:0; border-left:20px solid transparent; border-right:20px solid transparent; border-bottom:20px solid #000;} /*箭头向下*/.arrow-down { width:0; height:0; border-left:20px solid transparent; border-right:20px solid tran

使用CSS3制作三角形小图标

话不多说,直接写代码,希望能够对大家有所帮助! 1.html代码如下: <a href="#" class="usetohover"> <div class="triangle_border_right"></div> </a> <div class="ball"> <div class="triangle_border_up rotate-back&

css3实现三角形,聊天背景气泡,心形等形状

1.聊天背景气泡: css代码如下: #talkbubble {width: 120px;margin:auto; background: red; position: relative; -moz-border-radius: 10px;-webkit-border-radius: 10px; border-radius: 10px; } #talkbubble:before { content:""; position: absolute; right: 100%; top: 26