HTML border Attribute

Beginner
⏱️ 4 min read
📚 Updated: Jun 2026
🎯 2 Examples
Layout & Styling

Introduction

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.

What You’ll Learn

01

Pixel Width

1, 2, 0, etc.

02

Tables

border="1"

03

Images

Legacy img use.

04

CSS border

Style & color.

05

JavaScript

setAttribute.

06

Deprecated

Use CSS instead.

Purpose of border

The 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.

⚠️
Width Only — Not CSS Shorthand

The HTML border attribute sets border width in pixels only. Border style and color require the CSS border property.

📝 Syntax

Add border to a supported element with a non-negative integer width in pixels:

border.html
<table border="1">...</table>
<img src="photo.jpg" alt="Photo" border="2">

Syntax Rules

  • Value is a non-negative integer representing border width in pixels.
  • border="0" removes the visible border on supported elements.
  • Valid on table, img, object, and iframe (legacy).
  • For dashed borders, colors, or rounded corners, use CSS instead.

💎 Values

The HTML border attribute accepts a numeric value that sets the border width in pixels. Common values include:

  • Width (pixels) — Specifies how thick the border appears. Examples: border="1" for a thin border, border="2" for a thicker border.
  • Zeroborder="0" hides the border on tables and images.
  • Style & color — Not controlled by the HTML attribute. Use CSS such as border: 2px solid #3498db; for dashed lines, colors, and other effects.
border-values.html
<table border="1">...</table>
<table border="0">...</table>

<!-- CSS for style and color -->
<table style="border: 2px solid #3498db;">...</table>

⚡ Quick Reference

ValueEffectCSS Alternative
border="1"1px border on table/imgborder: 1px solid #ccc;
border="2"2px borderborder: 2px solid #ccc;
border="0"No visible borderborder: none;
Border styleNot in HTML attributeborder-style: dashed;
Border colorNot in HTML attributeborder-color: #3498db;
HTML5 statusDeprecatedUse CSS for new code

Applicable Elements

ElementSupported?Notes
<table>Yes (legacy)Most common use
<img>Yes (legacy)Frame around image
<object>, <iframe>Yes (legacy)Embedded content frame
<div>, <p>NoUse CSS border

Examples Gallery

Table border example and dynamic JavaScript border width.

👀 Live Preview

Simple table with a 1-pixel border:

Row 1, Cell 1Row 1, Cell 2
Row 2, Cell 1Row 2, Cell 2

Example

Let’s look at an example of how to use the border attribute in an HTML table:

border.html
<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>
Try It Yourself

How It Works

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.

Dynamic Values with JavaScript

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:

border.html
<table id="dynamicElement">...</table>

<script>
  // Dynamically add border attribute to the table
  document.getElementById("dynamicElement").setAttribute("border", "2");
</script>
Try It Yourself

How It Works

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.

♿ Accessibility

  • Borders aid structure — Visible table borders can help users distinguish cells, but semantic markup matters more.
  • Do not rely on color alone — Border color differences should not be the only way to convey meaning.
  • Ensure contrast — Border lines should be visible against the page background.
  • Use CSS for focus rings — Interactive element outlines use CSS outline, not the HTML border attribute.
  • Keep tables readable — Pair borders with proper <th> headers and captions.

🧠 How border Works

1

Author sets border width

Add a pixel number such as border="1" on a supported element.

Markup
2

Browser draws frame

The user agent renders a border frame around the table or image.

Render
3

Cells inherit outline

On tables, cell grid lines appear based on border width and browser defaults.

Table
=

Visible border displayed

For full control of style and color, use CSS border in modern projects.

Browser Support

The border attribute is deprecated in HTML5 but still rendered by browsers on legacy elements like table and img.

⚠️ HTML4 legacy · Deprecated

Still renders in browsers

Use CSS border for style, color, and responsive layouts.

Legacy Rendering support
Google Chrome Legacy rendering
Deprecated
Mozilla Firefox Legacy rendering
Deprecated
Apple Safari Legacy rendering
Deprecated
Microsoft Edge Legacy rendering
Deprecated
border attribute Deprecated

Bottom line: Browsers still draw table borders, but CSS is the standard for new code.

💡 Best Practices

✅ Do

  • Use CSS border for all new styling projects
  • Use border-collapse: collapse with CSS table borders
  • Keep border widths subtle for readability
  • Test table borders on mobile screen sizes
  • Use border="0" only when legacy markup requires it

❌ Don’t

  • Confuse HTML border with CSS border shorthand
  • Expect HTML border to set dashed or dotted styles
  • Apply border attribute to div or paragraph elements
  • Rely on thick borders alone for layout structure
  • Mix legacy border with conflicting CSS without testing
  • Use the border attribute judiciously, considering the design requirements of your webpage.
  • Experiment with different pixel widths to achieve the desired visual effect on legacy tables.
  • For more complex border requirements, consider using CSS for a finer level of control and flexibility.

Conclusion

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.

Key Takeaways

Knowledge Unlocked

Five truths every developer should know about border

Bookmark these before adding borders to your HTML.

5
Core concepts
📊 02

Tables

Most common.

Scope
💻 03

CSS border

Style & color.

Modern
⚙️ 04

setAttribute

Dynamic width.

JS
⚠️ 05

Deprecated

Use CSS.

HTML5

❓ Frequently Asked Questions

It sets the border width in pixels on supported elements like tables and images.
No. The HTML attribute only controls width. Use CSS border-style for solid, dashed, or dotted lines.
table, img, object, and iframe in legacy HTML.
Set border="0" on the table, or use CSS border: none;.
It is deprecated. Browsers still render it, but CSS is the recommended approach.
border: 1px solid #ccc; controls width, style, and color together.

Style borders the modern way

Practice the legacy HTML border attribute and compare it with CSS borders in the Try It editor.

Try table example →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

5 people found this page helpful