问题 带边框的纯CSS语音泡泡


所以我正在研究一个纯粹的CSS语音泡沫 http://bit.ly/zlnngM 但我想让边框环绕整个讲话泡泡,如图所示 http://bit.ly/zUgeZi 。我正在使用以下标记;

<div class="speech-bubble"><p>This is a speech bubble created using only CSS. No images to be found here...</p></div

到目前为止,我的样式如下;

.speech-bubble {
margin: 3em;
width: 320px;
padding: 10px;
background-color:rgba(250, 250, 250, 0.95);
color: #666;
font: normal 12px "Segoe UI", Arial, Sans-serif;
-moz-border-radius: 10px;
-webkit-border-radius: 10px;
border-radius: 10px;
border: 10px solid rgba(0,0,0,0.095);
}

.speech-bubble:before
{
content: "";
border: solid 20px transparent; /* set all borders to 10 pixels width */
border-top: 0; /* we do not need the top border in this case */
border-bottom-color:rgba(250, 250, 250, 0.95);
width: 0;
height: 0;
overflow: hidden;
display: block;
position: relative;
top: -30px; /* border-width of the :after element + padding of the root element */
margin: auto;
}

.speech-bubble p {
margin-top: -15px;
font-size: 1.25em;
}

正如你所看到的,我只能在内容框中添加边框(.speech-bubble)而不是标注(.speech-bubble:之前)我的边框也需要透明。不确定这是否重要,但要记住这一点。任何建议?


1985
2018-01-17 14:28


起源

只是使用SVG背景,它更简单。 - c69
@ c69刚刚开始玩SVG,但我很想知道它是否可以用纯CSS完成。干杯... - Jedda
是的,它 可以这样做, 但它 不应该是。 - c69
@ c69为什么这么说? - Jedda
当你想要图形时,你需要图形:) svg是矢量图像格式 - 所以它可以随你的规模扩展 div的。它甚至可以通过IE9支持,当然还有任何其他现代浏览器。 CSS可用于装饰块,但也可以用ascii-art - 但大多数人不使用它。因为某种原因 ;) - c69


答案:


你很亲密。你只需要同时使用一个 :before 和 :after 分层另一个三角形边框。

这是一个jsfiddle: http://jsfiddle.net/rgthree/DWxf7/ 和使用的CSS:

.speech-bubble {
    position:relative;
    width: 320px;
    padding: 10px;
    margin: 3em;
    background-color:#FFF;
    color: #666;
    font: normal 12px "Segoe UI", Arial, Sans-serif;
    -moz-border-radius: 10px;
    -webkit-border-radius: 10px;
    border-radius: 10px;
    border: 10px solid rgba(0,0,0,0.095);
}
.speech-bubble p {
    font-size: 1.25em;
}

.speech-bubble:before,
.speech-bubble:after {
    content: "\0020";
    display:block;
    position:absolute;
    top:-20px;  /* Offset top the height of the pointer's border-width */
    left:20px;
    z-index:2;
    width: 0;
    height: 0;
    overflow:hidden;
    border: solid 20px transparent;
    border-top: 0;
    border-bottom-color:#FFF;
}
.speech-bubble:before {
    top:-30px; /* Offset of pointer border-width + bubble border-width */
    z-index:1;
    border-bottom-color:rgba(0,0,0,0.095);
}

16
2018-01-17 16:20



很好的工作伙伴!非常感谢... - Jedda
这在Firefox中呈现了一个丑陋的迷你边框(在Windows上)。我的讲话泡泡也有同样的问题。 - Alexander Rechsteiner