Change HTML Background Color in C#

To change HTML background color in C#, load the document with HTMLDocument, find the target element, set background-color through HTMLElement.Style or a <style> element, and save the updated file.

This article shows how to change background color in HTML files using Aspose.HTML for .NET. You can update one element, set the color for the entire page, or add an internal CSS rule to the document.

The CSS background-color property accepts named colors, HEX, RGB, RGBA, HSL, and HSLA values. The examples below use HEX and RGB color values. For more details, see HTML Color Codes and the Background Color section of the color tutorial.

Change Background Color of Specific Element

To change background color for HTML element using Aspose.HTML API you should follow a few steps:

  1. Load an existing HTML file.
  2. Determine which element you want to change the background 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.
  3. Set the style attribute with background-color property: use the Style property of the HTMLElement class.
  4. Save the modified HTML document.

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

Change One Element Background Color in C#

 1// Change background color for HTML paragraph using C#
 2
 3// Prepare output path for document saving
 4string savePath = Path.Combine(OutputDir, "change-background-color-p-inline-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// Find the paragraph element to set a style attribute
13HTMLElement paragraph = (HTMLElement)document.GetElementsByTagName("p").First();
14
15// Set the style attribute with background-color property
16paragraph.Style.BackgroundColor = "#e5f3fd";
17
18// Save the HTML document to a file
19document.Save(Path.Combine(savePath));

Equivalent JavaScript

1<script>
2	// Find the paragraph element to set a style attribute
3	var paragraph = document.getElementsByTagName("p")[0];
4
5	// Set the style attribute with background-color property
6	paragraph.style.backgroundColor = "#e5f3fd";
7</script>

The figure illustrates the results of changing background color for the first paragraph in the HTML file using inline CSS:

HTML paragraph background color changed with inline CSS in C#

Change Background Color of the Entire Web Page

You can change the background color using inline style attribute or using internal CSS.

Change Background Color Using Inline CSS

If you want to change the color of the entire HTML document, you should use the background-color property of the style attribute in the opening tag of the <body> section.

 1// Change background color with inline CSS in C#
 2
 3// Prepare output path for document saving
 4string savePath = Path.Combine(OutputDir, "change-background-color-inline-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// Find the body element to set a style attribute
13HTMLElement body = (HTMLElement)document.GetElementsByTagName("body").First();
14
15// Set the style attribute with background-color property
16body.Style.BackgroundColor = "#e5f3fd";
17
18// Save the HTML document to a file
19document.Save(Path.Combine(savePath));

Equivalent JavaScript

1<script>
2	// Find the body element to set a style attribute
3	var body = document.getElementsByTagName("body")[0];
4
5	// Set style attribute with background-color property
6	body.style.backgroundColor = "#e5f3fd";
7</script>

Change Background Color Using Internal CSS

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

1<head>
2<style>
3	body {
4	background-color: rgb(229, 243, 253);
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 background color for a whole HTML file. Take a few steps:

  1. Load an existing HTML file.
  2. Find the <body> element and remove the background-color property from the style attribute. Note: If the background color is set using inline style attribute, this step is required because using the style attribute overrides both internal and external CSS.
  3. Create a <style> element and assign the background-color value for <body> element.
  4. Find the <head> element in your document and add the <style> element into it.
  5. Save the modified HTML document.
 1// Change background color for HTML using internal CSS - C#
 2
 3// Prepare output path for document saving
 4string savePath = Path.Combine(OutputDir, "change-background-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// Find the body element
13HTMLElement body = (HTMLElement)document.GetElementsByTagName("body").First();
14
15// Remove the background-color property from the style attribute
16body.Style.RemoveProperty("background-color");
17
18// Create a style element and assign the background-color value for body element
19Element style = document.CreateElement("style");
20style.TextContent = "body { background-color: rgb(229, 243, 253) }";
21
22// Find the document head element and append style element to the head
23Element head = document.GetElementsByTagName("head").First();
24head.AppendChild(style);
25
26// Save the HTML document
27document.Save(Path.Combine(savePath));

Equivalent JavaScript

 1<script>
 2	// Find the body element
 3	var body = document.getElementsByTagName("body")[0];
 4
 5	// Remove the background-color property from style attribute
 6	body.style.removeProperty("background-color");
 7
 8	// Create a style element and assign the background-color value for body element
 9	var style = document.createElement("style");
10	style.textContent = "body { background-color: rgb(229, 243, 253) }";
11
12	// Find the document head element and append style element to the head
13	var head = document.getElementsByTagName("head")[0];
14	head.appendChild(style);
15</script>

The figure shows two fragments of the HTML file before (a) and after (b) changing the background color for the entire document:

HTML document before and after changing the page background color in C#

FAQ

How do I change the background color of one element?

Find the target element and set HTMLElement.Style.BackgroundColor. This writes the background-color value to the element’s inline style.

How do I change the background color of the whole page?

Set background-color on the <body> element, either with inline CSS or with an internal CSS rule added to the document’s <head>.

Why remove the inline background color before adding internal CSS?

Inline CSS has higher priority than internal and external CSS. If the <body> already has an inline background-color, remove it before relying on a <style> rule.

Related Articles