Add Border To Table With Bootstrap, CSS or jQuery

Last Updated on July 16, 2021 by Roshan Parihar

In this tutorial, learn how to add a border to the table using Bootstrap, CSS, or jQuery. The short answer is: add the Bootstrap .table-bordered class to <table> element.

You can also use the CSS border property to add borders to the table cells. A table contains <th> cell and <td> cell. However, if you do not apply any method to add borders to your table cells. The table has no default property to automatically add bordered cells.

That’s why you need a method that is given below to add borders. So, let’s start learning each method of adding borders.

Use Bootstrap Table Classes For Adding Border

One of my favorite and simple methods is by using Bootstrap. If you use the Bootstrap method to add borders, you have to just add the classes .table and .table-bordered to the <table> tag.

You don’t need to do anything after that. After the addition of the above classes, you will get a table with each cell are bordered. You may also like to read different Bootstrap table classes in detail.

Test it Live

Output

Sr. No. Device Name Quantity
1 Washing Machine 2
2 Keyboard 6
3 Ceiling Fan 12
4 Laptop 3

The above table contains the added borders using Bootstrap. It’s the clean method where you have just put the useful Bootstrap classes.

Bonus: download a Free Bootstrap CHEAT SHEET that will show you 20+ most important examples to learn in Bootstrap.

How to Add Borders to Table Using CSS

If you use the CSS to add borders to the table cell, you have to use CSS border property and add a few more CSS. You also have to add a class selector to select the element and apply the CSS. Add CSS individually for the table header rows as well as to the internal rows.

See the example below to apply CSS and add the borders to table cells.

Test it Live

Output

Sr. No. Computer Parts Name Size
1 RAM 2 GB
2 Hard Disk 1 TeraByte
3 Mouse 2
4 Pen Drive 40 GB

The above example contains the CSS style and the table. The added CSS applied the borders to each cell of the table. You have to use the same CSS as given above. However, you can change the border color and padding as per your requirements.

Create Border In Table Cells Using jQuery

In addition to the above all, you can also apply the borders to the table cell using jQuery. To perform this task and apply borders, you have to use the css() method to apply a style to the element using jQuery. Before applying the jQuery given below, you also have to add the class to the <table> tag.

See the method given below and apply borders using jQuery.

Test it Live

Output

Sr. No. Computer Parts Name Size
1 RAM 2 GB
2 Hard Disk 1 TeraByte
3 Mouse 2
4 Pen Drive 40 GB

You will get the same bordered table as you get using Bootstrap and CSS. The above example contains the bordered table applied through jQuery. You may also like to read How JQuery Add Style To HTML Element And Apply CSS.

DOWNLOAD Free Bootstrap CHEAT SHEET

I hope you like this post on how to add the border to the table using Bootstrap, CSS, and jQuery.

Reference
Stackoverflow Discussion for Bootstrap Classes for adding table-border