Videos, maps, and preview panels sometimes need more room than a page layout allows. The browser Fullscreen API lets JavaScript display one chosen element across the screen after the user activates a control, while keeping the rest of the page out of view.
Fullscreen is a browser-controlled state, not just a larger CSS box. requestFullscreen() asks to enter it, document.fullscreenElement identifies the active element, and document.exitFullscreen() restores the page. A fullscreenchange listener keeps the interface synchronized when the user leaves with Esc instead of the page's button.
An exit control belongs inside the element being expanded so it remains visible in fullscreen. Browser support and permissions can still prevent entry, so the inline panel must remain usable when fullscreen is unavailable or a request fails.
Steps to use the Fullscreen API in JavaScript:
- Create index.html in your working folder with the preview and its toggle button.
- index.html
<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>Fullscreen preview</title> <link rel="stylesheet" href="styles.css"> <script src="fullscreen.js" defer></script> </head> <body> <p>Product workspace</p> <section id="product-preview" aria-label="Product preview"> <h1>Quarterly roadmap preview</h1> <p>Review the product timeline without the surrounding page layout.</p> <button id="fullscreen-toggle" type="button" aria-pressed="false">Open fullscreen</button> <p id="fullscreen-status" role="status">Preview is inline.</p> </section> <p>The rest of the workspace stays outside the preview.</p> </body> </html>
The button and live status are inside #product-preview; the workspace paragraphs are outside it. The deferred script runs after the HTML has been parsed.
Related: Load JavaScript with defer - Create styles.css in the same folder with inline and fullscreen styles.
- styles.css
body { margin: 1.5rem; font: 1rem/1.5 system-ui, sans-serif; color: #111827; } #product-preview { box-sizing: border-box; padding: 1.5rem; border: 1px solid #cbd5e1; background: #f8fafc; } #product-preview:fullscreen { display: grid; align-content: center; padding: 2rem; overflow: auto; background: #111827; color: #f8fafc; } button { justify-self: start; padding: 0.65rem 1rem; font: inherit; cursor: pointer; }
The :fullscreen selector applies only while the element is in fullscreen mode. The dark background makes the transition visible without adding a separate state class.
- Create fullscreen.js in the same folder with feature detection and a state-change listener.
- fullscreen.js
const preview = document.querySelector("#product-preview"); const toggleButton = document.querySelector("#fullscreen-toggle"); const status = document.querySelector("#fullscreen-status"); if (!document.fullscreenEnabled || !preview.requestFullscreen) { toggleButton.disabled = true; status.textContent = "Fullscreen is not available in this browser."; } function updateFullscreenState() { const isFullscreen = document.fullscreenElement === preview; toggleButton.textContent = isFullscreen ? "Exit fullscreen" : "Open fullscreen"; toggleButton.setAttribute("aria-pressed", String(isFullscreen)); status.textContent = isFullscreen ? "Preview is fullscreen." : "Preview is inline."; } document.addEventListener("fullscreenchange", updateFullscreenState);
fullscreenchange fires on entry and exit. Reading document.fullscreenElement keeps the label, pressed state, and status tied to the browser's actual state.
Related: Select DOM elements with JavaScript - Append the button's click handler at the end of fullscreen.js.
- fullscreen.js
toggleButton.addEventListener("click", async () => { try { if (document.fullscreenElement === preview) { await document.exitFullscreen(); } else { await preview.requestFullscreen(); } } catch (error) { status.textContent = `Fullscreen failed: ${error.message}`; } });
The request runs directly from a user click, before any unrelated asynchronous work could consume the required user activation. Both methods return promises; the catch block displays a failed request without replacing the inline panel. If the page is embedded in an iframe, its parent must also permit fullscreen, for example with allow="fullscreen".
Related: Add a JavaScript event listener - Save index.html, styles.css, and fullscreen.js in your text editor.
- Open the saved index.html file in a desktop browser that supports the Fullscreen API.

- Click Open fullscreen inside the preview panel.
A disabled button with “Fullscreen is not available in this browser.” means the feature check failed. A “Fullscreen failed” message means the browser rejected the request.
- Click Exit fullscreen inside the expanded preview.
The panel fills the screen with a dark background, the status reads “Preview is fullscreen.”, and the surrounding workspace is hidden. Browser tabs and the address bar also disappear while fullscreen is active.
- Click Open fullscreen again after the inline layout returns.

- Press Esc to leave fullscreen through the browser.

- Confirm that the panel returns inline with Open fullscreen and Preview is inline. restored.
The surrounding workspace and browser controls reappear. The reset label and status show that fullscreenchange handled the keyboard exit as well as the button exit.