css - :after,:before的绝对定位问题

查看:397
本文介绍了css - :after,:before的绝对定位问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

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

<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <style>
    .reply {
        position: relative;
        width: 200px;
        height: 80px;
        border: 1px solid rgba(243, 237, 127, 0.68);
        border-radius: 5px;
        background-color: rgba(243, 237, 127, 0.35);
    }
    
    .reply:after {
        content: '';
        width: 0px;
        height: 0px;
        border-color: transparent transparent rgba(243, 237, 127, 0.35) transparent;
        border-style: solid;
        border-width: 6px;
        position: absolute;
        top: -11px;
        border-radius: 3px;
        left: 18px;
        right: auto;
    }
    
    .reply:before {
        content: '';
        width: 0px;
        height: 0px;
        border-color: transparent transparent rgba(243, 237, 127, 0.68) transparent;
        border-style: solid;
        border-width: 7px;
        position: absolute;
        top: -14px;
        border-radius: 3px;
        left: 17px;
        right: auto;
    }
    </style>
</head>

<body>
    <div class="reply"></div>
</body>

</html>

实现简单的聊天气泡(如下图),我的问题是两个伪类中top和left的值应该如何设置呢?

解决方案

border-color: transparent transparent rgba(243, 237, 127, 0.68) transparent;
border-style: solid;
border-width: 7px;

这句理解了就好了,这句话的意思是4个三角形合成1个正方形,其中另外3个三角形是transparent透明的,只有下一个显示。原始位置在父元素左上角

这篇关于css - :after,:before的绝对定位问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