A page can display a document that is maintained separately, such as a visitor center's opening hours. An HTML iframe loads that document inside the parent page.
A descriptive label helps screen reader users decide whether to enter the frame. The iframe's title attribute labels the frame, while the embedded document keeps its own <title> element for the page title.
A page can open in its own tab and still refuse to load inside a frame. Response headers such as X-Frame-Options and Content Security Policy's frame-ancestors directive let a site restrict where its pages can be embedded. Some sites provide a separate URL for embedding.
Steps to embed an iframe in HTML:
- Save the document as hours.html in an unused test folder served by your local web server.
- Enter the parent page markup in a second document in your text editor.
<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width"> <title>Visitor information</title> </head> <body> <h1>Visitor information</h1> <iframe src="hours.html" title="Opening hours" width="320" height="140"> </iframe> </body> </html>
The relative src URL points to hours.html in the same folder. The title describes the frame's contents. The width and height set its initial dimensions in CSS pixels.
- Save the parent document as visitor-info.html alongside hours.html.
- Open your local web server's URL for visitor-info.html in the browser.
The address is http://localhost:8000/visitor-info.html if the server listens on port 8000 and serves the test folder at its root.
- Verify that Opening hours appears inside the frame below Visitor information.
The address bar still shows visitor-info.html. A blank frame or an error in the frame means the embedded page did not load as intended.