Search code examples
htmlcsscss-shapes

How to create a curved speech bubble?


I'm trying to make a curved speech bubble out of a div, like this:

enter image description here

This is what I've tried, but it doesn't really work:

.userMsgBottom{
  position: relative;
  display: inline-block;
  max-width: 260px;
  height: auto;
  word-wrap: break-word;
  background-color: #2e7384;
  margin-top: 12px;
  margin-left: 8px;
  margin-right: 8px;
  padding: 7px;
  border-radius: 6px;
}
.userMsgBottom:after {
    content: "";
    position: absolute;
    bottom: 0px;
    right: 5px;
    width: 70px;
    height: 30px;
    background-color: #2e7384;
    transform: skew(45deg);
    transform-origin: top right;
    border-radius: 0 15% 5% 0% / 25%;
    z-index: -1;
}

it does this:

enter image description here

How would I go about doing this?


Solution

  • I created a collection where you find such shapes and more: https://css-generators.com/tooltip-speech-bubble/


    I would consider radial-gradient to do this:

    .userMsgBottom {
      position: relative;
      display: inline-block;
      color:#fff;
      max-width: 260px;
      background-color: #2e7384;
      margin: 30px;
      padding: 7px;
      border-radius: 6px;
    }
    
    .userMsgBottom:after {
      content: "";
      position: absolute;
      bottom: 0;
      right: -25px;
      width: 40px;
      height: 25px;
      background: radial-gradient(25px at top right, #0000 99%, #2e7384 102%);
    }
    
    .userMsgBottom.left:after {
      content: "";
      position: absolute;
      bottom: 0;
      left: -25px;
      width: 40px;
      height: 25px;
      background: radial-gradient(25px at top left, #0000 99%, #2e7384 102%);
    }
    <div class="userMsgBottom">
      Some text here<br> Some text here<br> Some text here
    </div>
    
    <div class="userMsgBottom left">
      Some text here<br> Some text here<br> Some text here
    </div>

    Here is another idea using mask with CSS variables to easily control the shape (from my blog: https://css-tip.com/css-speech-bubble/)

    .bubble {
      --r: 25px; /* the radius */
      --t: 30px; /* the size of the tail */
      
      max-width: 300px;
      padding: calc(2*var(--r)/3);
      -webkit-mask: 
        radial-gradient(var(--t) at var(--_d) 0,#0000 98%,#000 102%) 
          var(--_d) 100%/calc(100% - var(--r)) var(--t) no-repeat,
        conic-gradient(at var(--r) var(--r),#000 75%,#0000 0) 
          calc(var(--r)/-2) calc(var(--r)/-2) padding-box, 
        radial-gradient(50% 50%,#000 98%,#0000 101%) 
          0 0/var(--r) var(--r) space padding-box;
      background: linear-gradient(135deg,#FE6D00,#1384C5) border-box;
      color: #fff;
    }
    .left {
      --_d: 0%;
      border-left: var(--t) solid #0000;
      margin-right: var(--t);
      place-self: start;
    }
    .right {
      --_d: 100%;
      border-right: var(--t) solid #0000;
      margin-left: var(--t);
      place-self: end;
    }
    
    body {
      margin: 0;
      min-height: 100vh;
      display: grid;
      place-content: center;
      gap: 20px;
      font-family: system-ui, sans-serif;
      font-size: 20px;
    }
    <div class="bubble left">Bro ipsum dolor sit amet gaper backside single track, manny Bike epic clipless. Schraeder drop gondy, rail fatty slash gear jammer steeps</div>
    <div class="bubble right">Ok, Thank you</div>
    <div class="bubble left"> ut labore et dolore magna </div>
    <div class="bubble right">👌</div>

    CSS only curved speech bubble