Change HTML Border Color in C# | Change Border Color CSS

In this blog post, we will learn how to change the HTML border color in C#. This guide will provide you with the knowledge and skills you need to effectively change the border color, border color CSS, HTML table border color, etc. in HTML files programmatically using C#.

This article covers the following topics:

  1. C# API to change border color in HTML files
  2. Change the HTML border color
  3. Border color CSS using internal CSS
  4. Change the HTML table border color
  5. Free learning resources

C# API to Change Border Color in HTML Files

We will use Aspose.HTML for .NET for changing the border color in HTML files. It is a powerful and versatile cross-platform class library that empowers developers to manipulate and manage HTML documents within their .NET applications. It allows you to create, edit, and convert HTML files. Aspose.HTML for .NET enables you to analyze and extract content from HTML files. It supports not only HTML5 but also CSS3 and HTML Canvas specifications, allowing you to style your HTML documents and interact with dynamic elements.

Please either download the DLL of the API or install it using NuGet.

PM> Install-Package Aspose.Html

Change HTML Border Color in C#

The border-color property sets the color of all four borders of an element. When a single value is assigned to the border-color property, all borders will be painted with that color. For example, if we set the border-color property to the color red, then all four border colors will be red. Alternatively, we have the flexibility to specify distinct color values for the top, right, bottom, and left borders.

We can change the border color of any HTML element by following the steps below:

  1. Load an existing HTML file using the HTMLDocument class.
  2. Get the specific HTMLElement to change the border color.
  3. Set the border style attributes, e.g., BorderStyle, BorderColor.
  4. Finally, save the HTML document to a file.

The following code sample shows how to change the border color in HTML using C#.

Change HTML Border Color in C#

Change HTML Border Color in C#

Change Border Color CSS using Internal CSS in C#

We can change the border color by adding internal CSS using the <style> element in an HTML document by following the steps below:

  1. Load an existing HTML file using the HTMLDocument class.
  2. Create a <style> element using the CreateElement() method.
  3. Specify the TextContent for the <style> element.
  4. Get the specific HTMLElement to change the border color.
  5. After that, append the style element using the AppendChild() method.
  6. Finally, save the HTML document to a file.

The following code sample shows how to change the border color using internal CSS in C#.

The above code sample appends the following <style> element in the <head> section of the output HTML document.

<style>
h1 { 
	color: blue; 
	border-top-style: solid; 
	border-right-style: solid; 
	border-bottom-style: solid; 
	border-left-style: solid; 
	border-top-color: rgb(220, 30, 100); 
	border-right-color: rgb(220, 30, 100); 
	border-bottom-color: rgb(220, 30, 100); 
	border-left-color: rgb(220, 30, 100); }
</style>

Change HTML Table Border Color in C#

We can easily change the border color of HTML tables using internal or inline CSS. We can apply the <style> element to the HTML <table> element.

Please follow the steps below to change the border color of the HTML table.

  1. Load an existing HTML file using the HTMLDocument class.
  2. Select the table using the QuerySelector() method.
  3. Set the style attribute using the SetAttribute() method.
  4. Finally, save the HTML document to a file.

The following code sample shows how to change the border color of an HTML table in C#.

Change HTML Table Border Color in C#

Change HTML Table Border Color in C#

Get a Free License

You can get a free temporary license to try Aspose.HTML for .NET without evaluation limitations.

Create HTML Table – Learning Resources

Besides changing the border color, you can learn more about the library and explore various other features using the resources below:

Conclusion

In this blog post, we have learned how to change the border color in HTML documents using C#. We have explored various methods to change the border colors of different HTML elements. By following the steps and code samples provided in this article, you can easily develop your own customized solutions for working with HTML documents. In case of any ambiguity, please feel free to contact us on our free support forum.

See Also