Change HTML Border Color in Python

To change an HTML border color in Python, load the file with HTMLDocument, select the target element, define a visible border-style and the required border-color with inline or internal CSS, and save the updated document.

The CSS border-color property controls color but does not create a visible border by itself. Define a style such as solid and, when necessary, a border width. Border colors can use named, HEX, RGB, RGBA, HSL, or HSLA values.

This article demonstrates one border color, four side-specific colors, reusable internal CSS, and two ways to change an HTML table border.

Change Border Color Using Inline CSS

Inline CSS stores declarations directly in an element’s style attribute. The first example loads file.html, selects its first <h1> element, sets a solid rgb(220, 30, 100) border and dark-red text, and saves change-border-color.html.

  1. Load the source HTML into an HTMLDocument.
  2. Select the element whose border must change.
  3. Define a visible border style and the required border color through inline CSS.
  4. Save the modified HTML document.
 1# Change H1 border color in HTML in Python
 2
 3import os
 4import aspose.html as ah
 5
 6# Prepare input and output paths
 7output_dir = "output"
 8os.makedirs(output_dir, exist_ok=True)
 9save_path = os.path.join(output_dir, "change-border-color.html")
10
11data_dir = "data"
12document_path = os.path.join(data_dir, "file.html")
13
14# Change the heading text and border colors
15with ah.HTMLDocument(document_path) as document:
16    header = document.get_elements_by_tag_name("h1")[0]
17
18    # Set inline CSS properties
19    header.set_attribute("style", "color: #8B0000; border-style: solid; border-color: rgb(220,30,100);")
20
21    document.save(save_path)

The image compares the source document a) and the saved document b). The output heading has both a new border color and a new text color because the example sets both CSS properties.

HTML heading before and after changing its text and border colors in Python

Calling set_attribute("style", value) replaces the complete inline style. Preserve any existing declarations that the element still needs, or update only the required CSS properties.

Set Different Colors for Four Border Sides

The border-color shorthand accepts one to four values. With four values, the order is top, right, bottom, and left.

Number of valuesApplied to
OneAll four sides
TwoTop and bottom; right and left
ThreeTop; right and left; bottom
FourTop; right; bottom; left

The next example loads file.html, selects the first heading, assigns red, blue, green, and gray to its four border sides, and saves change-four-border-color.html.

  1. Load the source HTML into an HTMLDocument.
  2. Select the element whose border sides require different colors.
  3. Define a visible border style and supply four border-color values in top, right, bottom, and left order.
  4. Save the modified HTML document.
 1# Change CSS border colors for all four sides in Python
 2
 3import os
 4import aspose.html as ah
 5
 6# Prepare input and output 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# Change the border color on each side of the heading
14with ah.HTMLDocument(document_path) as document:
15    header = document.get_elements_by_tag_name("h1")[0]
16
17    # Set inline CSS for border style and different border colors on each side
18    header.set_attribute("style", "border-style: solid; border-color: red blue green gray;")
19
20    document.save(save_path)

The output heading has a red top border, blue right border, green bottom border, and gray left border:

HTML heading with a different color on each border side

Change Border Color Using Internal CSS

Internal CSS places reusable rules in a <style> element inside the document <head>. The following rule gives every paragraph a solid rgb(220, 30, 100) border:

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

The next example adds this rule to file.html and saves change-border-color-internal-css.html.

  1. Load the source HTML into an HTMLDocument.
  2. Create a <style> element with create_element().
  3. Define a CSS rule that selects the required elements and assigns their border style and color.
  4. Append the new <style> element to the document <head>.
  5. Save the modified HTML document.
 1# Change HTML border color with internal CSS in Python
 2
 3import os
 4import aspose.html as ah
 5
 6# Prepare input and output 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# Add an internal CSS rule for paragraph borders
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    head = document.get_elements_by_tag_name("head")[0]
20    head.append_child(style)
21
22    document.save(save_path)

The internal rule applies the same border to both paragraphs. A normal inline declaration on a paragraph can take precedence over this normal internal rule.

All HTML paragraphs styled with the same border color using internal CSS

Change HTML Table Border Color

The source table.html already defines a visible table grid with the legacy border and bordercolor attributes. Both examples select only the <table> element and change its border color. With this source markup, the saved document displays the complete table grid in blue.

For new HTML, prefer CSS instead of legacy presentational attributes. If a table does not already define cell borders, apply CSS rules to table, th, and td when both the outer frame and individual cell borders must be visible.

Change the Table Border Using Inline CSS

The inline example loads table.html, finds the first table with query_selector(), sets the shorthand border: 2px #0000ff solid, and saves change-table-border-color-inline-css.html.

  1. Load the HTML document containing the table.
  2. Select the table that requires a different border.
  3. Set the required border width, style, and color through inline CSS.
  4. Save the modified HTML document.
 1# Change table border color with inline CSS in Python
 2
 3import os
 4import aspose.html as ah
 5
 6# Prepare input and output paths
 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
11data_dir = "data"
12document_path = os.path.join(data_dir, "table.html")
13
14# Change the first table border color
15with ah.HTMLDocument(document_path) as document:
16    # Create a CSS selector to select the first table element
17    element = document.query_selector("table")
18
19    # Set inline style for the selected element
20    element.set_attribute("style", "border: 2px #0000ff solid;")
21
22    document.save(save_path)

Change the Table Border Using Internal CSS

The internal CSS example creates a <style> element containing a rule for table, appends it to <head>, and saves change-table-border-color-internal-css.html. It produces the same blue table grid without writing an inline declaration on the table.

  1. Load the HTML document containing the table.
  2. Create a <style> element and define the required table border rule.
  3. Append the style element to the document <head>.
  4. Save the modified HTML document.
 1# Change table border color with internal CSS in Python
 2
 3import os
 4import aspose.html as ah
 5
 6# Prepare 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# Add an internal CSS rule for the table border
14with ah.HTMLDocument(document_path) as document:
15    # Create a <style> element and set CSS for the <table> element
16    style = document.create_element("style")
17    style.text_content = "table { border-style:solid; border-color:rgb(0, 0, 255) }"
18
19    head = document.get_elements_by_tag_name("head")[0]
20    head.append_child(style)
21
22    document.save(save_path)

The image compares the source table a) and the blue table grid after the inline CSS update b):

HTML table before and after changing its border lines to blue

Common Border Color Issues

IssueCause and recommended action
The border color is not visibleborder-color does not create a border. Set a visible border-style and, when needed, an appropriate width.
Internal CSS does not change the borderAn inline declaration or another rule may take precedence. Inspect the target element and matching CSS rules.
Existing inline styles disappearReplacing the complete style attribute removes its previous declarations. Preserve them or update individual CSS properties.
Four colors appear on unexpected sidesUse the clockwise order top, right, bottom, and left.
Changes are missing from the fileDOM changes remain in memory until save() is called. Verify the output path and open the saved file.

FAQ

Why does border-color not create a visible border?

border-color controls only color. Define a visible border-style, such as solid, and set a suitable border-width when the default width is not appropriate.

Should I use the border shorthand or separate properties?

Use a declaration such as border: 1px solid #000 when width, style, and color can be set together. Use separate properties when application code must update only one border characteristic or side.

How do I set different colors for each border side?

Supply four border-color values in top, right, bottom, and left order, or set properties such as border-top-color and border-left-color individually.

What color is used when border-color is omitted?

The initial border color is currentColor, which uses the element’s computed foreground color. A visible border style is still required.

How do I remove a border?

Set border: none or border-style: none. Avoid clearing the complete style attribute unless every inline declaration should be removed.

Related Articles

Other Platforms

HTML Table Generator