Media Queries Responsive Website Layout Tutorial

Published :
Author :
Adam Khoury
Learn to easily create responsive website layouts using CSS media queries. Media queries allow us to target a particular media type and then apply zero or more expressions that check for the conditions of particular media features. Using this logic we can adjust all CSS for our site according to the screen size of the device that a user is viewing it with. Example 1 - Media Queries Changing Stylesheet <!DOCTYPE html> <html> <head> <link rel="stylesheet" media="screen and (min-width: 0px)" href="small.css"> <link rel="stylesheet" media="screen and (min-width: 1000px)" href="medium.css"> <link rel="stylesheet" media="screen and (min-width: 1400px)" href="large.css"> </head> <body> <div id="mydiv">example element</div> </body> </html> Example 2 - Media Queries Inline CSS <!DOCTYPE html> <html> <head> <style> @media screen and (min-width: 0px) { div#mydiv{ width:500px; margin:0px auto; height:100px; background: #D5E600; font-size:27px; } } @media screen and (min-width: 1000px) { div#mydiv{ width:900px; margin:0px auto; height:100px; background: #D5E600; font-size:27px; } } </style> </head> <body> <div id="mydiv">example element</div> </body> </html>