Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.
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.
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.
text-color.html to the application’s working directory.HTMLDocument.<p> element with
querySelector().style attribute to color: #8B0000; with
setAttribute(). 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.
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.
<style> element with
createElement().p { color: #8B0000; } rule as its text content.<style> element to the document <head>.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.
The CSS color property accepts several value formats. Choose one format consistently within a project.
| Format | Example |
|---|---|
| Named color | color: darkred; |
| HEX | color: #8B0000; |
| RGB | color: rgb(139, 0, 0); |
| HSL | color: hsl(0, 100%, 27%); |
| RGB with alpha | color: rgba(139, 0, 0, 0.8); |
| HSL with alpha | color: 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.
| Requirement | Recommended approach |
|---|---|
| Change one specific element | Inline style attribute |
| Apply one rule to multiple elements | Internal <style> element |
| Reuse styles across several HTML files | External stylesheet |
| Keep content and presentation easier to maintain | Internal 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.
| Issue | Cause and fix |
|---|---|
| The text color does not change | Confirm that the selector matches the intended element and that another declaration does not override the rule. |
| Only one paragraph changes | querySelector("p") returns the first matching element. Use an internal p rule or querySelectorAll("p") to target every paragraph. |
| Existing inline styles disappear | Replacing 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 color | The 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 read | The foreground and background colors have insufficient contrast. Select a more accessible color combination and validate it. |
| Changes are missing from the file | DOM changes are in memory until document.save() is called. Verify the output path and open the saved file. |
Select the element and set its inline CSS, for example element.setAttribute("style", "color: #8B0000;"). Then save the modified HTMLDocument.
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.
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.
No. The <font> element and its color attribute are obsolete in HTML. Use the CSS color property through inline, internal, or external CSS.
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.
No. color controls the foreground text color. Use background-color to change the background behind the text or element.
Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.