👀 Live Preview
Simple table with a 1-pixel border:
| Row 1, Cell 1 | Row 1, Cell 2 |
| Row 2, Cell 1 | Row 2, Cell 2 |

The border attribute in HTML is a fundamental element used to define the border properties of an HTML element, primarily applicable to table elements. It allows developers to specify the width of the border surrounding an element in pixels.
1, 2, 0, etc.
border="1"
Legacy img use.
Style & color.
setAttribute.
Use CSS instead.
borderThe primary purpose of the border attribute is to provide a visual distinction for HTML elements. It is commonly used to outline tables, images, and other elements on a webpage, enhancing the overall layout and design.
The HTML border attribute sets border width in pixels only. Border style and color require the CSS border property.
Add border to a supported element with a non-negative integer width in pixels:
<table border="1">...</table>
<img src="photo.jpg" alt="Photo" border="2">border="0" removes the visible border on supported elements.table, img, object, and iframe (legacy).The HTML border attribute accepts a numeric value that sets the border width in pixels. Common values include:
border="1" for a thin border, border="2" for a thicker border.border="0" hides the border on tables and images.border: 2px solid #3498db; for dashed lines, colors, and other effects.<table border="1">...</table>
<table border="0">...</table>
<!-- CSS for style and color -->
<table style="border: 2px solid #3498db;">...</table>| Value | Effect | CSS Alternative |
|---|---|---|
border="1" | 1px border on table/img | border: 1px solid #ccc; |
border="2" | 2px border | border: 2px solid #ccc; |
border="0" | No visible border | border: none; |
| Border style | Not in HTML attribute | border-style: dashed; |
| Border color | Not in HTML attribute | border-color: #3498db; |
| HTML5 status | Deprecated | Use CSS for new code |
| Element | Supported? | Notes |
|---|---|---|
<table> | Yes (legacy) | Most common use |
<img> | Yes (legacy) | Frame around image |
<object>, <iframe> | Yes (legacy) | Embedded content frame |
<div>, <p> | No | Use CSS border |
Table border example and dynamic JavaScript border width.
Simple table with a 1-pixel border:
| Row 1, Cell 1 | Row 1, Cell 2 |
| Row 2, Cell 1 | Row 2, Cell 2 |
Let’s look at an example of how to use the border attribute in an HTML table:
<table border="1">
<tr>
<td>Row 1, Cell 1</td>
<td>Row 1, Cell 2</td>
</tr>
<tr>
<td>Row 2, Cell 1</td>
<td>Row 2, Cell 2</td>
</tr>
</table>In this example, the border attribute is set to 1, indicating that a border with a width of 1 pixel should be applied to the entire table.
You can dynamically set the border attribute using JavaScript. This can be useful when you want to change the border properties based on user interactions or certain conditions. Here’s a brief example:
<table id="dynamicElement">...</table>
<script>
// Dynamically add border attribute to the table
document.getElementById("dynamicElement").setAttribute("border", "2");
</script>In this script, the border attribute is dynamically set to 2 for an element with the id dynamicElement, giving the table a 2-pixel border. To also control border color and style, use CSS element.style.border instead.
outline, not the HTML border attribute.<th> headers and captions.Add a pixel number such as border="1" on a supported element.
The user agent renders a border frame around the table or image.
On tables, cell grid lines appear based on border width and browser defaults.
For full control of style and color, use CSS border in modern projects.
The border attribute is deprecated in HTML5 but still rendered by browsers on legacy elements like table and img.
Use CSS border for style, color, and responsive layouts.
Bottom line: Browsers still draw table borders, but CSS is the standard for new code.
border for all new styling projectsborder-collapse: collapse with CSS table bordersborder="0" only when legacy markup requires itborder with CSS border shorthandborder attribute judiciously, considering the design requirements of your webpage.The border attribute is a valuable tool for defining the visual appearance of borders around HTML elements, especially legacy tables and images. By understanding and using this attribute appropriately, you can create visually appealing and well-defined layouts on your webpages.
For modern development, CSS border gives you full control over width, style, color, and radius. Learn the HTML attribute for legacy code, then reach for CSS in new projects.
borderBookmark these before adding borders to your HTML.
1, 2, 0.
ValuesMost common.
ScopeStyle & color.
ModernDynamic width.
JSUse CSS.
HTML5border-style for solid, dashed, or dotted lines.table, img, object, and iframe in legacy HTML.border="0" on the table, or use CSS border: none;.border: 1px solid #ccc; controls width, style, and color together.Practice the legacy HTML border attribute and compare it with CSS borders in the Try It editor.
5 people found this page helpful