如何用 CSS 创建对话气泡的曲线尾部?

问题描述 投票:0回答:2

我正在使用 CSS 创建一个对话气泡,我已经做到了这一点。

.says{
  width: 200px;
  padding: 20px;
  margin-right: 20px;
  background: #BF7EF2;
  color: #fff;
  box-shadow: -3px 3px 5px #C1B9C8;
  position: relative;
  border-radius: 5px;
}

.says:before{
  content: "";
  position: absolute;
  z-index: -1;
  top: 14px;
  right: -18px;
  height: 20px;
  border-right: 20px solid #BF7EF2;
  border-bottom-right-radius: 25px 20px;
  transform: translate(0, -4px);
  box-shadow: -3px 3px 5px #C1B9C8;
}

.says:after{
  content: "";
  position: absolute;
  z-index: -1;
  top: 7px;
  right: -18px;
  width: 30px;
  height: 30px;
  background: #fff;
  border-bottom-left-radius: 40px 35px;
  transform: translate(0px, -20px);
}
<div class="says">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Ratione aut facere cupiditate, sunt, nisi fugiat consectetur officiis veniam!</div>

基本上我已经使用了

:before
:after
伪类并应用了
border-radius
。然后相互重叠即可达到想要的效果。现在,如您所见,我在
background: #fff
上使用
:after
,因为当前父级的背景是白色。这将在我的应用程序中遍历许多具有不同背景颜色的不同 div。这就是我现在遇到的问题。

示例-
enter image description here

我可以在不使用

:after
上的背景属性的情况下实现相同的“语音气泡”尾巴吗?
^ 这行解释了它不是链接问题的重复。

或者通过任何其他完全不同的方式?

css css-shapes
2个回答
13
投票

正如webtiki所说,你可以根据我之前的答案得到这个结果(尽管可能有点困难)

.container {
  width:300px;
  margin:5px;
}
.test 
{
position: relative;
width: 300px;
height: 150px;
padding: 0px;
background: pink;
border-radius: 6px;
}

.test:after {
    content: '';
    top: 1px;
    right: -29px;
    position: absolute;
    border: 0px solid;
    display: block;
    width: 38px;
     height: 26px;
    background-color: transparent;
    border-bottom-left-radius: 50%;
    border-bottom-right-radius: 50%;
    box-shadow: -21px 9px 0px 8px pink;
}
<div class="container">
  <div class="test"></div>
</div>
<img src="https://i.sstatic.net/MYlKY.png" alt="enter image description here">


0
投票

喜欢这里的解决方案!我最近在一个项目中使用了它,但需要使用与屏幕上使用位置成比例的不同方向。为此,我在测试中跟踪了该解决方案的 8 个方向变化,并且不希望工作白费。如果需要的话,我整理了一个 CodePen 供其他人使用。

/** IGNORE THIS STUFF, JUST PEN SETUP **/
body {
  width: 100%;
  height: 100%;
  margin: 0;
  display: flex;
  flex-direction: column;
}
.bubble-group {
  display: flex;
  flex-direction: row;
}
.bubble {
  display: block;
  position: relative;
  color: #ddd;
  border: 2px dashed #eee;
  background-color: transparent;
  width: 200px;
  height: 150px;
  display: flex;
  justify-content: center;
  align-items: center;
}
.bubble > span {
  position: absolute;
}


/** FOCUS ON STYLINGS BELOW **/

/* Default Stylings (Except border) */
.one:after,
.two:after,
.three:after,
.four:after,
.five:after,
.six:after,
.seven:after,
.eight:after {
  content: "";
  display: block;
  border: 1px dashed #e28cff;
  background-color: transparent;
  /* Probably want to position these aboslute to move them around */
}

/***** GROUP #1 Constants *****/
.one:after,
.two:after,
.three:after,
.four:after {
  width: 42px;
  height: 26px;
}

/** Top side (Notice Border-radius) **/
.one:after {
  /* W & H defined above */
  border-top-right-radius: 50%;
  border-top-left-radius: 50%;
  box-shadow: 21px -9px 0px 8px #782893;
}
.two:after {
  /* W & H defined above */
  border-top-right-radius: 50%;
  border-top-left-radius: 50%;
  box-shadow: -21px -9px 0px 8px #782893;
}

/** Bottom side (Notice Border-radius) **/
.three:after {
  /* W & H defined above */
  border-bottom-right-radius: 50%;
  border-bottom-left-radius: 50%;
  box-shadow: -21px 9px 0px 8px #782893;
}
.four:after {
  /* W & H defined above */
  border-bottom-right-radius: 50%;
  border-bottom-left-radius: 50%;
  box-shadow: 21px 9px 0px 8px #782893;
}

/***** GROUP #2 Constants *****/
/* Notice we swapped W and H */
.five:after,
.six:after,
.seven:after,
.eight:after {
  width: 26px;
  height: 42px;
}

/* Because of this adjustment, we also swapped offset values for the box-shadow */

/** Left side (Notice Border-radius) **/
.five:after {
  /* W & H defined above */
  border-top-left-radius: 50%;
  border-bottom-left-radius: 50%;
  box-shadow: -9px -21px 0px 8px #782893;
}
.six:after {
  /* W & H defined above */
  border-top-left-radius: 50%;
  border-bottom-left-radius: 50%;
  box-shadow: -9px 21px 0px 8px #782893;
}

/** Right side (Notice Border-radius) **/
.seven:after {
  /* W & H defined above */
  border-top-right-radius: 50%;
  border-bottom-right-radius: 50%;
  box-shadow: 9px 21px 0px 8px #782893;
}
.eight:after {
  /* W & H defined above */
  border-top-right-radius: 50%;
  border-bottom-right-radius: 50%;
  box-shadow: 9px -21px 0px 8px #782893;
}
<!-- Group #1 -->
<div class="bubble-group">
  <div class="bubble one"><span>1</span></div>
  <div class="bubble two"><span>2</span></div>
  <div class="bubble three"><span>3</span></div>
  <div class="bubble four"><span>4</span></div>
</div>

<!-- Group #2 -->
<div class="bubble-group">
  <div class="bubble five"><span>5</span></div>
  <div class="bubble six"><span>6</span></div>
  <div class="bubble seven"><span>7</span></div>
  <div class="bubble eight"><span>8</span></div>
</div>

https://codepen.io/nss5161/pen/abeBgvq

© www.soinside.com 2019 - 2024. All rights reserved.