MSK Institute LogoMSK.Institute
HomeCourses
Book Free Demo➔Sign In
Free Demo➔
MSK Institute LogoMSK.Institute

Leading computer training academy in Shikohabad for software engineering, Python, web development, and digital literacy.

ISO 9001:2015 Certified Center
Follow & Connect
4.9

Programs & Admissions

  • All CoursesCatalog
  • Learning PathsRoadmaps
  • Student ProjectsShowcase
  • Live Classes & ScheduleLive
  • Upcoming Batches
  • Campuses & LocationsHub
  • Verify Certificates
  • Book Free Demo Class

Resources & Tools

  • Free Online Mock TestsAll Tests
  • NIELIT CCC Mock TestFree
  • Course & Career FinderQuiz
  • Study Material Hub
  • All MSK ToolsHub
  • Keyboard ShortcutsNew
  • TypeQuest Speed LabTyping
  • MSK Code PlaygroundCode
  • Interactive Tutorials
  • Quick Cheatsheets

Institute & Contact

  • About MSK Institute
  • Careers & InternshipsHiring
  • Publications & Blogs
  • Gali No. 3, Near Gyan Jyoti Public School,
    Adarsh Nagar, Shikohabad, Uttar Pradesh 283135
  • +91 83930 42166
  • mskshikohabad@gmail.com
  • Hours:Mon – Sat: 8:00 AM – 7:00 PM | Sun: 10:00 AM – 2:00 PM
  • 4.9 ★(38 Google Reviews)
© 2026 MSK Institute. All rights reserved.•Privacy Policy•Terms of Service•Disclaimer•Contact Us

Shikohabad's Leading Computer & Coding Academy

Home
Courses
Live
Material
Fullscreen & Screen Orientation APIs0%
Study Materials
JavaScript for AdvancedProgress 0%
1.1Higher-Order Functions & Currying1.2Function Composition & Pipelining1.3Memoization & Pure Functions1.4Execution Context & Call Stack Deep Dive1.5Scope Chain & Lexical Environment
15.1History API & Client-Side Routing15.2Geolocation, Clipboard & Device APIs15.3Fullscreen & Screen Orientation APIs15.4Web Audio & MediaStream APIs Basics
HomeTutorialsJavaScript for AdvancedFullscreen & Screen Orientation APIs
Web Audio & MediaStream APIs Basics

Fullscreen & Screen Orientation APIs

Beginner12 min read•Updated: 2026-11-18•By Er. Sumit Kumar (MSK Institute)
Study Materials

For multimedia players, presentation tools, dashboards, and mobile web games, engaging the user requires removing browser chrome (toolbars, address bars) and managing display layout. The Fullscreen API and Screen Orientation API provide programmatic control over the viewport display mode.


1. The Fullscreen API

The Fullscreen API allows any HTML element—or the entire document—to expand to fill the entire physical screen.

Entering & Exiting Fullscreen:

1const videoPlayer = document.querySelector('#video-container');
2const fullscreenBtn = document.querySelector('#btn-fullscreen');
3 
4async function toggleFullscreen() {
5 // If not currently in fullscreen, request it
6 if (!document.fullscreenElement) {
7 try {
8 // Must be triggered by a direct user gesture (click/keypress)!
9 await videoPlayer.requestFullscreen();
10 console.log('Entered Fullscreen mode.');
11 } catch (err) {
12 console.error(`Error attempting to enable fullscreen: ${err.message}`);
13 }
14 } else {
15 // Exit fullscreen
16 await document.exitFullscreen();
17 console.log('Exited Fullscreen mode.');
18 }
19}
20 
21fullscreenBtn.addEventListener('click', toggleFullscreen);

2. Fullscreen Event & Pseudo-Class

Listening to Changes:

1document.addEventListener('fullscreenchange', () => {
2 if (document.fullscreenElement) {
3 console.log('Active fullscreen element:', document.fullscreenElement);
4 fullscreenBtn.textContent = 'Exit Fullscreen';
5 } else {
6 console.log('Returned to normal windowed view.');
7 fullscreenBtn.textContent = 'Enter Fullscreen';
8 }
9});

Styling with CSS :fullscreen Pseudo-Class:

1/* Style element specifically when it is in fullscreen mode */
2#video-container:fullscreen {
3 width: 100vw;
4 height: 100vh;
5 background-color: black;
6}
7 
8/* Style child controls during fullscreen */
9#video-container:fullscreen .controls {
10 position: fixed;
11 bottom: 20px;
12 left: 50%;
13 transform: translateX(-50%);
14}

3. The Screen Orientation API

Mobile web applications often require locking the display to landscape (for video/gaming) or portrait:

1// Inspect current orientation
2console.log('Orientation Type:', screen.orientation.type); // 'portrait-primary', 'landscape-primary'
3console.log('Orientation Angle:', screen.orientation.angle); // 0, 90, 180, 270
4 
5// Listen for device orientation rotations:
6screen.orientation.addEventListener('change', () => {
7 console.log(`Orientation rotated to: ${screen.orientation.type}`);
8});
9 
10// Locking orientation (Requires Fullscreen mode on mobile!):
11async function lockToLandscape() {
12 try {
13 await document.documentElement.requestFullscreen();
14 await screen.orientation.lock('landscape');
15 console.log('Screen locked to landscape mode.');
16 } catch (err) {
17 console.warn('Orientation lock failed or unsupported:', err);
18 }
19}
20 
21function unlockOrientation() {
22 screen.orientation.unlock();
23}

4. Security Requirement: User Gesture Gating

To prevent malicious websites from hijacking user screens, requestFullscreen() strictly requires a transient user gesture (e.g. click or key down). Calling element.requestFullscreen() automatically inside setTimeout() or on page load throws a TypeError: Permissions check failed.


Interactive Quiz• Knowledge Check

Practice Quiz

Question 1
What property on the document object indicates which element is currently expanded to fullscreen mode?
Question 2
Why will calling element.requestFullscreen() immediately upon page load fail?
Question 3
What CSS pseudo-class matches elements that are currently displayed in fullscreen mode?
Question 4
On mobile devices, what must typically occur before calling screen.orientation.lock('landscape')?
Question 5
How do you exit fullscreen mode programmatically?
Next Lesson

Web Audio & MediaStream APIs Basics

Continue learning with hands-on practice, examples, and exercises in the upcoming topic.

Related Lessons

Previous LessonNext Lesson
Geolocation, Clipboard & Device APIsWeb Audio & MediaStream APIs Basics

Practice Quiz

Test your understanding of this lesson with 5 questions. Each question has one correct answer.

Mentor-Led Practical Training

Master JavaScript for Advanced with Hands-On Projects

Practice in live mentor-led sessions with real projects, mock tests, and verifiable certification at MSK Institute.

View Full CourseBook Free Demo
PreviousNext
PreviousPrevNext