How to Change Border Color in Python

Programmatically changing the border color of HTML elements is a common task in automated document processing and styling. Using Aspose.HTML for Python via .NET, you can easily load an HTML document, apply CSS rules, and save the modified version without manually editing the source files.

This article explains how to change the border color of HTML elements in Python using the CSS border-color property with inline or internal styles. Color values may be specified as the standard color names or with HEX, RGB, RGBA, HSL, and HSLA values.

Change Border Color Using Inline CSS

You can set or change the border color using inline style attribute. The border-color property works only when a border-style is defined, as it controls the visibility of the border. Without it, the border color won’t appear and may inherit the element’s text color. The property supports color Names, RGB, RGBA, HEX, HSL, and HSLA values. To change border color for HTML element using Aspose.HTML for Python via .NET you should follow a few steps:

  1. Load an existing HTML file and create an instance of an HTML document.
  2. Determine which element you want to change the border color for and find this element to set a style attribute for it. Use the get_elements_by_tag_name(name) method of the Element class that returns HTML element with a given tag name.
  3. Use the set_attribute() method to set the style attribute with border-style and border-color properties.
  4. 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 Python example shows the border color change for the <h1> element:

 1# Сhange border color for <h1> element using Python
 2
 3import os
 4import aspose.html as ah
 5
 6# Prepare output directory and path for saving
 7output_dir = "output"
 8os.makedirs(output_dir, exist_ok=True)
 9save_path = os.path.join(output_dir, "change-border-color.html")
10
11# Prepare path to source HTML file
12data_dir = "data"
13document_path = os.path.join(data_dir, "file.html")
14
15# Create an instance of an HTML document
16with ah.HTMLDocument(document_path) as document:
17    # Find the first <h1> element
18    header = document.get_elements_by_tag_name("h1")[0]
19
20    # Set inline CSS properties
21    header.set_attribute("style", "color: #8B0000; border-style: solid; border-color: rgb(220,30,100);")
22
23    # Save the modified HTML document
24    document.save(save_path)

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).

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

