Change HTML Text Color in Java

To change HTML text color in Java, load an HTMLDocument, select the target element, set the CSS color property through its style attribute or an internal <style> element, and call save() to write the updated HTML.

Use inline CSS when one particular element needs a different text color. Use an internal stylesheet when a selector should apply the same rule to several elements. Both approaches modify the HTML DOM before the document is saved or converted.

CSS accepts text colors in formats such as named colors, HEX, RGB, HSL, and color values with alpha transparency. The examples use #8B0000, the HEX value for dark red.

Change Text Color Using Inline CSS

Inline CSS stores declarations in an element’s style attribute. The example uses text-color.html, selects the first paragraph, changes only that paragraph to dark red, and saves the result as text-color-inline.html.

  1. Download text-color.html to the application’s working directory.
  2. Load the HTML file into an HTMLDocument.
  3. Select the first <p> element with querySelector().
  4. Set its style attribute to color: #8B0000; with setAttribute().
  5. Save the modified document under a new file name.
 1import com.aspose.html.HTMLDocument;
 2import com.aspose.html.dom.Element;
 3
 4String inputPath = "text-color.html";
 5String outputPath = "text-color-inline.html";
 6
 7HTMLDocument document = new HTMLDocument(inputPath);
 8Element paragraph = document.querySelector("p");
 9
10paragraph.setAttribute("style", "color: #8B0000;");
11document.save(outputPath);

The output document contains this declaration on its first paragraph:

1<p style="color: #8B0000;">...</p>

Setting the complete style attribute replaces any inline declarations already stored on that element. If existing declarations must be preserved, include them in the new attribute value or update the inline style through the CSS DOM instead of replacing the attribute.

Change Text Color Using Internal CSS

Internal CSS is stored in a <style> element, normally inside the document <head>. One rule can therefore change text color for every element that matches its selector. The next example applies dark red to all paragraphs without adding a style attribute to each one.

  1. Load the source HTML file.
  2. Create a <style> element with createElement().
  3. Add the p { color: #8B0000; } rule as its text content.
  4. Append the <style> element to the document <head>.
  5. Save the result as text-color-internal.html.
 1import com.aspose.html.HTMLDocument;
 2import com.aspose.html.dom.Element;
 3
 4String inputPath = "text-color.html";
 5String outputPath = "text-color-internal.html";
 6
 7HTMLDocument document = new HTMLDocument(inputPath);
 8Element style = document.createElement("style");
 9
10style.setTextContent("p { color: #8B0000; }");
11document.querySelector("head").appendChild(style);
12
13document.save(outputPath);

In the saved file, both paragraphs match the selector and use the new text color. Change the selector to target another group, for example h1, .notice, or article p.

CSS Text Color Values

The CSS color property accepts several value formats. Choose one format consistently within a project.

FormatExample
Named colorcolor: darkred;
HEXcolor: #8B0000;
RGBcolor: rgb(139, 0, 0);
HSLcolor: hsl(0, 100%, 27%);
RGB with alphacolor: rgba(139, 0, 0, 0.8);
HSL with alphacolor: hsla(0, 100%, 27%, 0.8);

Text color is inherited by default, so setting color on a parent can affect its descendants. A descendant’s own matching CSS declaration can override the inherited value.

When choosing foreground and background colors, check that their contrast is sufficient for readable content. See Check Color Contrast in Java for the Aspose.HTML accessibility validation workflow.

Inline CSS vs. Internal CSS

RequirementRecommended approach
Change one specific elementInline style attribute
Apply one rule to multiple elementsInternal <style> element
Reuse styles across several HTML filesExternal stylesheet
Keep content and presentation easier to maintainInternal or external CSS

A normal inline declaration generally takes precedence over normal declarations from internal or external stylesheets. If an internal rule does not change the visible result, inspect the element’s inline style and other rules involved in the CSS cascade.

Common Text Color Issues

IssueCause and fix
The text color does not changeConfirm that the selector matches the intended element and that another declaration does not override the rule.
Only one paragraph changesquerySelector("p") returns the first matching element. Use an internal p rule or querySelectorAll("p") to target every paragraph.
Existing inline styles disappearReplacing the complete style attribute removes its previous declarations. Preserve the required declarations or update only the color property through the CSS DOM.
A child element keeps another colorThe child has its own matching declaration, so it does not use the inherited parent color. Inspect rules applied to the child.
Text is difficult to readThe foreground and background colors have insufficient contrast. Select a more accessible color combination and validate it.
Changes are missing from the fileDOM changes are in memory until document.save() is called. Verify the output path and open the saved file.

FAQ

How do I change the color of one HTML element in Java?

Select the element and set its inline CSS, for example element.setAttribute("style", "color: #8B0000;"). Then save the modified HTMLDocument.

How do I change the text color of all paragraphs?

Create a <style> element containing p { color: #8B0000; } and append it to the document <head>. The rule applies to every paragraph unless a more influential declaration overrides it.

How do I change the color of only part of a paragraph?

Wrap the required text in a <span> element and apply the color property to that span. This changes the selected phrase without recoloring the entire paragraph.

Should I use the HTML font color attribute?

No. The <font> element and its color attribute are obsolete in HTML. Use the CSS color property through inline, internal, or external CSS.

Why does an internal CSS color rule not work?

Check that the <style> element is inside the document, the selector matches, and another rule does not win in the CSS cascade. An inline declaration on the target element commonly causes this result.

Does the CSS color property change the background?

No. color controls the foreground text color. Use background-color to change the background behind the text or element.

Related Articles