Categories: Snippets

Create responsive websites with CSS3 media query

Responsive web design (RWD) is an approach whereby a designer creates a web page that “responds to” or resizes itself depending on the type of device it is being seen through. That could be an over-sized desktop computer monitor, a laptop or devices with small screens such as smartphones and tablets. Media queries are used to detect and respond to device with variable device width.

We have 3 ways to use media query.

  1. @import Method import CSS files
  2. Writing media query in single CSS file
  3. including linked CSS

1. @import Method import CSS files

@import url(style320.css) screen and (min-width: 320px);
@import url(style544.css) screen and (min-width: 544px);
@import url(style768.css) screen and (min-width: 768px);
@import url(style992.css) screen and (min-width: 992px);
@import url(style1200.css) screen and (min-width: 1200px);

2. Writing media query in single CSS file

/* CSS code except media */
@media only screen and (min-width: 320px){
/* 320px device css */}

@media only screen and (min-width: 544px){
/* 320px device css */}

@media only screen and (min-width: 768px){
/* 768px device css */}

@media only screen and (max-width: 992px) {
/* 992px device css */}

@media only screen and (min-width: 1200px){
/* 1200px device css */}

3. including linked CSS

<link rel="stylesheet" type="text/css" media="screen and (max-device-width: 320px)" href="style320.css" />
<link rel="stylesheet" type="text/css" media="screen and (max-device-width: 544px)" href="style544.css" />
<link rel="stylesheet" type="text/css" media="screen and (max-device-width: 768px)" href="style768.css" />
<link rel="stylesheet" type="text/css" media="screen and (max-device-width: 992px)" href="style992.css" />
<link rel="stylesheet" type="text/css" media="screen and (max-device-width: 1200px)" href="style1200.css" />

Media queries for common device breakpoints

/* Smartphones (portrait and landscape) ----------- */@media only screen and (min-device-width : 320px) and (max-device-width : 480px) {
/* Styles */}

/* Smartphones (landscape) ----------- */@media only screen and (min-width : 321px) {
/* Styles */}

/* Smartphones (portrait) ----------- */@media only screen and (max-width : 320px) {
/* Styles */}

/* iPads (portrait and landscape) ----------- */@media only screen and (min-device-width : 768px) and (max-device-width : 1024px) {
/* Styles */}

/* iPads (landscape) ----------- */@media only screen and (min-device-width : 768px) and (max-device-width : 1024px) and (orientation : landscape) {
/* Styles */}

/* iPads (portrait) ----------- */@media only screen and (min-device-width : 768px) and (max-device-width : 1024px) and (orientation : portrait) {
/* Styles */}
/**********
iPad 3
**********/@media only screen and (min-device-width : 768px) and (max-device-width : 1024px) and (orientation : landscape) and (-webkit-min-device-pixel-ratio : 2) {
/* Styles */}

@media only screen and (min-device-width : 768px) and (max-device-width : 1024px) and (orientation : portrait) and (-webkit-min-device-pixel-ratio : 2) {
/* Styles */}
/* Desktops and laptops ----------- */@media only screen  and (min-width : 1224px) {
/* Styles */}

/* Large screens ----------- */@media only screen  and (min-width : 1824px) {
/* Styles */}

/* iPhone 4 ----------- */@media only screen and (min-device-width : 320px) and (max-device-width : 480px) and (orientation : landscape) and (-webkit-min-device-pixel-ratio : 2) {
/* Styles */}

@media only screen and (min-device-width : 320px) and (max-device-width : 480px) and (orientation : portrait) and (-webkit-min-device-pixel-ratio : 2) {
/* Styles */}

/* iPhone 5 ----------- */@media only screen and (min-device-width: 320px) and (max-device-height: 568px) and (orientation : landscape) and (-webkit-device-pixel-ratio: 2){
/* Styles */}

@media only screen and (min-device-width: 320px) and (max-device-height: 568px) and (orientation : portrait) and (-webkit-device-pixel-ratio: 2){
/* Styles */}

/* iPhone 6 ----------- */@media only screen and (min-device-width: 375px) and (max-device-height: 667px) and (orientation : landscape) and (-webkit-device-pixel-ratio: 2){
/* Styles */}

@media only screen and (min-device-width: 375px) and (max-device-height: 667px) and (orientation : portrait) and (-webkit-device-pixel-ratio: 2){
/* Styles */}

/* iPhone 6+ ----------- */@media only screen and (min-device-width: 414px) and (max-device-height: 736px) and (orientation : landscape) and (-webkit-device-pixel-ratio: 2){
/* Styles */}

@media only screen and (min-device-width: 414px) and (max-device-height: 736px) and (orientation : portrait) and (-webkit-device-pixel-ratio: 2){
/* Styles */}

/* Samsung Galaxy S3 ----------- */@media only screen and (min-device-width: 320px) and (max-device-height: 640px) and (orientation : landscape) and (-webkit-device-pixel-ratio: 2){
/* Styles */}

@media only screen and (min-device-width: 320px) and (max-device-height: 640px) and (orientation : portrait) and (-webkit-device-pixel-ratio: 2){
/* Styles */}

/* Samsung Galaxy S4 ----------- */@media only screen and (min-device-width: 320px) and (max-device-height: 640px) and (orientation : landscape) and (-webkit-device-pixel-ratio: 3){
/* Styles */}

@media only screen and (min-device-width: 320px) and (max-device-height: 640px) and (orientation : portrait) and (-webkit-device-pixel-ratio: 3){
/* Styles */}

/* Samsung Galaxy S5 ----------- */@media only screen and (min-device-width: 360px) and (max-device-height: 640px) and (orientation : landscape) and (-webkit-device-pixel-ratio: 3){
/* Styles */}

@media only screen and (min-device-width: 360px) and (max-device-height: 640px) and (orientation : portrait) and (-webkit-device-pixel-ratio: 3){
/* Styles */}

 

Recent Posts

Customize the Browser’s Scrollbar only with CSS

-webkit-scrollbar consists of seven different pseudo-elements, and together comprise a full scrollbar UI element: ::-webkit-scrollbar the background…

6 years ago

Fix iOS bug not displaying 100vh correctly

There are many Javascript fixes for the viewport height units bug in iOS 7 (iPhone…

7 years ago

Pure CSS Page Loader in website

A preloader or what some call a loading screen — is the what you see on…

7 years ago