Removing HTML tags from an image depends on what you are trying to change. HTML tags are not stored inside an image file. Instead, they are used in a webpage’s code to display, format, link, or position an image.
For example, an image may be surrounded by HTML elements such as <figure>, <div>, or <a>. You might want to remove these additional elements while keeping the image visible. Alternatively, you may want to extract the image URL from an HTML string, remove all HTML markup from text, or clean an image element’s attributes.
The appropriate method depends on the result you need. You can use HTML editing, JavaScript, PHP, or an HTML parser to handle these tasks. This guide explains how each method works and how to avoid accidentally removing the image itself.
Can You Remove HTML Tags From an Image?
You cannot directly remove HTML tags from an image file because the image and its HTML markup are separate things.
An image file, such as JPG, PNG, or WebP, contains visual information. HTML defines how that image appears on a webpage.
For example, consider this HTML code:
<figure class="image-container">
<a href="large-image.jpg">
<img src="photo.jpg" alt="A beautiful mountain landscape">
</a>
<figcaption>Mountain landscape during sunrise</figcaption>
</figure>
This code contains several HTML elements:
<figure>creates a container for the image and its caption.<a>makes the image clickable.<img>displays the actual image.<figcaption>displays a caption.
If you want to remove the surrounding HTML elements but keep the image visible, you need to retain the <img> element.
The cleaned HTML would look like this:
<img src="photo.jpg" alt="A beautiful mountain landscape">
The image still appears on the webpage, but its clickable link, surrounding container, and caption have been removed.
It is important to understand that removing the <img> element itself will also remove the image from that location on the webpage.
How to Remove Surrounding HTML Tags While Keeping an Image
If an image is surrounded by unnecessary HTML elements, you can remove those elements manually by editing the webpage’s HTML.
For example, suppose your existing code looks like this:
<div class="image-wrapper">
<figure>
<a href="example-large.jpg">
<img src="example.jpg" alt="Website development">
</a>
</figure>
</div>
To remove the surrounding tags, keep the image element and delete the additional containers:
<img src="example.jpg" alt="Website development">
This method is useful when you want to remove unnecessary wrappers, hyperlinks, or formatting elements without removing the image.
However, check whether the surrounding elements provide any important functionality before deleting them. For example, removing an anchor tag removes the clickable link, while removing a figure caption removes its visible description.
What Happens When You Remove the Image Tag?
The <img> element is responsible for embedding an image into an HTML document.
For example:
<img src="example.jpg" alt="Example image">
The browser reads the image location from the src attribute and displays the corresponding image.
Removing this entire element leaves the browser without that image reference.
Therefore, when editing HTML, distinguish between the image element and its surrounding containers. Remove only the elements you no longer need.
How to Remove HTML Tags From an Image Using JavaScript
JavaScript provides an HTML parser that can read HTML strings and identify individual elements. This is useful when you want to extract an image from a larger block of HTML without keeping the surrounding markup.
The DOMParser interface can convert an HTML string into a document that JavaScript can inspect.
For example, consider this HTML string:
<figure>
<a href="large-photo.jpg">
<img src="photo.jpg" alt="A mountain">
</a>
<figcaption>Mountain photography</figcaption>
</figure>
You can extract the image element using the following code:
const html = `
<figure>
<a href="large-photo.jpg">
<img src="photo.jpg" alt="A mountain">
</a>
<figcaption>Mountain photography</figcaption>
</figure>
`;
const parser = new DOMParser();
const doc = parser.parseFromString(html, "text/html");
const image = doc.querySelector("img");
const cleanImage = image ? image.outerHTML : "";
console.log(cleanImage);
The resulting output is:
<img src="photo.jpg" alt="A mountain">
This approach extracts the first image element and retains its original attributes, including its source and alternative text.
You can also extract the image URL separately:
const imageUrl = image?.getAttribute("src") ?? "";
console.log(imageUrl);
The output will be:
photo.jpg
This is particularly useful when processing HTML content from a database, content management system, or website and collecting image URLs without retaining unnecessary markup.
If the HTML contains multiple images, use querySelectorAll("img") instead of querySelector("img") to select all image elements.
How to Remove All HTML Tags From Text Using JavaScript
Sometimes the requirement is not to preserve the image. Instead, you may have an HTML string containing text, formatting tags, and image elements, and you want to extract only the readable text.
For example:
<p>Welcome to our website.</p>
<strong>Website development services</strong>
<img src="photo.jpg" alt="Website image">
You can extract the plain text using JavaScript:
function removeHtmlTags(html) {
const parser = new DOMParser();
const doc = parser.parseFromString(html, "text/html");
doc.querySelectorAll("script, style").forEach(element => {
element.remove();
});
return doc.body.textContent ?? "";
}
const html = `
<p>Welcome to our website.</p>
<strong>Website development services</strong>
<img src="photo.jpg" alt="Website image">
`;
console.log(removeHtmlTags(html));
Output:
Welcome to our website.
Website development services
The method removes the HTML structure and extracts the remaining text.
Notice that the image is not included in the output. Its alt attribute is not automatically included in the element’s textContent.
If you also need image descriptions, extract the alt attributes separately before processing the HTML.
How to Remove HTML Tags Using PHP
PHP provides a built-in function called strip_tags() that removes HTML and PHP tags from a string.
It is useful when processing HTML content on a PHP-based website, including applications built with WordPress.
For example:
<?php
$html = '<p>Welcome to our website.</p>
<strong>Website development services</strong>';
$clean_text = strip_tags($html);
echo $clean_text;
?>
Output:
Welcome to our website.
Website development services
The function removes the HTML elements while preserving the text between them.
How to Keep Image Tags While Removing Other HTML Tags
PHP also allows you to specify which HTML elements should remain in the output.
For example:
<?php
$html = '<div>
<p>Our website image</p>
<img src="photo.jpg" alt="Website image">
</div>';
$clean_html = strip_tags($html, '<img>');
echo $clean_html;
?>
The output retains the image element while removing other HTML tags.
This can be useful when you want to preserve images but remove surrounding formatting markup.
However, there is an important limitation: allowing an HTML tag does not automatically clean its attributes. For example, strip_tags() does not remove potentially unsafe event-handler attributes from an allowed image element.
Consequently, do not treat this function as an HTML security sanitizer when processing content submitted by untrusted users. Use an appropriate HTML sanitization library and an explicit allowlist of permitted elements and attributes for that purpose.
How to Remove HTML Tags From an Image in WordPress
WordPress websites often generate additional HTML markup around images when publishing content through the block editor.
For example, an image block may contain a figure element, classes, captions, and other attributes.
A typical structure looks like this:
<figure class="wp-block-image">
<img
src="example.jpg"
alt="Website design"
class="wp-image-123"
>
</figure>
If you want to remove the surrounding figure element while keeping the image, the simplified markup is:
<img src="example.jpg" alt="Website design">
You can make the required changes through the WordPress editor or by editing the relevant HTML through its code editor.
Before making changes, consider the following:
- Keep the image’s
srcattribute so the browser can load the correct file. - Preserve meaningful
alttext to describe the image. - Remove wrapper classes only when they are no longer required for styling.
- Check whether the image block contains a caption or hyperlink that should remain.
- Preview the updated page before publishing your changes.
Avoid removing WordPress block-comment delimiters or modifying serialized block markup without understanding its structure. Doing so can cause WordPress to report that a block contains unexpected or invalid content.
If you are modifying multiple images through custom code, use an HTML parser rather than applying broad text replacements to the entire page.
Can You Remove HTML Tags Without Removing the Image URL?
Yes. You can extract the image URL from an HTML element without retaining the complete HTML markup.
For example:
<img src="https://example.com/images/photo.webp"
alt="Nature photography"
width="800">
The image source is stored in the src attribute.
Using JavaScript, you can retrieve it as follows:
const image = document.querySelector("img");
const imageURL = image?.getAttribute("src") ?? "";
console.log(imageURL);
This returns:
https://example.com/images/photo.webp
If the image uses responsive image attributes such as srcset, you may also need to retrieve those values. The browser can use srcset and sizes to select an appropriate image resource for different display conditions.
The important distinction is that extracting an image URL does not require keeping the complete <img> element.
Common Mistakes When Removing HTML Tags From Images
Removing HTML markup is relatively straightforward, but using the wrong approach can produce unexpected results.
Removing the entire image element: If you delete the <img> element, the browser will no longer display that image at the original location. Keep it when the image needs to remain visible.
Using regular expressions for complex HTML: A simple regular expression may work for a predictable text pattern, but HTML can contain quoted attributes, comments, nested elements, and malformed markup. A proper HTML parser is generally more reliable.
Deleting useful image attributes: Removing src, srcset, or meaningful alt text can affect image loading, responsive display, or accessibility. Remove only attributes that are genuinely unnecessary.
Using strip_tags() as a security solution: Removing HTML tags does not guarantee that the remaining content is safe. PHP’s strip_tags() is a text-processing function, not a complete HTML validation or sanitization solution.
Confusing image markup with image content: If an HTML code snippet is visibly printed inside a JPG or PNG image, removing HTML elements from the webpage will not erase those pixels. You need an image editor to modify the actual visual content.
Frequently Asked Questions
Can I remove HTML tags from an image without deleting the image?
Yes. You can remove the surrounding HTML elements while retaining the <img> element. Keep the image’s source attribute so the browser can continue displaying it.
Does removing the <img> tag delete the original image file?
No. Removing the <img> element from a webpage does not delete the original image stored on your server or in your media library. It only removes that image from the corresponding location in the webpage’s markup.
Which method is best for removing HTML tags from an image?
It depends on your requirement. Manual HTML editing works for individual images, JavaScript with DOMParser is useful for browser-based processing, and PHP’s strip_tags() can remove markup from server-side strings. Use an HTML sanitizer when cleaning untrusted HTML that must retain selected elements.
Can I remove HTML tags but keep the image’s alt text?
Yes. You can retrieve the alt attribute separately before removing the HTML elements. The alt attribute provides a textual description of an image and is useful for accessibility.
Can HTML tags be removed from a JPG or PNG file?
HTML tags are not part of the internal image data in a standard JPG or PNG file. If you can see HTML-like text within the image, it is part of the visual pixels and needs to be edited using an image-editing application.
Conclusion
Removing HTML tags from an image requires understanding whether you want to change the webpage markup, extract an image URL, clean an HTML string, or modify the actual image.
When you want to keep an image visible, retain its <img> element and remove only the unnecessary surrounding elements. JavaScript’s DOMParser provides a practical way to identify and extract image elements, while PHP’s strip_tags() is useful for basic server-side text processing.
For more complex content or user-submitted HTML, use a proper parser and an HTML sanitizer rather than relying on regular expressions or basic tag removal. Choosing the correct method helps you clean HTML without accidentally losing images, useful attributes, or important functionality.
Explore More Relevant Guides
How To Center An Image In Html
Can I Use Html With The Traffic Pass Safelist App
How To Add Html To Lioden Profile
How To Add Html Signature To Apple Mail
How To Add Textures In Html Games
How Do You Indent In Html
How Long Does It Take To Learn Html
How To Convert Html To Excel
How To Embed A Google Form In Html
How To Get Html Code From A Website
How To Hide Text In Html
What Are Tags In Websites