How to Change Border Color in HTML? C# Examples

In this article, we will use C# examples to show different ways to set or change border color in HTML files using Aspose.HTML class library.

To set or change the border color for HTML element is easy with the CSS border-color property. There are a few ways you can set this property value. You can use inline, internal or external CSS, and HTML color values may be specified as the standard color names or with HEX, RGB, RGBA, HSL, and HSLA values.

For more information on how to use HTML color codes, please visit the HTML Color Codes article. In the Border Color section, you find out HTML code examples of how to change the border color.

Change Border Color Using Inline CSS

You can set or change the border color using inline style attribute – inline CSS. The border-color property will only work when the border-style property is defined first, which is used to set the borders. This property will not work alone. If this property is not set, it inherits the element’s colour. The border-color property accepts color names, RGB, RGBA, HEX, HSL, or HSLA values. To change border color for HTML element using Aspose.HTML API you should follow a few steps:

  1. Load an existing HTML file.
  2. Create an instance of an HTML document.
  3. Determine which element you want to change the border color for and find this element to set a style attribute for it. Use the GetElementsByTagName(name) method of the Element class that returns HTML element with a given tag name.
  4. Set the style attribute with border-style and border-color properties: use the Style property of the HTMLElement class.
  5. Save the modified HTML document.

You can set or change border color for various HTML elements such as <p>, <h1><h6>, <div> or <table>. The following C# example shows the border color change for the <h1>element:

 1using Aspose.Html;
 2using System.IO;
 3using System.Linq;
 4...
 5
 6    // Prepare an output path for a document saving
 7    string savePath = Path.Combine(OutputDir, "change-border-color.html");
 8
 9    // Prepare path to source HTML file
10    string documentPath = Path.Combine(DataDir, "file.html");
11
12    // Create an instance of an HTML document
13    var document = new HTMLDocument(documentPath);
14
15    // Find the h1 element to set a style attribute
16    var header = (HTMLElement)document.GetElementsByTagName("h1").First();
17
18    // Set style attribute properties
19    header.Style.Color = "#8B0000";
20    header.Style.BorderStyle = "solid";
21    header.Style.BorderColor = "rgb(220,30,100)";
22
23    // Save the HTML document to a file
24    document.Save(Path.Combine(savePath));

It should be noted that in this С# example, not only the border color for the <h1> element was changed, but also the text color for this element (the color property was added).

You can download the complete examples and data files from GitHub.

Change Color for Four Sides of the Border

The border-color property sets the color of the four borders of an element. If the border-color property has a single value, then the entire border will be painted with this color. But you can set different color values for the top, right, bottom, and left borders. For example, if you set the border-color: red blue green gray, the top border is red, the right border is blue, the bottom border is green and the left border is grey.

 1using Aspose.Html;
 2using System.IO;
 3using System.Linq;
 4...
 5
 6    // Prepare output path for document saving
 7    string savePath = Path.Combine(OutputDir, "change-four-border-color.html");
 8
 9    // Prepare path to source HTML file
10    string documentPath = Path.Combine(DataDir, "change-border-color.html");
11
12    // Create an instance of an HTML document
13    var document = new HTMLDocument(documentPath);
14
15    // Find the h1 element to set a style attribute
16    var header = (HTMLElement)document.GetElementsByTagName("h1").First();
17
18    // Set style attribute properties
19    header.Style.BorderStyle = "solid";
20    header.Style.BorderColor = "red blue green gray";
21
22    // Save the HTML document to a file
23    document.Save(Path.Combine(savePath));

The figure illustrates the results of changing border color for the <h1> element using inline CSS: a) changed color for entire border; b) changed color for each of the four sides of the border.

Text “Rendered the change-border-color.html and change-four-border-color.html files with changed border color for the h1”

Change Border Color Using Internal CSS

The same border colorization result can be achieved using internal CSS, as shown in the following HTML code example:

1<head>
2<style>
3	h1 {
4	border-color: rgb(220,30,100);
5	}
6</style>
7</head>

Note: Keep in mind, that the usage of a style attribute overrides any style set in the HTML <style> tag or external style sheet.

The next C# example demonstrates how to realize internal CSS to change border color. Take a few steps:

  1. Load an existing HTML file.
  2. Create an instance of an HTML document.
  3. Create a <style> element and assign the border-style and border-color values for <h1> element.
  4. Find the <head> element in your document and add the <style> element into it.
  5. Save the modified HTML document.
 1using Aspose.Html;
 2using System.IO;
 3using System.Linq;
 4...
 5
 6    // Prepare an output path for a document saving
 7    string savePath = Path.Combine(OutputDir, "change-border-color-internal-css.html");
 8
 9    // Prepare path to source HTML file
