简体   繁体   English

如何用伪元素制作弯曲的三角形气泡?

[英]How to make a curved triangle speech bubble with pseudo-elements?

Hi everyone I am trying to make a curved triangle speech bubble with the CSS ::after pseudo-element. 大家好,我想用CSS ::after伪元素制作一个弯曲的三角形气泡。

I have created like this DEMO : 我创建了这样的DEMO

.test 
{
position: relative;
width: 430px;
height: 175px;
padding: 0px;
background: #FFFFFF;
-webkit-border-radius: 6px;
-moz-border-radius: 6px;
border-radius: 6px;
}

.test:after 
{
content: '';
position: absolute;
border-style: solid;
border-width: 14px 14px 0;
border-color: #FFFFFF transparent;
display: block;
width: 0;
z-index: 1;
bottom: -14px;
left: 8px;
}

How do I do like the picture? 我如何喜欢图片?

在此处输入图片说明

you could use pseudo elements (and their trickery) to generate something similar to this: 您可以使用伪元素(及其伪造)来生成类似于以下内容的内容:


 div { height: 200px; width: 350px; background: tomato; margin: 50px; position: relative; display: inline-block; } div:before { content: ""; position: absolute; height: 40px; width: 40px; background: tomato; bottom: 2%; left: -15px; border-radius: 0 0 100% 0; -webkit-transform: rotate(35deg); transform: rotate(35deg); } div:after { content: ""; position: absolute; height: 40px; width: 10px; background: white; bottom: 7%; left: -18px; border-radius: 50%; -webkit-transform: rotate(35deg); transform: rotate(35deg); } 
 <div>Hello world</div> 


 .test { height: 200px; width: 350px; background: tomato; border-radius: 5px; position: relative; margin: 20px; } .test:before { content: ""; position: absolute; height: 20px; width: 20px; background: tomato; bottom: 20px; left: -6px; transform-origin: center center; transform: rotate(30deg); border-radius: 0 0 100% 0; } .test:after { content: ""; position: absolute; height: 20px; width: 10px; background: white; bottom: 24px; left: -11px; border-radius: 50%; transform-origin: center center; transform: rotate(30deg); } .zoom { margin: 0 auto; height: 200px; width: 200px; background: red; position: relative; border-radius: 0 0 100% 0; transform-origin:center center; transform: rotate(45deg); margin-top:50px; } .zoom:before { content: ""; position: absolute; height: 100%; width: 50%; background: white; border-radius: 50%; left: -25%; top: 0; } 
 <div class="test">hello world</div> <hr/> <div class="zoom"></div> 


tested in Chrome, IE and Firefox (latest versions) 在Chrome,IE和Firefox(最新版本)中进行了测试

Just another posibility, so that you can choose 只是另一种可能性,所以您可以选择

.test:after {
    content: '';
    position: absolute;
    border: 0px solid;
    display: block;
    width: 0;
    z-index: -1;
    bottom: 30px;
    left: -13px;
    width: 40px;
     height: 40px;
    background-color: transparent;
    border-bottom-left-radius: 50%;
    border-bottom-right-radius: 50%;
    box-shadow: 14px 10px 0px 10px white;
}

codepen 码笔

To move the arrow to the left side, simply change the position of the pseudo element and the borders. 要将箭头移到左侧,只需更改伪元素和边框的位置。 At this time, however, I don't think it is possible to create a concave curve without introducing way more complexity, like clipping masks. 但是,这时,我认为在不引入更多复杂性(如剪切蒙版)的情况下创建凹曲线是不可能的。 It would be easier to create a .png image of the curved triangle in Photoshop or Illustrator and then position it next to the speech bubble as desired. 在Photoshop或Illustrator中创建弯曲三角形的.png图像,然后根据需要将其放置在气泡旁边会更容易。

CodePen Example CodePen示例

HTML: HTML:

<div class="container">
    <div class="test"></div>
</div>

CSS: CSS:

body{
    background-color:#000;
}
.container {
    margin:0px auto;
    width:500px;
    margin-top:50px;
}
.test 
{
    position: relative;
    width: 430px;
    height: 175px;
    padding: 0px;
    background: #FFFFFF;
    -webkit-border-radius: 6px;
    -moz-border-radius: 6px;
    border-radius: 6px;
}

.test:after 
{
    content: '';
    position: absolute;
    border-style: solid;
    border-width: 0px 0px 15px 16px;
    border-color: #FFFFFF transparent;
    display: block;
    width: 0;
    z-index: 1;
    bottom: 4px;
    left: -15px;
}

Trying to achieve that curved triangle shape with pure CSS might be very tricky, so I'd suggest instead you create that shape in vector format, save it off as an SVG and use that in your :after element. 尝试使用纯CSS实现该弯曲三角形的形状可能会非常棘手,所以我建议您改为以矢量格式创建该形状,将其另存为SVG并在您的:after元素中使用。

Of course save off a PNG fallback for browsers that don't support SVG as well. 当然,也为不支持SVG的浏览器节省PNG后备。

As others have already mentioned, a simple CSS potitioning change would get your :after element in the bottom left-hand corner, but an SVG might be your only option to get the curved triangle shape. 正如其他人已经提到的,一个简单的CSS定位更改将使您的:after元素位于左下角,但是SVG可能是获取弯曲三角形形状的唯一选择。

It seems you're just positioning it at the bottom instead of the left... 看来您只是将其定位在底部而不是左侧...

.test:after {
    content: '';
    position: absolute;
    border-style: solid;
    border-width: 0 0 14px 14px;
    border-color: #FFFFFF transparent;
    display: block;
    width: 0;
    z-index: 1;
    bottom: 4px;
    left: -14px;
}

However this won't replicate the image exactly, which instead shows a curve. 但是,这不会完全复制图像,而是显示一条曲线。 While you could probably replicate that curve with nothing but a bunch of clever border tricks, it would make a lot more sense to use an image. 尽管您可能只用了一系列巧妙的边框技巧就可以复制该曲线,但是使用图像会更有意义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM