How To Create A Box Around Text In HTML (The Easy Way)

Welcome to a beginner’s tutorial on how to create a box around the text in HTML. New to HTML and want to quickly create your own notification or information box?

An easy way to create a box around text is to simply add padding and border. For example, <p style="padding: 10px; border: 2px solid red;">TEXT</p>

That’s it. But rather than leaving you guys clueless as to what just happened, we will walk through more examples in this guide – Read on!

ⓘ I have included a zip file with all the example source code at the start of this tutorial, so you don’t have to copy-paste everything… Or if you just want to dive straight in.

 

 

QUICK SLIDES

 

TABLE OF CONTENTS

Download & Notes Creating Boxes Useful Bits & Links
The End

 

DOWNLOAD & NOTES

Firstly, here is the download link to the example code as promised.

 

EXAMPLE CODE DOWNLOAD

Click here to download the source code, I have released it under the MIT license, so feel free to build on top of it or use it in your own project.

 

QUICK NOTES

If you spot a bug, please feel free to comment below. I try to answer questions too, but it is one person versus the entire world… If you need answers urgently, please check out my list of websites to get help with programming.

 

 

CREATING BOXES

All right, let us now get into creating boxes with HTML and CSS.

 

EXAMPLE 1) BOX BASICS – THE BOX MODEL

HMTL & CSS

1-box.html
<div style="background: ghostwhite; 
            font-size: 20px; 
            padding: 10px; 
            border: 1px solid lightgray; 
            margin: 10px;">
  This is some dummy text.
</div>

 

THE RESULT

This is some dummy text.

 

THE EXPLANATION

Just what are all those background, font-size, padding, border, margin? Simply put, HTML renders each element based on something called “the box model” – That is something like layers of an onion.

  1. At the center of the box, we have the text (contents).
    • The text can be controlled with various properties – font-size, font-weight, font-familiy, text-decoration… Which should be pretty self-explanatory.
    • We can also set the background color or use a background-image.
  2. Followed by a layer of padding.
  3. Next, a layer of border.
  4. Finally, a margin on the outside.

Well, the best is to hands-on – Go ahead, download the example above, edit each property and see for yourself.

 

 

EXAMPLE 2) CSS CLASSES – SOMETHING LIKE TEMPLATES

HMTL & CSS

2-css-class.html
<style>
/* (A) SHARED CLASS */
.box {
  margin: 10px;
  padding: 10px;
  font-size: 18px;
}
 
 /* (B) BOX VARIATIONS */
/* (B1) INFORMATION BOX */
.info {
  color: brown;
  background: cornsilk;
  border: 1px solid burlywood;
}
 
/* (B2) WARNING BOX */
.warn {
  color: darkmagenta;
  background: lightpink;
  border: 1px solid darkred;
}
 
/* (B3) SUCCESS */
.ok {
  color: darkgreen;
  background: greenyellow;
  border: 1px solid darkgreen;
}
</style>
 
<!-- (B1) INFORMATION -->
<div class="box info">
  &#9432; Information box.
</div>
 
<!-- (B2) WARNING -->
<div class="box warn">
  &#9888; Warning icon.
</div>
 
<!-- (B3) SUCCESS -->
<div class="box ok">
  &#9432; Successful!
</div>

 

THE RESULT

ⓘ Information box.
⚠ Warning icon.
ⓘ Successful!

 

THE EXPLANATION

The first example works great, but it will be kind of dumb to copy the same style all over. So now for a crash course on how to create reusable CSS classes.

  • First, we create a <style> tag in the <head> section.
  • Then define the CSS classes  .CLASS-NAME { STYLES-TO-APPLY } in the <style> tag.
  • Finally, simply attach the CSS classes to the HTML elements – <ELEMENT class="CLASS-NAME">.

Yep, it’s that simple, and it should save you a lot of time from copy-pasting the same style property all over the place. This will also make it easier for future updates – Just change the class styles in the <head> section, and all the rest will follow.

 

 

EXTRA EXAMPLE) ADDING ICONS

extra-icons.html
<div style="background: ghostwhite; font-size: 20px; padding: 10px; border: 1px solid lightgray; margin: 10px;">
  &#9432; Information icon.
  <br>
  &#9888; Warning icon.
  <br>
  &#10084; Heart icon.
  <br>
  &#128161; Bulb icon.
</div>
ⓘ Information icon.
⚠ Warning icon.
❤ Heart icon.
💡 Bulb icon.

Lastly, here is a small extra. Just what are those &#ABCD? Those are HTML symbols, and they are native to HTML; We don’t need to download any images nor use any extra third-party libraries to spice up the boxes. HTML actually has a ton of symbols, and I will leave a link in the extras section below for the complete list of symbols.

 

 

USEFUL BITS & LINKS

That’s all for this tutorial, and here is a small section on some extras and links that may be useful to you.

 

LINKS & REFERENCES

 

INFOGRAPHIC CHEAT SHEET

How To Create A Box Around Text In CSS (click to enlarge)

 

THE END

Thank you for reading, and we have come to the end of this guide. I hope that it has helped you with your project, and if you want to share anything with this guide, please feel free to comment below. Good luck and happy coding!

2 thoughts on “How To Create A Box Around Text In HTML (The Easy Way)”

  1. I still don’t understand. I can’t grasp it correctly. In my navigation tab, where there are links like HOME, ABOUT, CONTACT, and others, I wanted to put a box to surround each of them, but I find it quite hard.

Leave a Comment

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