10    string documentPath = Path.Combine(DataDir, "file.html");
11
12    // Create an instance of an HTML document
13    var document = new HTMLDocument(documentPath);
14
15    // Create a style element and assign the color border-style and border-color values for h1 element
16    var style = document.CreateElement("style");
17    style.TextContent = "h1 { color:DarkRed; border-style:solid; border-color:rgb(220,30,100) }";
18
19    // Find the document's <head> element and add a <style> element to it
20    var head = document.GetElementsByTagName("head").First();
21    head.AppendChild(style);
22
23    // Save the HTML document to a file
24    document.Save(Path.Combine(savePath));

The С# code execution that we have given above will result in the <style> element appearing in the <head> of the output file, which is illustrated by the following HTML code:

 1<head>
 2<style>
 3	h1 { 
 4		color: darkred;
 5		border-top-style: solid;
 6		border-right-style: solid;
 7		border-bottom-style: solid;
 8		border-left-style: solid;
 9		border-top-color: rgb(220, 30, 100);
10		border-right-color: rgb(220, 30, 100);
11		border-bottom-color: rgb(220, 30, 100);
12		border-left-color: rgb(220, 30, 100); }
13</style>
14</head>

The result is similar to the one shown in Figure (a) above.

Change Table Border Color

If you want to change the table border color, you can use inline or internal CSS.

You can apply the style attribute with the HTML <table> element. Keep in mind, that the usage of a style attribute overrides any style set in the HTML <style> tag or external style sheet. To change the table border color using inline CSS style attribute you can use the QuerySelector() method to navigate DOM and find the <table> element. Then set the style attribute with the required properties for the <table> element:

C# Example 1 – Using inline CSS

 1using Aspose.Html;
 2using System.IO;
 3using System.Linq;
 4...
 5
 6    // Prepare an output path for a document saving
 7    string savePath = Path.Combine(OutputDir, "change-table-border-color.html");
 8
 9    // Prepare path to source HTML file
10    string documentPath = Path.Combine(DataDir, "table.html");
11
12    // Create an instance of an HTML document
13    var document = new HTMLDocument(documentPath);
14
15    // Create a CSS Selector that selects the first table element
16    var element = document.QuerySelector("table");
17
18    // Set style attribute with properties for the selected element
19    element.SetAttribute("style", "border: 2px #0000ff solid");
20
21    // Save the HTML document to a file
22    document.Save(Path.Combine(savePath));

JavaScript code 1

1<script>
2	// Create a CSS Selector that selects the first table element
3    var element = document.querySelector("table");
4
5    // Set style attribute with properties for the selected element
6    element.setAttribute("style", "border: 2px #0000ff solid");
7</script>

The same table border colorization result can be achieved using internal CSS, as shown in the following C# code example 2:

C# Example 2 – Using internal CSS

 1using Aspose.Html;
 2using System.IO;
 3using System.Linq;
 4...
 5
 6    // Prepare an output path for a document saving
 7    string savePath = Path.Combine(OutputDir, "change-table-border-color.html");
 8
 9    // Prepare path to source HTML file
10    string documentPath = Path.Combine(DataDir, "table.html");
11
12    // Create an instance of an HTML document
13    var document = new HTMLDocument(documentPath);
14
15    // Create a style element and assign the color border-style and border-color values for table element
16    var style = document.CreateElement("style");
17    style.TextContent = "table { border-style:solid; border-color:rgb(0, 0, 255) }";
18
19    // Find the document's <head> element and add a <style> element to it
20    var head = document.GetElementsByTagName("head").First();
21    head.AppendChild(style);
22
23    // Save the HTML document to a file
24    document.Save(Path.Combine(savePath));

JavaScript code 2

1<script> 
2	// Create a style element and assign the border-style and border-color values for table element
3	var style = document.createElement("style");
4	style.textContent = "table { border-style:solid; border-color:rgb(0, 0, 255) }";
5
6	// Find the document's <head> element and add a <style> element to it
7	var head = document.getElementsByTagName("head")[0];
8	head.appendChild(style);
9</script>	

The figure shows the HTML table table.html after changing the border color from red to blue:

Text “Rendered the change-table-border-color.html file with changed table border color from red to blue”

HTML Table Generator is an online application for creating tables with customizable options. It’s free and clear to use. Just fill in all required options and get a result!

Text “Banner HTML Table Generator”

Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.