Hosting Solutions
mobile_logo Table of Contents

Window Size Responsive CSS Layout Stylesheet Change Tutorial

Learn to program layout responses to the browser window size changes using JavaScript to affect the CSS style sheet link element in the document. The script also handles layout adjustment when the document is initially loading. <!DOCTYPE html> <html> <head> <link id="pagestyle" rel="stylesheet" type="text/css" href="large.css"> <script> function layoutHandler(){ var styleLink = document.getElementById("pagestyle"); if(window.innerWidth < 900){ styleLink.setAttribute("href", "mobile.css"); } else if(window.innerWidth < 1200){ styleLink.setAttribute("href", "medium.css"); } else { styleLink.setAttribute("href", "large.css"); } } window.onresize = layoutHandler; layoutHandler(); </script> </head> <body> <div id="my_header"></div> <div id="my_menu"> <a href="#">Home</a> <a href="#">Services</a> <a href="#">Staff</a> <a href="#">Contact</a> </div> <div id="my_content"> Lorem ipsum goes here as dummy text, lots of it. </div> </body> </html>

JavaScript Videos

General Programming

Flat Icons HTML Code Symbols Discovery JavaScriptHow to Program Code Syntax Highlighter Using JavaScriptFat Arrow Functions JavaScript Programming TutorialJavaScript Arguments Object Function TutorialJavaScript Storage Interface sessionStorage localStorage TutorialMulti Column Layout Grid Programming TutorialCustom Alert Box Programming TutorialCustom Confirm Box Programming TutorialCustom Prompt Box Programming TutorialChange CSS Class Style className Toggle TutorialPartial Print Document Tutorial HTML div ContentClick Outside Close Menu Box TutorialPercent Math Calculations Programming TutorialDual Image Reveal Control TutorialPage Loading Screen Document Preloader TutorialControl Panel Flyout Menu Windows AnimationsCustom Data Attributes HTML JavaScript CSS TutorialFullscreen API JavaScript Code Examples and SpecificationConsole Log Tutorial Chrome Firefox IE BrowsersToggle Function Click Drop Down Menus TutorialChange Tab Text and Window Title On The Fly TutorialDice Roll Programming Tutorial For Web Browser GamesDynamic Lorem Ipsum Placeholder Text TutorialBest HTML Event Handling addEventListener TutorialsetAttribute removeAttribute Methods TutorialDisabled User Handling Redirect WebsiteCapture Keyboard Input Tutorial HTML ApplicationCapture Keyboard Input Condition Logic TutorialChange Style Sheet Using Tutorial CSS Swap StylesheetWeb Page Overlay Tutorial Transparent CoverLights Out For Earth Day Tutorial Dark Page SwitchWindow Size Responsive CSS Layout Stylesheet Change TutorialRead Mouse Coordinates Pointer Position Tutorial