allowfullscreen
an HTML attribute for the iframe tag

How to use allowfullscreen in an iframe?

Answers:

  • The allowfullscreen attribute in an <iframe> element is used to enable fullscreen mode for the content embedded within the iframe. To use it properly, follow these steps:

    1. Basic Syntax: When you create an <iframe>, include the allowfullscreen attribute. Here’s a basic example:
    <iframe src="https://example.com" allowfullscreen></iframe>
    
    1. Complete Example: Here’s a full example including width and height attributes:
    <iframe 
        src="https://www.youtube.com/embed/dQw4w9WgXcQ" 
        width="560" 
        height="315" 
        frameborder="0" 
        allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" 
        allowfullscreen>
    </iframe>
    
    1. Additional Attributes: It is often beneficial to use additional attributes that specify permissions for the iframe content. The allow attribute can be used to restrict or permit certain features. In the example above, some permissions are granted (e.g., autoplay, encrypted media).

    2. Browser Compatibility: The allowfullscreen attribute is widely supported across modern browsers, but make sure to test on different platforms if you are developing for a wide audience.

    3. JavaScript & Dynamic Use: If you are creating iframes dynamically using JavaScript, you can also set the allowfullscreen attribute like this:

    const iframe = document.createElement('iframe');
    iframe.src = "https://example.com";
    iframe.allowFullscreen = true; // Use 'true' in JS
    document.body.appendChild(iframe);
    

    Note

    • When content from a different source is embedded, you may need to ensure that the source itself supports fullscreen mode. For example, some video platforms may require specific parameters in the URL to enable fullscreen functionality.

    That's it! With the inclusion of the allowfullscreen attribute, your iframe should properly support fullscreen mode when requested by the user.

Related Questions: