Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.
To change HTML border color in C#, load the document with HTMLDocument, find the target element, set both border-style and border-color with inline or internal CSS, and save the updated file.
This article shows several ways to set or change border color in HTML files using Aspose.HTML for .NET. The examples cover one element, four-side border colors, internal CSS, and HTML table borders.
The CSS border-color property accepts named colors, HEX, RGB, RGBA, HSL, and HSLA values. It is important to define border-style as well, because border-color only becomes visible when a border style is applied. For more details, see
HTML Color Codes and the
Border Color section of the color tutorial.
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. This property will not create a visible border by itself. If border-color is not set, the border color inherits the element’s color.
To change border color for HTML element using Aspose.HTML API you should follow a few steps:
name) method of the
Element class that returns HTML element with a given tag name.style attribute with border-style and border-color properties: use the
Style property of the
HTMLElement class.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:
1// Сhange border color for <h1> element using C#
2
3// Prepare an output path for a document saving
4string savePath = Path.Combine(OutputDir, "change-border-color.html");
5
6// Prepare path to source HTML file
7string documentPath = Path.Combine(DataDir, "file.html");
8
9// Create an instance of an HTML document
10HTMLDocument document = new HTMLDocument(documentPath);
11
12// Find the h1 element to set a style attribute
13HTMLElement header = (HTMLElement)document.GetElementsByTagName("h1").First();
14
15// Set the style attribute properties
16header.Style.Color = "#8B0000";
17header.Style.BorderStyle = "solid";
18header.Style.BorderColor = "rgb(220,30,100)";
19
20// Save the HTML document to a file
21document.Save(Path.Combine(savePath));It should be noted that in this C# example, not only the border color for the <h1> element was changed, but also the text color for this element because the color property was added.
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.
1// Set different colors for all four borders of HTML element using C#
2
3// Prepare an output path for a document saving
4string savePath = Path.Combine(OutputDir, "change-four-border-color.html");
5
6// Prepare path to source HTML file
7string documentPath = Path.Combine(DataDir, "change-border-color.html");
8
9// Create an instance of an HTML document
10HTMLDocument document = new HTMLDocument(documentPath);
11
12// Find the h1 element to set a style attribute
13HTMLElement header = (HTMLElement)document.GetElementsByTagName("h1").First();
14
15// Set the style attribute properties
16header.Style.BorderStyle = "solid";
17header.Style.BorderColor = "red blue green gray";
18
19// Save the HTML document to a file
20document.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.

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-style: solid;
5 border-color: rgb(220,30,100);
6 }
7</style>
8</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:
<style> element and assign the border-style and border-color values for <h1> element.<head> element in your document and add the <style> element into it. 1// Add internal CSS to style HTML in C#
2
3// Prepare an output path for a document saving
4string savePath = Path.Combine(OutputDir, "change-border-color-internal-css.html");
5
6// Prepare path to source HTML file
7string documentPath = Path.Combine(DataDir, "file.html");
8
9// Create an instance of an HTML document
10HTMLDocument document = new HTMLDocument(documentPath);
11
12// Create a style element and assign the color border-style and border-color values for h1 element
13Element style = document.CreateElement("style");
14style.TextContent = "h1 {color:DarkRed; border-style:solid; border-color:rgb(220,30,100) }";
15
16// Find the document head element and append style element to the head
17Element head = document.GetElementsByTagName("head").First();
18head.AppendChild(style);
19
20// Save the HTML document to a file
21document.Save(Path.Combine(savePath));The C# 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.
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:
1// Change table border color using C#
2
3// Prepare an output path for a document saving
4string savePath = Path.Combine(OutputDir, "change-table-border-color-inline-css.html");
5
6// Prepare path to source HTML file
7string documentPath = Path.Combine(DataDir, "table.html");
8
9// Create an instance of an HTML document
10HTMLDocument document = new HTMLDocument(documentPath);
11
12// Create a CSS Selector that selects the first table element
13Element element = document.QuerySelector("table");
14
15// Set style attribute with properties for the selected element
16element.SetAttribute("style", "border: 2px #0000ff solid");
17
18// Save the HTML document to a file
19document.Save(Path.Combine(savePath));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:
1// Change HTML table border color using C#
2
3// Prepare an output path for a document saving
4string savePath = Path.Combine(OutputDir, "change-table-border-color-internal-css.html");
5
6// Prepare path to source HTML file
7string documentPath = Path.Combine(DataDir, "table.html");
8
9// Create an instance of an HTML document
10HTMLDocument document = new HTMLDocument(documentPath);
11
12// Create a style element and assign the color border-style and border-color values for table element
13Element style = document.CreateElement("style");
14style.TextContent = "table { border-style:solid; border-color:rgb(0, 0, 255) }";
15
16// Find the document head element and append style element to the head
17Element head = document.GetElementsByTagName("head").First();
18head.AppendChild(style);
19
20// Save the HTML document to a file
21document.Save(Path.Combine(savePath));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:

| Issue | Why it happens | What to check |
|---|---|---|
| Border color is not visible | border-color does not create a border by itself. | Set border-style and, when needed, border width together with border-color. |
| Internal CSS does not change the border | Inline style attributes override rules in <style> and external CSS. | Remove or update the inline style on the target element. |
| Table border changes only the outer border | A table can have borders on the table, rows, and cells. | Apply CSS to table, th, or td depending on which border should change. |
The element may not have a visible border. Set border-style, such as solid, before or together with border-color.
Yes. Use a four-value border-color declaration or set side-specific properties such as border-top-color, border-right-color, border-bottom-color, and border-left-color.
Use QuerySelector() to find the table and set inline CSS, or add an internal CSS rule for table, th, and td elements depending on the required result.
color property for text elements.Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.