Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a speech bubble in css?

Tags:

css

css-shapes

I would like to create a speech like this,

enter image description here

I try to create using CSS. But I cannot align the top arrow like this. My Code is,

.bubble
{
  position: relative;
  width: 275px;
  height: 40px;
  padding: 5px;
  background: #C00006;
  -webkit-border-radius: 14px;
  -moz-border-radius: 14px;
  border-radius: 14px;
}

.bubble:after
{
  content: '';
  position: absolute;
  border-style: solid;
  border-width: 0 19px 79px;
  border-color: #C00006 transparent;
  display: block;
  width: 0;
  z-index: 1;
  margin-left: -19px;
  top: -79px;
  left: 69%;
}
<br><br><br><br>
<div class="bubble"></div>

Online example (on JSFiddle).

like image 407
Vinod VT Avatar asked Oct 28 '14 06:10

Vinod VT


People also ask

What is CSS bubble?

Event bubbling directs an event to its intended target, and works like this: When an object (like a button) is clicked, an event is directed to the object. If an event handler is set for the object, the event handler is triggered. Then the event bubbles up (like a bubble in water) to the objects parent.

How do you add a bubble in HTML?

bubbles = []; Then we can create a bubble with arbitrary size and color with the following function. Noticed that the height and width are 2 times the size, in our scenario bubble. bubbleSize indicates the radius of the bubble, which is half of the width or height.


1 Answers

You could achieve that by using skewX transform and specifying the origin of the transform as follows:

.bubble {
    position: relative;
        top: 4.8em;
    width: 275px;
    height: 40px;
    padding: 5px;
    background: #C00006;
    -webkit-border-radius: 14px;
    -moz-border-radius: 14px;
    border-radius: 14px;
}

.bubble:after {
    content: '';
    position: absolute;
    border-style: solid;
    border-width: 0 19px 79px;
    border-color: #C00006 transparent;
    display: block;
    width: 0;
    z-index: 1;
    /* top: -79px; */
    bottom: 100%; /* better than specifying the top */
    right: 38px;  /* equal to width of the arrow, for instance */
    
    -webkit-transform: skewX(-45deg);
    -moz-transform: skewX(-45deg);
    -ms-transform: skewX(-45deg);
    -o-transform: skewX(-45deg);
    transform: skewX(-45deg);
    
    -webkit-transform-origin: 38px 100%;
    -moz-transform-origin: 38px 100%;
    -ms-transform-origin: 38px 100%;
    -o-transform-origin: 38px 100%;
    transform-origin: 38px 100%;
}
<div class="bubble"></div>

It's worth noting that CSS transforms are supported in IE 9 and newer.

like image 108
Hashem Qolami Avatar answered Oct 06 '22 18:10

Hashem Qolami