CSS实现对话框小三角效果

效果图

应用场景

一般在模拟对话框类UI时使用

实现思路

  • 利用伪类实现添加小三角
  • 用border制作小三角,小三角宽高为0,设置4个边的边框宽度及颜色来实现
  • 边框的实现是利用两种颜色的小三角叠加实现,两个三角的位移决定小三角边框宽度

代码

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>Test Page</title>
    <link rel="stylesheet" href="style/main.css">
    <style>
        .dialog{
            width: 200px;
            height: 100px;
            border: 1px solid #ccc;
            margin :  50px auto;
            position: relative;
        }
        .triangle:before{
            content: '';
            border-right: 20px solid #ccc;
            border-top: 20px solid transparent;
            border-bottom: 20px solid transparent;
            border-left: 0;
            position: absolute;
            left: 0;
            top: 50%;
            margin-left: -20px;
            margin-top: -20px;
        }
        .triangle:after{
            content: '';
            border-right: 20px solid #fff;
            border-top: 20px solid transparent;
            border-bottom: 20px solid transparent;
            border-left: 0;
            position: absolute;
            left: 0;
            top: 50%;
            margin-left: -19px;
            margin-top: -20px;
        }
    </style>
</head>

<body>
    <div class="dialog triangle">Let's party!</div>
</body>

</html>
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 转载请标明出处:http://www.aiuxian.com/article/p-1982467.html 接下来...
    lucky_yaya阅读 11,787评论 3 9
  • 选择qi:是表达式 标签选择器 类选择器 属性选择器 继承属性: color,font,text-align,li...
    wzhiq896阅读 5,790评论 0 2
  • 选择qi:是表达式 标签选择器 类选择器 属性选择器 继承属性: color,font,text-align,li...
    love2013阅读 6,831评论 0 11
  • 各种纯css图标 CSS3可以实现很多漂亮的图形,我收集了32种图形,在下面列出。直接用CSS3画出这些图形,要比...
    剑残阅读 13,240评论 0 8
  • 问答题47 /72 常见浏览器兼容性问题与解决方案? 参考答案 (1)浏览器兼容问题一:不同浏览器的标签默认的外补...
    _Yfling阅读 14,680评论 1 92