How to Use CSS Media Queries in Responsive Design

  • google plus

In Brief...

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.

Take our Advanced CSS Training course for free.

See the Course Outline and Register

Instructions

  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

Author: Chris Minnick

Chris is a prolific author and trainer, and the CEO of WatzThis?. His published books include Writing Computer Code, JavaScript for Kids, Coding with JavaScript For Dummies, Beginning HTML5 and CSS3 For Dummies, Webkit For Dummies, CIW eCommerce Certification Bible, and New Riders' XHTML.

Discuss