首页前端开发HTMLbefore :after怎么输出一个小三角形

before :after怎么输出一个小三角形

时间2024-01-23 07:00:26发布访客分类HTML浏览235
导读:收集整理的这篇文章主要介绍了before :after怎么输出一个小三角形,觉得挺不错的,现在分享给大家,也给大家做个参考。这次给大家带来before :after怎么输出一个小三角形,before :after输出一个小三角形的注意事项有...
收集整理的这篇文章主要介绍了before :after怎么输出一个小三角形,觉得挺不错的,现在分享给大家,也给大家做个参考。这次给大家带来before :after怎么输出一个小三角形,before :after输出一个小三角形的注意事项有哪些,下面就是实战案例,一起来看一下。

之前写的三角形一直在同一个颜色,没有边框的样式。如下:

CSS代码如下:

.tri-up{
    width: 0;
    height:0;
    border-left:20px solid transparent;
    border-right: 20px solid transparent;
    border-bottom: 20px solid red;
  }
.tri-left{
    width: 0;
    height:0;
    border-top:20px solid transparent;
    border-bottom: 20px solid transparent;
    border-left: 20px solid red;
}
.tri-right{
    width: 0;
    height:0;
    border-top:20px solid transparent;
    border-bottom: 20px solid transparent;
    border-right: 20px solid red;
}
.tri-down{
    width: 0;
    height:0;
    border-left:20px solid transparent;
    border-right: 20px solid transparent;
    border-top: 20px solid red;
 }
    

若要写以下的小三角,则需要用到伪类:before :after

@H_126_16@

CSS代码:

#demo {
                margin: 100px;
    ;
                width: 100px;
                height: 100px;
                background-color: #fff;
                posITion: relative;
                border: 2px solid #333;
        }
 //方框的样式        #demo:after, #demo:before {
                border: solid transparent;
                content: ' ';
                height: 0;
                left: 100%;
        //根据三角形的位置,可以随意更改。            position: absolute;
                width: 0;
        }
         #demo:after {
                border-width: 10px;
                border-left-color: #fff;
                top: 20px;
//根据三角的位置改变        }
//此处是一个白色的三角        #demo:before {
                border-width: 12px;
                border-left-color: #000;
                top: 18px;
        }
    此处是一个黑色的三角//当#demo:after和#demo:before的样式重合以后,由于top值的大小不同,就可以得到中间是白色,但是边框的三角形。如上图。
p id="demo">
    /p>
    

若要改为下图的样式:

则CSS代码如下:

#demo {
                margin: 100px;
    ;
                width: 100px;
                height: 100px;
                background-color: #fff;
                position: relative;
                border: 2px solid #333;
        }
        #demo:after, #demo:before {
                border: solid transparent;
                content: ' ';
                height: 0;
                top: 100%;
                position: absolute;
                width: 0;
        }
        #demo:after {
                border-width: 10px;
                border-top-color: #fff;
                left: 20px;
        }
        #demo:before {
                border-width: 12px;
                border-top-color: #000;
                left: 18px;
        }
    

相信看了本文案例你已经掌握了方法,更多精彩请关注其它相关文章!

推荐阅读:

h5实现多图片预览上传及点击可拖拽控件

CSS3混合模式使用详解

以上就是before :after怎么输出一个小三角形的详细内容,更多请关注其它相关文章!

声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!

afterbefore

若转载请注明出处: before :after怎么输出一个小三角形
本文地址: https://pptw.com/jishu/583879.html
毛毛虫爬行动画怎样实现 实现卡片3D翻转效果

游客 回复需填写必要信息