HTML Text Color Java

HTML is a markup language used to structure web pages using different elements, tags, etc. HTML documents are usually assisted by formatting technologies like CSS. In some use cases, you may need to modify the text formatting in an HTML file. In accordance with such scenarios, this article covers how to change text color in an HTML file programmatically in Java.

HTML Text Color Formatting – Java API Installation

Aspose.HTML for Java API supports manipulating or converting different file formats including MD, XPS, SVG, etc. You can easily access the API by configuring its JAR file from the Downloads page, or pasting the following configurations in the pom.xml file of your project in order to install it from the Aspose Repository:

Repository:

 <repositories>
     <repository>
         <id>snapshots</id>
         <name>repo</name>
         <url>http://repository.aspose.com/repo/</url>
     </repository>
</repositories>

Dependency:

 <dependencies>
    <dependency>
        <groupId>com.aspose</groupId>
        <artifactId>aspose-html</artifactId>
        <version>22.9</version>
        <classifier>jdk18</classifier>
    </dependency>
</dependencies>

Change Text Color in HTML using Inline CSS using Java

The steps below elaborate on how to change text color in an HTML file using inline CSS:

  1. Load the source HTML using the HTMLDocument class.
  2. Find the first paragraph element to set a style attribute.
  3. Set the style attribute with the color property.
  4. Save the HTML document.

The following code snippet demonstrates how to change text color in HTML using inline CSS programmatically using Java:

Change Text Color in HTML using Internal CSS using Java

The following steps explain how to change text color in HTML using internal CSS using Java:

  1. Create an object of HTMLDocument class.
  2. Make a style element and set the text color value for all paragraph elements.
  3. Append style after finding the head element.
  4. Save the updated HTML file.

The following sample code elaborates on how to change text color in an HTML file using internal CSS programmatically in Java:

Explore Aspose.HTML for Java

You may visit the [documentation][7] section to learn many features included in the API.

Conclusion

In conclusion, you have learned how to change text color in an HTML document programmatically using Java. Furthermore, please feel free to reach out to us at forum in case of any queries.

See Also