How to Use CSS Media Queries in Responsive Design

See CSS: Tips and Tricks for similar articles.

By targeting the browser width, we can style content to look appropriate for a wide desktop browser, a medium-sized tablet browser, or a small phone browser. Adjusting the layout of a web page based on the width of the browser is called "responsive design." Responsive design is made possible by CSS media queries.

In this how to, you'll learn how to use media queries in responsive design.

  1. Start with an HTML page and a set of default CSS styles. These styles will be used by the browser no matter what width the browser is.
  2. 
    <!DOCTYPE HTML>
    <html>
    <head>
    	<meta charset="UTF-8">
    	<title>Media Queries Example</title>
    	<meta name="viewport" content="width=device-width, initial-scale=1.0">
    	<style>
    		body {
    			background-color: #ccc;
    		}
    		#main {
    			background-color: #fff;
    			width: 80%;
    			margin: 0 auto;
    			padding: 2em;
    		}
    		article {
    			float: right;
    			width: 64.6666666%;
    			padding: 1%;
    			background-color: #ffaaaa;
    		}
    		aside {
    			float: left;
    			width: 31.3333333%;
    			padding: 1%;
    			background-color: #ffaaff;
    		}
    		footer {
    			clear: both;
    		}
    	</style>
    </head>
    <body>
    	<div id="main">
    		<header>
    			<h1>Media Queries</h1>
    		</header>
    		<article>
    			<h2>Main Content</h2>
    			<p>This is main content - it shows on right on desktops, on bottom on phones</p>
    			<p>This is main content - it shows on right on desktops, on bottom on phones</p>
    			<p>This is main content - it shows on right on desktops, on bottom on phones</p>
    			<p>This is main content - it shows on right on desktops, on bottom on phones</p>
    			<p>This is main content - it shows on right on desktops, on bottom on phones</p>
    		</article>
    		<aside>
    			<h2>Sidebar Content</h2>
    			<p>This is sidebar content - it shows on left on desktops, on bottom on phones</p>
    			<p>This is sidebar content - it shows on left on desktops, on bottom on phones</p>
    			<p>This is sidebar content - it shows on left on desktops, on bottom on phones</p>
    		</aside>
    		<footer>
    			<p>This is the footer - it shows only on desktops</p>
    		</footer>
    	</div>
    </body>
    </html>
    	
  3. After the footer styles, write the following media query. This will apply the CSS within it whenever the browser width is less than or equal to 700px.
    
    	@media screen and (max-width: 700px) {
    
    	}
    
  4. Between the curly braces of the media query, you can override the default styles to change the layout of the page for smaller browsers, like this:
    
    @media screen and (max-width: 700px) {
    	article {
    		float: none;
    		width: 98%;
    		padding: 1%;
    		background-color: #ffaaaa;
    	}
    	aside {
    		float: none;
    		width: 98%;
    		padding: 1%;
    		background-color: #ffaaff;
    	}
    	footer {
    		display: none;
    	}
    }
    
  5. Open the HTML page in a browser. This code renders the following, if your browser window is greater than 700px wide: Responsive web page - large
  6. Drag the right edge of your web browser to make it narrower. When the width of the browser gets to 700px or less, the layout will change to the following: Responsive web page - small

Related Articles

  1. Learn the Very Basics of CSS in One Minute
  2. How to Create a CSS External Style Sheet
  3. How to Align Text with CSS
  4. How to Create a Horizontal Navigation Menu with CSS
  5. How to Create a Fixed-Width Layout with CSS
  6. How to Remove Spacing Between Table Borders with CSS
  7. How to Set a Background Image with CSS
  8. How to Set Text Spacing and Placement in CSS
  9. How to Style a Table with CSS
  10. How to Create Boxes with Rounded Corners in CSS
  11. How to Create a Vertical Navigation Menu with CSS
  12. How to Use the CSS Opacity Property
  13. How to Use Multiple Background Images with CSS
  14. Absolute Positioning with CSS
  15. How to Use the CSS Border Shorthand Property
  16. How to Create CSS Button Links
  17. How to Create a Fluid-Width Layout with CSS
  18. How to Set Text and Background Color with CSS
  19. How to Create a CSS Embedded Style Sheet
  20. How to Add Inline Styles to CSS
  21. How to Create a Border with CSS
  22. How to Use the CSS Padding Shorthand Property
  23. How to Create a Fly-Out Menu with CSS
  24. How to Use CSS Media Queries in Responsive Design (this article)
  25. How to Adjust Margins with CSS
  26. How to Use the CSS Background Shorthand Property
  27. How to Create a Form without Tables Using CSS
  28. How to Modify Fonts in CSS
  29. How to Create a Drop-Down Menu with CSS
  30. How to Apply Padding with CSS
  31. Fixed Positioning with CSS
  32. How to Use CSS Transitions
  33. How to Use the CSS list-style Shorthand Property
  34. How to Change Text Style in CSS
  35. How to Create CSS Sprites
  36. How to Use CSS with Different Media Types
  37. How to Import Style Sheets with @import in CSS
  38. How to Use the CSS White-Space Property
  39. How to Use the CSS Z-index Property
  40. How to Create Drop Shadows with the box-shadow Property in CSS3