Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.
To add an image to HTML in Java, create or load an
HTMLDocument, create an <img> element with createElement("img"), set its src and alt attributes, append it to the required DOM element, and call save() to write the updated HTML.
Use an HTML <img> element when an image is part of the document content. Use the CSS background-image property when the image is decorative or belongs to the page layout. Aspose.HTML for Java provides DOM methods for both workflows.
This article shows how to add an image to a new HTML document, insert one into an existing file, and create a CSS background image.
An <img> element needs a src attribute that identifies the image resource. Meaningful images also need concise alt text. The optional width and height attributes reserve layout space, while CSS can control responsive rendering.
The following example creates an empty document, adds a responsive image to its <body>, and saves the result as add-image.html.
HTMLDocument.<img> element with
createElement(). 1import com.aspose.html.HTMLDocument;
2import com.aspose.html.dom.Element;
3
4String outputPath = "add-image.html";
5
6HTMLDocument document = new HTMLDocument();
7Element image = document.createElement("img");
8
9image.setAttribute("src", "https://docs.aspose.com/html/files/flower.svg");
10image.setAttribute("alt", "Stylized blue and pink flower");
11image.setAttribute("width", "300");
12image.setAttribute("height", "300");
13image.setAttribute("style", "max-width: 100%; height: auto;");
14
15document.getBody().appendChild(image);
16document.save(outputPath);The saved document contains an <img> element in its body. The CSS lets the image shrink when its container is narrower than 300 pixels while preserving the aspect ratio. When the remote SVG is accessible, the save(String) overload also saves the linked resource in an adjacent resource folder.
Use the same DOM operations to edit an existing page. The sample file
image-insertion.html contains a <main> element with introductory content. The example loads that file, appends an image to <main>, and saves the changed document under a new name.
image-insertion.html to the application’s working directory.HTMLDocument.<main> element.<img> element.<main> and save image-insertion-updated.html. 1import com.aspose.html.HTMLDocument;
2import com.aspose.html.dom.Element;
3
4String inputPath = "image-insertion.html";
5String outputPath = "image-insertion-updated.html";
6
7HTMLDocument document = new HTMLDocument(inputPath);
8Element main = document.querySelector("main");
9Element image = document.createElement("img");
10
11image.setAttribute("src", "https://docs.aspose.com/html/files/flower.svg");
12image.setAttribute("alt", "Stylized blue and pink flower");
13image.setAttribute("width", "240");
14image.setAttribute("height", "240");
15
16main.appendChild(image);
17document.save(outputPath);The original file remains unchanged because the example saves the edited DOM to image-insertion-updated.html.
CSS background images do not create content nodes and do not support an alt attribute. Use them for decorative images or styling. When a background conveys information, provide the same information as accessible HTML text.
The following example adds a <style> element to the document <head>. Its CSS uses one image as a non-repeating background that covers the entire page.
<style> element and assign a background-image rule to it.background-repeat: no-repeat and background-size: cover.<head>, append the style element, and save background-image.html. 1import com.aspose.html.HTMLDocument;
2import com.aspose.html.dom.Element;
3
4String inputPath = "image-insertion.html";
5String outputPath = "background-image.html";
6
7HTMLDocument document = new HTMLDocument(inputPath);
8Element style = document.createElement("style");
9
10style.setTextContent(
11 "body {"
12 + " background-image: url('https://docs.aspose.com/html/files/iceland-1.png');"
13 + " background-repeat: no-repeat;"
14 + " background-position: top right;"
15 + " background-size: cover;"
16 + "}"
17);
18
19Element head = document.querySelector("head");
20head.appendChild(style);
21document.save(outputPath);The output uses one background image scaled to cover the page. The image keeps its aspect ratio, so cover can crop part of it when the page and image have different proportions.
Internal CSS keeps reusable rules in the document <head>. Inline CSS is convenient for styling one element but can become harder to maintain when repeated across a page.
The src attribute and CSS url() value can contain an absolute URL or a relative reference.
https://example.com/images/photo.jpg identifies the resource independently of the HTML file location.images/photo.jpg is resolved against the document’s base URL.HTMLDocument constructor if the markup contains relative resource paths.| Requirement | Use |
|---|---|
| The image conveys content or performs a function | An <img> element with appropriate alt text |
| The image is decorative or part of the layout | CSS background-image |
| The image should be discovered as document content | An <img> element |
| The same decoration applies to several elements | A reusable CSS rule |
| Issue | Cause and fix |
|---|---|
| The image is broken | Check the src value, base URL, file location, network access, and letter case in the path. |
| A relative image path resolves incorrectly | Load the file from its actual location or provide the correct base URL when creating the document from HTML code. |
| The image distorts when resized | Preserve its aspect ratio. Set accurate width and height attributes and use CSS such as max-width: 100%; height: auto;. |
| A meaningful image has no accessible description | Add an alt attribute that communicates the image’s purpose in context. Preserve alt="" for intentionally decorative <img> elements. |
| The background image repeats | Add background-repeat: no-repeat, or deliberately configure repeat, repeat-x, or repeat-y. |
| Changes are not present in the source file | DOM changes remain in memory until save() is called. Confirm which output path the application saved. |
Create an element with document.createElement("img"), assign its src and alt attributes with setAttribute(), append it to the required parent node, and save the document.
Yes. Use a relative reference such as images/photo.jpg or an appropriate file URL. Make sure the document base URL and saved folder structure allow the resource to be resolved.
Yes. Set src to an absolute HTTP or HTTPS URL. The save(String) overload can copy an accessible linked resource into an adjacent resource folder. Use HTMLSaveOptions when the application needs a different resource-handling policy.
Use CSS such as max-width: 100%; height: auto;. Accurate width and height attributes can still be retained to establish the aspect ratio and reserve layout space.
Yes. Set src to a valid data URI such as data:image/png;base64,.... Embedding removes the separate image request but increases the size of the HTML document.
CSS backgrounds cannot have an alt attribute. Use them for decoration. If a background communicates important information, provide an accessible text equivalent in the HTML content.
<img> elements without alt attributes and add reviewed descriptions.Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.