CSS三角形制作技巧

网页中常见一些三角形,使用 CSS 直接画出来就可以,不必做成图片或者字体图标
因为边框的衔接处是倾斜的,所以可以设置一个宽高为0的盒子,边框设置的大一些,不想要的部分设置成透明色,只给想要的部分设置颜色。这样就可以实现三角形的效果。

.box {
    width: 0;
    height: 0;
    border-top: 50px solid pink;
    border-right: 50px solid red;
    border-bottom: 50px solid blue;
    border-left: 50px solid green;
}
<div class="box"></div>

image

等腰直角三角形

div {
 width: 0;
 height: 0;
 line-height: 0;
 font-size: 0;
 border: 50px solid transparent;
 border-left-color: pink;
}

image

直角边不等的直角三角形

  • 综合属性实现
.box1 {
    width: 0;
    height: 0;
    /* 顺时针方向,分别是上、右、下、左 */
    border-color: transparent red transparent transparent;
    border-style: solid;
	/*根据预期的三角形2边长设置这2个方向的边框宽度*/
    border-width: 100px 50px 0 0 ;
}
  • 四个边框的属性分开写
.box1 {
width: 0;
height: 0;
border-top: 100px solid transparent;
border-right: 50px solid red;
/* 左边和下边的边框宽度设置为0 */
border-bottom: 0;
border-left: 0;

实现过程
image

等腰三角形

只需相对的边框宽度相等

.box {
    width: 0;
    height: 0;
	/*这里设置的是上下的边框相等*/
    border-top: 25px solid transparent;
    border-left: 50px solid green;
    border-right: none;
    border-bottom: 25px solid transparent;
}

image

普通的三角形

只需相对的边框宽度不相等

.box {
    width: 0;
    height: 0;
	/*这里设置的是上下的边框相等*/
    border-top: 10px solid transparent;
    border-left: 50px solid green;
    border-right: none;
    border-bottom: 25px solid transparent;
}

image

案例练习

练习1
实现如下气泡效果
image
参考代码:

.left{
    position: relative;
    width: 500px;
    height: 200px;
    background-color: rgb(68, 114, 196);
    border: 2px solid rgb(47, 82, 143);
}
.right{
    position: absolute;
    top: 50%;
    margin-top: -50px;
    right: -99px;
    width: 0;
    border: 50px solid transparent;
    /* border-left-color: pink */
    border-left-color: rgb(68, 114, 196);
}
<div class="left">
    <div class="right"></div>
</div>

运行效果
image

练习2
实现如下效果
image

.price {
    width: 160px;
    height: 24px;
    line-height: 24px;
    border: 1px solid red;
    margin: 0 auto;
}
.miaosha {
    position: relative;
    float: left;
    width: 90px;
    height: 100%;
    background-color:red;
    text-align: center;
    color: #fff;
    font-weight: 700;
}
.miaosha i {
    position: absolute;
    right: 0;
    top: 0;
    width: 0;
    height: 0;
    border-color: transparent #fff transparent transparent;
    border-style: solid;
    border-width: 24px 10px 0 0;
}
.origin {
    font-size: 12px;
    color: gray;
    text-decoration: line-through;
    margin-left: 8px;
}
<div class="price">
    <span class="miaosha">
        ¥1650
        <i></i>
    </span>
    <span class="origin">¥5650</span>
</div>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值