CSS Tutorial
CSS Advanced
CSS Responsive
CSS Grid
CSS Media Queries – Examples
Media queries are a great way for delivering a tailored style sheet to different devices. To illustrate a simple example, let’s change the background color for different devices:
HTML
				
					
Resize the browser window to see the effect!
By default, the background color of the document is "tan". If the screen size is 992px or less, the color will change to "blue". If it is 600px or less, it will change to "olive".
 
				
			
		CSS
				
					body {
  background-color: tan;
  color: black;
}
/* On screens that are 992px wide or less, the background color is blue */
@media screen and (max-width: 992px) {
  body {
    background-color: grey;
    color: white;
  }
}
/* On screens that are 600px wide or less, the background color is olive */
@media screen and (max-width: 600px) {
  body {
    background-color: olive;
    color: white;
  }
} 
				
			
		Output
Resize the browser window to see the effect!
By default, the background color of the document is "tan". If the screen size is 992px or less, the color will change to "blue". If it is 600px or less, it will change to "olive".
Responsive Navigation
HTML
CSS
				
					* {
  box-sizing: border-box;
}
/* Style the top navigation bar */
.topnav {
  overflow: hidden;
  background-color: #333;
}
/* Style the topnav links */
.topnav a {
  float: left;
  display: block;
  color: #f2f2f2;
  text-align: center;
  padding: 14px 16px;
  text-decoration: none;
}
/* Change color on hover */
.topnav a:hover {
  background-color: #ddd;
  color: black;
}
/* On screens that are 600px wide or less, make the menu links stack on top of each other instead of next to each other */
@media screen and (max-width: 600px) {
  .topnav a {
    float: none;
    width: 100%;
  }
} 
				
			
		Output
Media Queries for Columns
One of the most common use of media queries, is to design a flexible layout. In this example, a layout is created that varies between four, two and full-width columns, depending on different screen sizes:
HTML
				
					Resize the browser window to see the responsive effect. On screens that are 992px wide or less, the columns will resize from four columns to two columns. On screens that are 600px wide or less, the columns will stack on top of each other instead of next to eachother.
  
    Column 1
    Some text..
  
  
    Column 2
    Some text..
  
  
  
    Column 3
    Some text..
  
  
  
    Column 4
    Some text..
  
			
		CSS
				
					* {
  box-sizing: border-box;
}
/* Create four equal columns that floats next to each other */
.column {
  float: left;
  width: 25%;
  padding: 20px;
}
/* Clear floats after the columns */
.row:after {
  content: "";
  display: table;
  clear: both;
}
/* On screens that are 992px wide or less, go from four columns to two columns */
@media screen and (max-width: 992px) {
  .column {
    width: 50%;
  }
}
/* On screens that are 600px wide or less, make the columns stack on top of each other instead of next to each other */
@media screen and (max-width: 600px) {
  .column {
    width: 100%;
  }
}
 
				
			
		Output
Resize the browser window to see the responsive effect. On screens that are 992px wide or less, the columns will resize from four columns to two columns. On screens that are 600px wide or less, the columns will stack on top of each other instead of next to eachother.
Column 1
Some text..
Column 2
Some text..
Column 3
Some text..
Column 4
Some text..
Responsive Layout with Flex
HTML
				
					Resize the browser window to see the responsive effect. On screens that are 992px wide or less, the columns will resize from four columns to two columns. On screens that are 600px wide or less, the columns will stack on top of each other instead of next to eachother.
  
    Column 1
    Some text..
  
  
  
    Column 2
    Some text..
  
  
  
    Column 3
    Some text..
  
  
  
    Column 4
    Some text..
  
			
		CSS
				
					* {
  box-sizing: border-box;
}
/* Container for flexboxes */
.row {
  display: flex;
  flex-wrap: wrap;
}
/* Create four equal columns */
.column {
  flex: 25%;
  padding: 20px;
}
/* On screens that are 992px wide or less, go from four columns to two columns */
@media screen and (max-width: 992px) {
  .column {
    flex: 50%;
  }
}
/* On screens that are 600px wide or less, make the columns stack on top of each other instead of next to each other */
@media screen and (max-width: 600px) {
  .row {
    flex-direction: column;
  }
} 
				
			
		Output
Resize the browser window to see the responsive effect. On screens that are 992px wide or less, the columns will resize from four columns to two columns. On screens that are 600px wide or less, the columns will stack on top of each other instead of next to eachother.
Column 1
Some text..
Column 2
Some text..
Column 3
Some text..
Column 4
Some text..
Hide Elements with Media Queries
Media queries can also be used to hide elements on different screen sizes:
HTML
				
					Hide elements on different screen sizes
Example DIV.
When the browser's width is 600px wide or less, hide the div element. Resize the browser window to see the effect.
			
		CSS
				
					div.example {
  background-color: grey;
  padding: 20px;
}
@media screen and (max-width: 600px) {
  div.example {
    display: none;
  }
} 
				
			
		Output
When the browser's width is 600px wide or less, hide the div element. Resize the browser window to see the effect.
Change Font Size with Media Queries
You can also use media queries to change the font size of an element on different screen sizes:
HTML
				
					Change the font size of an element on different screen sizes
