srcdoc

The srcdoc attribute allows you to embed HTML content directly within an <iframe>, specifying the content to display instead of an external source.

srcdoc attribute

The srcdoc attribute in HTML is utilized with the <iframe> element to define the HTML content to be displayed within the iframe. This feature enables the direct embedding of HTML inside the iframe instead of linking to an external resource.

Syntax

index.html
<iframe srcdoc="HTML content"></iframe>

Example

Here’s an example of using the srcdoc attribute:

The iframe srcdoc attribute
index.html

<h1>The iframe srcdoc attribute</h1>

<iframe srcdoc="<p>Hello world!</p>" src="demo_iframe_srcdoc.htm">
  <p>Your browser does not support iframes.</p>
</iframe>
This attribute is intended to be used in conjunction with the <sandbox> and seamless attributes.

Applicable to

The srcdoc attribute can be used with the following element:

ElementsAttribute
<iframe>srcdoc

Key Points

  • Content: The HTML provided in the srcdoc attribute must be valid.
  • No External Resource Required: In contrast to the <src> attribute, srcdoc lets you embed HTML directly without an external URL.
  • Browser Compatibility: The srcdoc attribute is supported by most modern browsers, making it a practical choice for embedding static content.

Conclusion

The srcdoc attribute allows embedding HTML directly within an iframe, eliminating the need for an external source. It simplifies content inclusion and is supported by modern browsers. This approach is useful for static content where no external resource is necessary.