Front-end Tutorials

Front-end Tutorials

HTMLCSSSassJavaScriptReactJS
CMS Tutorials

CMS Tutorials

WordPress
Tutorials expand

HTML Basic

HTML Reference

HTML border Attribute

Posted in HTML Tutorial
Updated on Jan 27, 2024
By Mari Selvan
👁️ 42 - Views
⏳ 4 mins
💬 1 Comment
HTML border Attribute

Photo Credit to CodeToFun

🙋 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, style, and color of the border surrounding an element.

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

💎 Values

The border attribute accepts three values to define different aspects of the border:

  • Width: Specifies the width of the border. It can be set in pixels, em units, or as a percentage of the element's width.
  • Style: Defines the style of the border. Common styles include solid, dashed, dotted, double, and more.
  • Color: Sets the color of the border. You can use color names, hexadecimal values, RGB values, or other valid color representations.

📄 Example

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

border.html
Copied
Copy To Clipboard
<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>

🧠 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
Copied
Copy To Clipboard
<script>
  // Dynamically set border properties for an element
  document.getElementById("dynamicElement").style.border = "2px solid #3498db";
</script>

🧠 How it Works

In this script, the border attribute is dynamically set to a 2-pixel solid border with the color #3498db for an element with the id dynamicElement.

🏆 Best Practices

  • Use the border attribute judiciously, considering the design requirements of your webpage.
  • Experiment with different combinations of width, style, and color to achieve the desired visual effect.
  • 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.

By understanding and using this attribute appropriately, you can create visually appealing and well-defined layouts on your webpages.

👨‍💻 Join our Community:

To get interesting news and instant updates on Front-End, Back-End, CMS and other Frameworks. Please Join the Telegram Channel:

Author

author
👋 Hey, I'm Mari Selvan

For over eight years, I worked as a full-stack web developer. Now, I have chosen my profession as a full-time blogger at codetofun.com.

Buy me a coffee to make codetofun.com free for everyone.

Buy me a Coffee

Share Your Findings to All

Subscribe
Notify of
guest
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Mari Selvan
Mari Selvan
3 months ago

If you have any doubts regarding this article (HTML border Attribute), please comment here. I will help you immediately.

We make use of cookies to improve our user experience. By using this website, you agree with our Cookies Policy
AgreeCookie Policy