Note: border-style is required. border-color by itself will not display anything unless the element has a border-style (e.g., solid, dashed, or double). Always set a border-style (or use shorthand border: 1px solid #000;) to make the border visible.

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.

 1# Change color for four sides of the border using Python
 2
 3import os
 4import aspose.html as ah
 5
 6# Setup directories and define paths
 7data_dir = "data"
 8output_dir = "output"
 9os.makedirs(output_dir, exist_ok=True)
10document_path = os.path.join(data_dir, "file.html")
11save_path = os.path.join(output_dir, "change-four-border-color.html")
12
13# Create an instance of an HTML document
14with ah.HTMLDocument(document_path) as document:
15    # Find the first <h1> element
16    header = document.get_elements_by_tag_name("h1")[0]
17
18    # Set inline CSS for border style and different border colors on each side
19    header.set_attribute("style", "border-style: solid; border-color: red blue green gray;")
20
21    # Save the modified HTML document
22    document.save(save_path)
23
24print("File saved to:", save_path)

Note: Order of border-color values. One value applies to all sides, two values → top/bottom and left/right, three values → top, left/right, bottom, four values → top, right, bottom, left.

The figure illustrates the results of changing the border color for the <h1> element using inline CSS – changed the color for each of the four sides of the border.

Text “Rendered the change-four-border-color.html file with changed border color for ”

Change Border Color Using Internal CSS

The border colorization can be achieved using internal CSS, as shown in the following HTML code example. The styles will be applied to all paragraph elements in the document:

1<head>
2<style>
3	p {
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 Python example demonstrates how to realize internal CSS to change border color for all <p> elements. Take a few steps:

  1. Create an instance of an HTML document.
  2. Create a <style> element and assign the border-style and border-color values for <p> element.
  3. Use the get_elements_by_tag_name() method to find the <head> element in your document and add the <style> element into it.
  4. Save the modified HTML document.
 1# Change border color for HTML using internal CSS in Python
 2
 3import os
 4import aspose.html as ah
 5
 6# Setup directories and define paths
 7data_dir = "data"
 8output_dir = "output"
 9os.makedirs(output_dir, exist_ok=True)
10document_path = os.path.join(data_dir, "file.html")
11save_path = os.path.join(output_dir, "change-border-color-internal-css.html")
12
13# Create an instance of an HTML document
14with ah.HTMLDocument(document_path) as document:
15    # Create a <style> element and set CSS for <p> elements
16    style = document.create_element("style")
17    style.text_content = "p {border-style: solid; border-color: rgb(220,30,100); }"
18
19    # Find the <head> element and append the <style> element
20    head = document.get_elements_by_tag_name("head")[0]
21    head.append_child(style)
22
23    # Save the modified HTML document
24    document.save(save_path)

Text “Rendered the change-border-color-internal-css.html file with color borders for all paragraphs”

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. To change the table border color using inline CSS style attribute you can use the query_selector() method to navigate DOM and find the <table> element. Then set the style attribute with the required properties for the <table> element:

Change Table Border Color Using Inline CSS in Python

 1# Change table border color using Python
 2
 3import os
 4import aspose.html as ah
 5
 6# Prepare an output path for saving the document
 7output_dir = "output"
 8os.makedirs(output_dir, exist_ok=True)
 9save_path = os.path.join(output_dir, "change-table-border-color-inline-css.html")
10
11# Prepare a path to the source HTML file
12data_dir = "data"
13document_path = os.path.join(data_dir, "table.html")
14
15# Create an instance of an HTML document
16with ah.HTMLDocument(document_path) as doc:
17    # Create a CSS selector to select the first table element
18    element = doc.query_selector("table")
19
20    # Set inline style for the selected element
21    element.set_attribute("style", "border: 2px #0000ff solid;")
22
23    # Save the modified HTML document to a file
24    doc.save(save_path)

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

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

Change Table Border Color Using Internal CSS

 1# Change table border color using internal CSS in Python
 2
 3import os
 4import aspose.html as ah
 5
 6# Prepare directories and input and output paths
 7output_dir = "output"
 8os.makedirs(output_dir, exist_ok=True)
 9data_dir = "data"
10document_path = os.path.join(data_dir, "table.html")
11save_path = os.path.join(output_dir, "change-table-border-color-internal-css.html")
12
13# Create an instance of an HTML document
14with ah.HTMLDocument(document_path) as doc:
15    # Create a <style> element and set CSS for the <table> element
16    style = doc.create_element("style")
17    style.text_content = "table { border-style:solid; border-color:rgb(0, 0, 255) }"
18
19    # Find the <head> element and append the <style> element
20    head = doc.get_elements_by_tag_name("head")[0]
21    head.append_child(style)
22
23    # Save the modified HTML document to a file
24    doc.save(save_path)

All methods for setting/changing border color are based on the same basic workflow:

  1. Load the source HTML with ah.HTMLDocument.
  2. Locate the target element(s) (create_element, get_elements_by_tag_name, query_selector).
  3. Set the desired CSS (style.text_content or set_attribute("style", …)).
  4. Save the modified document.

FAQ

1. What is the best way to set the border color – using border: 1px solid #000; or border-style and border-color separately?
Both methods work, but they serve different purposes. Using the shorthand border: 1px solid #000; is the simplest and safest option, as it defines the border width, style, and color at once – ensuring the border is visible. However, if you need to modify styles programmatically, setting border-style and border-color separately gives you more flexibility.

2. Why doesn’t my border-color appear even after setting it?
The border-color property only works with a border-style. Without the border-style (such as solid, dashed, or dotted), the border will be completely hidden. Always set the style first or use the shorthand border property to define all settings at once.

3. How to style multiple elements at once?
To achieve consistent results across multiple elements, use an internal <style> block in the <head> or assign a class to the elements and define the styles once. Inline styles work for individual elements, but internal CSS is better suited for tables or multiple similar tags.

4. How do I remove or reset a border color in HTML?
You can remove a border by setting border: none; or border-style: none;. If you want to reset only the color, use border-color: initial; or clear inline styles by setting set_attribute("style", "").

See Also

  • How to change text color in HTML? – In this article, you will learn how to change color of text in paragraphs, headers, etc., using Aspose.HTML for Python via .NET.
  • How to Safely Load and Convert HTML in Python – Sandboxing – In this article, you will learn how to securely load and convert untrusted HTML in Python using Aspose.HTML for Python via .NET. Includes sandbox examples to block scripts and external images.
  • How to change background color? – In this article, you will explore how to change background color for text in paragraphs or entire pages using Aspose.HTML for Python via .NET.
  • HTML Color Codes – In this article, you will find information about HTML color codes for your website – HTML color names, HEX color codes, RGB, RGBA, HSL and HSLA values.

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 “HTML Table Generator”

Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.