# Text not allowed in element “iframe” in this context.

> Canonical HTML version: https://rocketvalidator.com/html-validation/text-not-allowed-in-element-iframe-in-this-context
> Attribution: Rocket Validator (https://rocketvalidator.com)
> License: CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/)

The `<iframe>` element is designed to embed another browsing context (essentially a nested document) within the current page. According to the HTML specification, the content model of `<iframe>` is **nothing** — it must not contain any text nodes or child elements. Any text placed directly inside the `<iframe>` tags is invalid HTML.

Historically, some older HTML versions allowed fallback text inside `<iframe>` for browsers that didn't support iframes. This is no longer the case in modern HTML. All current browsers support `<iframe>`, so there is no need for fallback content. The W3C validator flags this as an error because text or elements between the `<iframe>` tags violate the current HTML specification.

There are two valid ways to specify the content an iframe should display:

- **`src` attribute** — Provide a URL pointing to the document you want to embed.
- **`srcdoc` attribute** — Provide inline HTML content directly as the attribute's value. The HTML is written as a string with appropriate character escaping (e.g., `&lt;` for `<` and `&quot;` for `"` if using double-quoted attributes).

If you were using text inside `<iframe>` as a description or fallback, consider using the `title` attribute instead to provide an accessible label for the embedded content.

## Examples

### ❌ Invalid: text inside the `<iframe>` element

```html
<iframe>Some content here</iframe>
```

```html
<iframe><p>This is my embedded content.</p></iframe>
```

Both of these will trigger the "Text not allowed in element `iframe` in this context" error.

### ✅ Valid: using the `src` attribute

```html
<iframe src="https://example.com" title="Example website"></iframe>
```

This loads the document at the specified URL inside the iframe.

### ✅ Valid: using the `srcdoc` attribute

```html
<iframe srcdoc="&lt;p&gt;This is my embedded content.&lt;/p&gt;" title="Inline content"></iframe>
```

This renders the inline HTML provided in the `srcdoc` attribute. Note that HTML special characters must be escaped when the attribute value is enclosed in double quotes.

### ✅ Valid: self-closing style (empty element)

```html
<iframe src="https://example.com" title="Example website"></iframe>
```

The `<iframe>` element requires a closing tag, but nothing should appear between the opening and closing tags. Keeping the element empty is the correct approach.

### ✅ Valid: adding an accessible label with `title`

If your original intent was to describe the iframe's content for users or assistive technologies, use the `title` attribute:

```html
<iframe src="/embedded-report.html" title="Monthly sales report"></iframe>
```

The `title` attribute provides a label that screen readers can announce, improving the accessibility of the embedded content.
