How To Create A Speech Balloon With Pure CSS?

Goal

The goal of this post is to help you understand how to create a speech balloon with pure CSS without using any image.

Quick Code

HTML

But I must explain to you how all this mistaken idea of denouncing pleasure and praising pain was born and I will give you a complete account of the system, and expound the actual teachings of the great explorer of the truth, the master-builder of human happiness. No one rejects, dislikes, or avoids pleasure itself, because it is pleasure, but because those who do not know how to pursue pleasure rationally encounter consequences that are extremely painful. Nor again is there anyone who loves or pursues or desires to obtain pain of itself, because it is pain, but because occasionally circumstances occur in which toil and pain can procure him some great pleasure. To take a trivial example, which of us ever undertakes laborious physical exercise, except to obtain some advantage from it? But who has any right to find fault with a man who chooses to enjoy a pleasure that has no annoying consequences, or one who avoids a pain that produces no resultant pleasure?

CSS

.balloon {
	background: #ccc;

	border-radius: 4px;
	-moz-border-radius: 4px;
	-webkit-border-radius: 4px;

	padding: 20px;
	position: relative;
	font-size: 12px;
	width: 300px;
	text-align: justify;
	color: #3a3a3a;
}

.balloon .arrow {
	border-color: transparent #CCCCCC transparent transparent;
    border-style: solid;
    border-width: 10px;
    display: block;
    height: 0;
    left: -20px;
    position: absolute;
    top: 20px;
    width: 0;
}

Step-By-Step Explanation

In my last post, we learned about creating triangles with pure CSS. Here we will be using same technique for creating a speech balloon.

1. Create A CSS Class “balloon”

Start by creating a simple class “balloon” in your CSS file. Purpose of this class is to easily “ballonify” any HTML element simply by adding “balloon” to it’s class list.

.balloon {}

2. Apply “balloon” To An HTML Element

Let us create a new DIV in our HTML and apply this class to it.

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

3. Apply Cosmetics to “balloon”

Let us add some style and color to “balloon”, just like you specify style of any element.

.balloon {
background: #ccc;

border-radius: 4px;
-moz-border-radius: 4px;
-webkit-border-radius: 4px;

width: 300px;
padding: 20px;

font-size: 12px;
color: #3a3a3a;
text-align: justify;
}

Our DIV will now look something like this:

4. Create A New DIV Inside “balloon” DIV

Let us create a new DIV inside the “balloon” DIV. Make sure that the width and height of this new DIV is 0 and it’s border width is 10px.

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
.balloon .arrow {
width: 0;
height: 0;
display: block;

border-width: 10px;
border-color: red green blue yellow;
border-style: solid;
}

5. Hide 3 Sides Of The Border Of Inner DIV

Next we will hide the top, bottom and left sides of the border of the inner DIV by assigning them ‘transparent’ color.

.balloon .arrow {
width: 0;
height: 0;
display: block;

border-width: 10px;
border-color: transparent green transparent transparent;
border-style: solid;
}

6. Place Triangle On Correct Position

Now we need to position triangle correctly on the left side of the “balloon”. It is important that we are able to absolutely position “arrow” relative to it’s parent “balloon”. For this, we will need to set “absolute” as “position” of “arrow” and “relative” as position of “balloon”. After this, we will be able to adjust arrow’s position using “top” and “left” properties.
Also, let’s change the color of arrow to grey, so that it looks part of the balloon.

.baloon {
	background: #ccc;
	
	border-radius: 4px;
	-moz-border-radius: 4px;
	-webkit-border-radius: 4px;
	
	padding: 20px;
	position: relative;
	font-size: 12px;
	width: 300px;
	text-align: justify;
	color: #3a3a3a;
}

.baloon .arrow {
	width: 0;
	height: 0;
	display: block;

	border-width: 10px;
	border-color: transparent #ccc transparent transparent;
	border-style: solid;

	position: absolute;
	top: 20px;
	left: -20px;
}

Next Steps

You have learned how to create speech balloons using pure CSS. You can play around with this technique and easily create arrows on top, right and bottom sides of the balloons.

There is a small problem with this technique: in order to apply “balloon” style to an HTML element, you need to update the markup of that element and append a new DIV inside it to display the triangle. This does not look very elegant. This post presents a technique using which you will not need to update the markup of the “balloon” element. Simply applying the “balloon” class to any HTML element will accurately balloonify it and will also display the triangle.

Published by

Umar Ashfaq

Umar Ashfaq is a full-stack web developer. His core strength is building neat UIs with JavaScript on web but he also enjoys server side Java, NodeJS and Objective C. Follow him on twitter @umarashfaq87

2 thoughts on “How To Create A Speech Balloon With Pure CSS?”

Leave a Reply

Your email address will not be published. Required fields are marked *