zaro

How Do I Change the Border Color in HTML?

Published in HTML CSS Border Color 4 mins read

You can change the border color of an HTML element primarily using Cascading Style Sheets (CSS), which can be applied directly to an element, defined within the HTML document's head, or linked from an external file. Another approach, often used by scripts or tools, involves programmatically modifying the HTML document to insert style rules.

Changing Border Color with CSS

The standard way to control the appearance of borders, including their color, in HTML is by using CSS properties. The main property for setting border color is border-color. However, you typically need to set the border-style property as well, as elements do not have a border by default.

Here are the common ways to apply CSS:

Using Inline Styles

This method applies CSS rules directly to a specific HTML element using the style attribute.

<p style="border: 2px solid blue;">This paragraph has a blue border.</p>
<div style="border-color: red; border-style: dashed; border-width: 1px;">This div has a red dashed border.</div>

While quick for single instances, inline styles are generally not recommended for large projects as they make managing styles difficult.

Using Internal Stylesheet

You can define CSS rules within a <style> element placed inside the <head> section of your HTML document. This is useful for styles specific to one HTML page.

As described in the reference steps, a programmatic approach to add style involves creating a <style> element and adding it to the <head>:

  1. Load an existing HTML file.
  2. Create an instance of an HTML document (often involving parsing the file).
  3. Create a <style> element and assign the border-style and border-color values for elements like <h1> within it.
  4. Find the <head> element in your document and add the created <style> element into it.
  5. Save the modified HTML document.

Manually, you would write this directly in your HTML file:

<!DOCTYPE html>
<html>
<head>
<title>Internal Stylesheet Example</title>
<style>
  h1 {
    border-style: solid; /* Must set style for color to appear */
    border-color: green;
    border-width: 3px;
  }
  .box {
    border: 1px solid purple; /* Shorthand property */
  }
</style>
</head>
<body>

  <h1>This heading has a green border.</h1>
  <div class="box">This div has a purple border.</div>

</body>
</html>

Using External Stylesheet

For larger websites, it's best practice to define your CSS rules in a separate .css file and link it to your HTML documents using the <link> element within the <head> section.

<!DOCTYPE html>
<html>
<head>
<title>External Stylesheet Example</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>

  <p class="bordered-text">This paragraph's border color is defined externally.</p>

</body>
</html>

And in your styles.css file:

.bordered-text {
  border-style: dotted;
  border-color: orange;
  border-width: 2px;
}

Understanding Border Properties

To fully control a border's appearance, you often use these CSS properties:

Property Description Example Value
border-style Required to display a border (e.g., solid, dashed) solid
border-color Sets the color of the border blue
border-width Sets the thickness of the border 2px
border Shorthand for style, width, and color 1px solid black

You can also set individual border sides using border-top-color, border-right-color, border-bottom-color, and border-left-color.

Programmatically Modifying HTML (as per Reference)

The steps provided in the reference outline a method often used by scripts or automated tools to dynamically add or change styles in an HTML document after it has been created or loaded. This differs from manually writing the HTML/CSS code but achieves the same result of adding style rules. The process involves:

  1. Loading the Document: Opening and parsing an existing HTML file into a structure (like a Document Object Model - DOM) that can be manipulated.
  2. Creating the Style: Generating a new HTML <style> element in memory.
  3. Defining Rules: Adding CSS rules inside the created <style> element. For example, setting border-style and border-color for a specific selector like h1.
  4. Inserting into Head: Finding the document's <head> element and inserting the newly created <style> element into it.
  5. Saving: Writing the modified document structure back to an HTML file.

This method is useful in scenarios like server-side rendering, build processes, or content management systems that generate or alter HTML output.

Examples of Border Colors

You can specify colors using various formats:

  • Color Names: red, blue, green, orange, purple, etc.
  • Hex Codes: #FF0000 (red), #0000FF (blue), #008000 (green)
  • RGB Values: rgb(255, 0, 0) (red), rgb(0, 0, 255) (blue)
  • RGBA Values: rgba(255, 0, 0, 0.5) (red with 50% opacity)
  • HSL Values: hsl(0, 100%, 50%) (red)

Using CSS with the border-color property (or the border shorthand) is the standard and most flexible way to change border colors in your HTML documents.