Generate HTML Code to Display an Image
Use this HTML image tag generator to add a picture to a webpage. Enter the image address, write alternative text, and optionally set its width and height. Check the preview and copy the generated <img> markup into your page.
The tool creates HTML code for an existing image; it does not generate pictures or compress image files. You can also use Aspose.HTML for Python via .NET to create an HTML document containing the image element with the Python code below.
HTML Image Tag Generator Settings
- Source sets the
srcattribute. Enter a direct image URL or a file path relative to the HTML page, rather than the address of a webpage containing the picture. - Alt Text sets the
altattribute. Describe the image’s purpose in context. Leave it empty for a purely decorative image that adds no information. - Width and Height add optional dimensions in whole pixels. Blank fields leave the corresponding attributes out of the HTML.
Use dimensions that match the image’s proportions to avoid stretching it. Providing both dimensions in the correct ratio helps the browser reserve space before the image loads. These attributes change the display size, not the image file’s resolution or download size. See the HTML image reference for attribute details.
Create an HTML Image Element in Python
The Python example creates an
HTMLDocument
, adds an <img> element, sets its attributes, and saves the HTML document. Changes to the generator settings update the same code block.
Use an image address that will be accessible from the saved page’s location. For a relative path, keep the image in the corresponding folder when publishing the page.
import aspose.html
with aspose.html.HTMLDocument() as document:
image = document.create_element("img")
image.set_attribute("src", "./lighthouse.jpg")
image.set_attribute("alt", "Lighthouse by the sea")
document.body.append_child(image)
document.save("image.html")
Steps to Add an Image to HTML in Python
- Create an
HTMLDocumentand callcreate_element()with"img". - Set
srcandaltwithset_attribute(). Addwidthandheightattributes if you need explicit display dimensions. - Append the image element to
document.bodywithappend_child(). - Save the HTML document with
save(). Thewithstatement closes the document automatically.
HTML Image Tag Generator FAQ
1. Why is my image not showing on my website?
Check that the src address points to an image file and opens successfully. For a relative path, verify the image’s location relative to the published HTML page, including filename capitalization. An image stored only on your computer will not be available to website visitors. If you use an external image, its server may also block requests from other sites.
2. How do I make an image clickable in HTML?
Place the generated <img> element inside an <a> element and set the link’s href to the destination address. The image’s src identifies the picture; the link’s href controls where clicking it takes the visitor. Use the
HTML Hyperlink Generator
to create link markup.
3. How do I make an HTML image fit the screen on mobile devices?
Apply CSS max-width: 100% and height: auto to the image so it can shrink to fit its container while keeping its proportions. Unlike width: 100%, max-width: 100% does not force a smaller image to fill the container. This additional CSS is not generated by the tool, and it does not reduce the image file’s download size.
Install Aspose.HTML for Python via .NET
Install the aspose-html-net package from
PyPI
.
Install Aspose.HTML for Python via .NET
pip install aspose-html-net
Review the installation guide and system requirements before deployment. Request a temporary license to test the API without evaluation limitations.
Related HTML Generators for Python
Add links, tables, embedded pages, and media to your HTML documents.