带有框阴影的CSS语音泡泡

Lor*_*lin 53 html css css3 dropshadow css-shapes

创建一个使用CSS在左侧绘制三角形的DIV.尝试将统一的box-shadow应用于父元素和伪元素(请参阅图像)和代码.

这可能吗?或者我最好使用border-image吗?

(上:暗影之前,中间:CSS Box-Shadow,Bottom:Desired Result)

添加Box-Shadow之前的元素

添加了box-shadow的元素

期望的结果

.bubble{
    height: 200px;
    width:  275px;

    opacity: 0;

    margin-top: 41px;

    float: right;

    background-color: #F2F2F2;

    -webkit-border-radius: 5px;
    -webkit-box-shadow: 0px 0px 6px #B2B2B2;
}

.bubble::after {
        height: 0px;
        width:  0px;

        content: "\00a0";

        display: block;

        margin-left: -10px;
        margin-top:   28px;

        border-width: 10px 10px 10px 0;
        border-style: solid;
        border-color: transparent #F2F2F2 transparent transparent;

        -webkit-box-shadow: 0px 0px 6px #B2B2B2;
    }
Run Code Online (Sandbox Code Playgroud)

Thi*_*iff 107

您可以使用旋转div来transform获得真实,而不是使用三角形黑客box-shadow.因为你只想在div的一侧(可见的三角形边)上有阴影,所以你必须使得blur越小越好opacity.

演示:http://jsfiddle.net/ThinkingStiff/mek5Z/

HTML:

<div class="bubble"></div>
Run Code Online (Sandbox Code Playgroud)

CSS:

.bubble{
    background-color: #F2F2F2;
    border-radius: 5px;
    box-shadow: 0px 0px 6px #B2B2B2;
    height: 200px;
    margin: 20px;
    width:  275px;
}

.bubble::after {
    background-color: #F2F2F2;
    box-shadow: -2px 2px 2px 0 rgba( 178, 178, 178, .4 );
    content: "\00a0";
    display: block;
    height: 20px;
    left: -10px;
    position: relative;
    top: 20px;
    transform:             rotate( 45deg );
        -moz-transform:    rotate( 45deg );
        -ms-transform:     rotate( 45deg );
        -o-transform:      rotate( 45deg );
        -webkit-transform: rotate( 45deg );
    width:  20px;
}
Run Code Online (Sandbox Code Playgroud)

输出:

在此输入图像描述

  • @LordVarlin我最近用它作为iPhone后退按钮,所以我不必使用图像.为了更有趣,您可以使用`transform:skew();`来改变三角形的角度. (3认同)
  • @jetlej那是在演示链接中.我会修改答案.谢谢! (2认同)

mez*_*zis 8

这是完整(S)CSS中的完整工作示例,其中包含鼻子大小阴影宽度和可选边框的变量.

诀窍是获得偏移并向右转换以实现像素完美,并在overflow:hidden必要时使用来削减气泡的鼻子(特别是如果你需要边框).

上面答案中的示例对我们不起作用,因为阴影被裁剪并覆盖在主要的泡沫区域上.

在IE7/8中优雅地降级.

HTML:

<div class="chat">
    <div class="bubble">
        <span class='tail'>&nbsp;</span>
        <p>The path of the righteous man is beset on all sides by the iniquities of the selfish and the tyranny of evil men. Blessed is he who, in the name of charity and good will, shepherds the weak through the valley of darkness, for he is truly his brother's keeper and the finder of lost children.</p><p>And I will strike down upon thee with great vengeance and furious anger those who would attempt to poison and destroy My brothers. And you will know My name is the Lord when I lay My vengeance upon thee.</p>
    </div>
</div>
Run Code Online (Sandbox Code Playgroud)

SCSS:

$shadow_radius = 6px;
$nose_size = 12px;
$shadow = 0 1px $shadow_radius #B2B2B2;
$border =  1px solid #bbb

.chat {
    font-family:      sans-serif;
    font-size:        small;
}

.bubble {
    background-color: #F2F2F2;
    border-radius:    5px;
    border:           $border;
    box-shadow:       $shadow;
    display:          inline-block;
    padding:          10px 18px;
    margin-left:     ($shadow_radius + $nose_size);
    margin-right:    ($shadow_radius + $nose_size);
    position:         relative;
    vertical-align:   top;
}

.tail {
    position: absolute;
    top:      $nose_size;
    left:   -($shadow_radius + $nose_size);
    height:  ($shadow_radius + $nose_size);
    width:   ($shadow_radius + $nose_size);
    overflow: hidden;
}
.tail:before {
    border:            $border;
    background-color:  #F2F2F2;
    box-shadow:        $shadow;
    content:           "\00a0";

    display:           block;
    position:          absolute;
    top:               0px;
    left:              $nose_size;
    height:            $nose_size;
    width:             $nose_size;
    -webkit-transform: skew( -45deg );
    -moz-transform:    skew( -45deg );
}
Run Code Online (Sandbox Code Playgroud)


Ami*_*far 7

另一种解决方案是使用filter: drop-shadow(0 1px 2px rgba(0,0,0,.5));它仅在对象形状周围放置阴影。