Example DIV.
			
		CSS
				
					div.example {
  background-color: lightgrey;
  padding: 20px;
}
@media screen and (min-width: 600px) {
  div.example {
    font-size: 20px;
  }
}
@media screen and (max-width: 600px) {
  div.example {
    font-size: 10px;
  }
} 
				
			
		Output
Flexible Image Gallery
In this example, media queries are grouped together with flexbox to create a responsive image gallery:
HTML
				
					
  Responsive Image Grid
  Resize the browser window to see the responsive effect.
 
  
     
     
  
  
     
     
     
   
   
  
     
     
  
  
  
     
     
     
 
  
 
				
			
		CSS
				
					* {
  box-sizing: border-box;
}
body {
  margin: 0;
  font-family: Arial;
}
.header {
  text-align: center;
  padding: 32px;
}
.row {
  display: flex;
  flex-wrap: wrap;
  padding: 0 4px;
}
/* Create four equal columns that sits next to each other */
.column {
  flex: 25%;
  max-width: 25%;
  padding: 0 4px;
}
.column img {
  margin-top: 8px;
  vertical-align: middle;
}
/* Responsive layout - makes a two column-layout instead of four columns */
@media screen and (max-width: 800px) {
  .column {
    flex: 50%;
    max-width: 50%;
  }
}
/* Responsive layout - makes the two columns stack on top of each other instead of next to each other */
@media screen and (max-width: 600px) {
  .column {
    flex: 100%;
    max-width: 100%;
  }
} 
				
			
		Output
 
															Flexible Website
In the below example, media queries are grouped together with flexbox to create a responsive website, that consists of a flexible navigation bar and flexible content.
HTML
				
					
  Resize the browser window to see the responsive effect.
  My Website
  With a flexible layout.
  
    About Me
    Photo of me:
    Image
    Some text about me in culpa qui officia deserunt mollit anim..
    More Text
    Lorem ipsum dolor sit ame.
    Image
    Image
    Image
  
  
    TITLE HEADING
    Title description, Dec 7, 2017
    Image
    Some text..
    Sunt in culpa qui officia deserunt mollit anim id est laborum consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco.
    
    TITLE HEADING
    Title description, Sep 2, 2017
    Image
    Some text..
    Sunt in culpa qui officia deserunt mollit anim id est laborum consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco.
  
  Footer
 
				
			
		CSS
				
					* {
  box-sizing: border-box;
}
/* Style the body */
body {
  font-family: Arial;
  margin: 0;
}
/* Header/logo Title */
.header {
  padding: 60px;
  text-align: center;
  background: #1abc9c;
  color: white;
}
/* Style the top navigation bar */
.navbar {
  display: flex;
  background-color: #333;
}
/* Style the navigation bar links */
.navbar a {
  color: white;
  padding: 14px 20px;
  text-decoration: none;
  text-align: center;
}
/* Change color on hover */
.navbar a:hover {
  background-color: #ddd;
  color: black;
}
/* Column container */
.row {  
  display: flex;
  flex-wrap: wrap;
}
/* Create two unequal columns that sits next to each other */
/* Sidebar/left column */
.side {
  flex: 30%;
  background-color: #f1f1f1;
  padding: 20px;
}
/* Main column */
.main {
  flex: 70%;
  background-color: white;
  padding: 20px;
}
/* Fake image, just for this example */
.fakeimg {
  background-color: #aaa;
  width: 100%;
  padding: 20px;
}
/* Footer */
.footer {
  padding: 20px;
  text-align: center;
  background: #ddd;
}
/* Responsive layout - when the screen is less than 700px wide, make the two columns stack on top of each other instead of next to each other */
@media screen and (max-width: 700px) {
  .row, .navbar {   
    flex-direction: column;
  }
} 
				
			
		Output
Resize the browser window to see the responsive effect.
My Website
With a flexible layout.
About Me
Photo of me:
Some text about me in culpa qui officia deserunt mollit anim..
More Text
Lorem ipsum dolor sit ame.
TITLE HEADING
Title description, Dec 7, 2017
Some text..
Sunt in culpa qui officia deserunt mollit anim id est laborum consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco.
TITLE HEADING
Title description, Sep 2, 2017
Some text..
Sunt in culpa qui officia deserunt mollit anim id est laborum consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco.
Footer
Min Width to Max Width
You can also use the (max-width: ..) and (min-width: ..) values to set a minimum width and a maximum width.
For example, when the browser’s width is between 600 and 900px, change the appearance of a <div> element
HTML
				
					Example DIV.
When the browser's width is between 600 and 900px, change the appearance of DIV. 
Resize the browser window to see the effect.
 
				
			
		CSS
				
					@media screen and (max-width: 900px) and (min-width: 600px) {
  div.example {
    font-size: 50px;
    padding: 50px;
    border: 8px solid black;
    background: grey;
  }
} 
				
			
		Output
When the browser's width is between 600 and 900px, change the appearance of DIV. Resize the browser window to see the effect.
