【问题标题】:How to make a curved triangle speech bubble with pseudo-elements?如何用伪元素制作一个弯曲的三角形语音气泡?
【发布时间】:2015-02-11 01:26:42
【问题描述】:

大家好,我正在尝试使用 CSS ::after 伪元素制作一个弯曲的三角形语音气泡。

我已经创建了这样的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;
}

我怎么喜欢这张照片?

【问题讨论】:

  • “梯形”是什么意思?...图片中的那个是弯曲的...不是梯形。
  • @TylerH 这很好,我怎么能像图片那样做?

标签: css css-shapes


【解决方案1】:

你可以使用伪元素(及其诡计)来生成 类似的东西:


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>

在 Chrome、IE 和 Firefox(最新版本)中测试

【讨论】:

  • 根据caniuse,2D 变换也需要一个前缀版本。将为此努力并为您改进。
  • np 很高兴我能帮上忙。
【解决方案2】:

只是另一种可能性,让你可以选择

.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

【讨论】:

    【解决方案3】:

    要将箭头移动到左侧,只需更改伪元素的位置和边框即可。然而,目前,我认为在不引入更多复杂性的情况下创建凹曲线是不可能的,比如剪切蒙版。在 Photoshop 或 Illustrator 中创建弯曲三角形的 .png 图像,然后根据需要将其放置在对话气泡旁边会更容易。

    CodePen Example

    HTML:

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

    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;
    }
    

    【讨论】:

    • 仅供参考,问题是问如何获得 very 微妙的弯曲形状,而不是如何将 after 元素定位在底角。
    • @tylerh 图中的三角形是梯形的。我想变成那样
    • @innovation 图中的三角形不是梯形。它是一个具有倒置或凹弧的扇区。
    • @TylerH 对不起我的英语。你说得对,我想让它变成那样
    • @JamieWade 是的,我的回答只是部分的,但它几乎实现了他的尝试。最重要的是,它将三角形放在 OP 想要的位置。曲线很重要,如果我能很好地实现它,以后可能会添加到答案中。
    【解决方案4】:

    看来您只是将其定位在底部而不是左侧...

    .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;
    }
    

    但是,这不会完全复制图像,而是显示一条曲线。虽然您可能只需要一些巧妙的边框技巧就可以复制该曲线,但使用图像会更有意义。

    【讨论】:

      【解决方案5】:

      尝试使用纯 CSS 实现弯曲的三角形可能非常棘手,因此我建议您以矢量格式创建该形状,将其保存为 SVG 并在您的 :after 元素中使用。

      当然,为不支持 SVG 的浏览器保存一个 PNG 后备。

      正如其他人已经提到的,一个简单的 CSS 药水更改将使您的 :after 元素位于左下角,但 SVG 可能是您获得弯曲三角形形状的唯一选择。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-06-27
        • 1970-01-01
        • 2022-06-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-09-22
        相关资源
        最近更新 更多