CSS

Media Queries of CSS & HTML (In Depth) 2020

Share your learning

Media queries is a CSS trick to make the website fully responsive for all devices like mobile and tablets. Actually, it adds the conditional CSS styles to the web page and apply style when certain condition is true.

Media query help us to organize the special look for different screen sizes, orientations (Landscape or Portrait) and even for printer also.

Let’s understand more deeply,

By the way, this is the 4th version of media query.

Basic Example:

@media only screen and (max-width: 768px) {
  // apply CSS if media type is SCREEN and width < = 768px;
}

Syntax:

@media not|only mediatype and (mediafeature and|or|not mediafeature) {
 // CSS style goes here
   // CSS style will apply when condition is true 
}

Key elements of media query:

  1. Media type
  2. Media features
  3. Logical operators

Note:  queries are case-insensitive and if query contain UNKOWN media type it will be false by default.

Media types:

1.AllDefault, match with all devices
2.PrintIt used to style print preview of the page.
3.ScreenIt used for device screens like computer, tablet, mobile etc.
4.SpeechIt is suitable for screen readers.

Media features:

1.any-hoverDoes any available input mechanism allow the user to hover over elements?
2.any-pointerIs any available input mechanism a pointing device, and if so, how accurate is it?
3.aspect-ratioWidth-to-height aspect ratio of the viewport
4.colorNumber of bits per color component of the output device, or zero if the device isn’t color
5.color-gamutApproximate range of colors that are supported by the user agent and output device
6.color-indexNumber of entries in the output device’s color lookup table, or zero if the device does not use such a table
7.display-modeThe display mode of the application, as specified in the web app manifest’s display member
8.forced-colorsDetect whether user agent restricts color palette
9.gridDoes the device use a grid or bitmap screen?
10.heightHeight of the viewport
11.hoverDoes the primary input mechanism allow the user to hover over elements?
12.inverted-colorsIs the user agent or underlying OS inverting colors?
13.light-levelLight level of the environment
14.monochromeBits per pixel in the output device’s monochrome frame buffer, or zero if the device isn’t monochrome
15.OrientationOrientation of the viewport
16.overflow-blockHow does the output device handle content that overflows the viewport along the block axis?
17.overflow-inlineCan content that overflows the viewport along the inline axis be scrolled?
18.pointerIs the primary input mechanism a pointing device, and if so, how accurate is it?
19.prefers-color-schemeDetect if the user prefers a light or dark color scheme
20.prefers-contrastDetects if the user has requested the system increase or decrease the amount of contrast between adjacent colors
21prefers-reduced-motionThe user prefers less motion on the page
22prefers-reduced-transparencyThe user prefers reduced transparency
23resolutionPixel density of the output device
24scanScanning process of the output device
25scriptingDetects whether scripting (i.e. JavaScript) is available
26updateHow frequently the output device can modify the appearance of content
27widthWidth of the viewport including width of scrollbar

Logical operators:

1.OnlyIt is basically used for old browsers that do not support media type with media feature. Like old browser can interpret screen and (min-width: 768px) as the screen only and apply to all screens, to avoid this we have to use the Only operator. It has no effect on a modern browser.  
Note: We must specify media type with the Only operator.
2.AndThis operator is used to create the chain of multiple media features together where all conditions should be satisfied to apply the specified style.
3.NotThis operator used to apply the style when the media query becomes oppose. It basically reverses the condition of the media query.
Note: We must specify media type with Not operator.
4., (comma)It is used to separate the queries in a single media query. That means different queries with the same style can use this operator to combine into a single rule. It basically works like OR operator. When a single query becomes true, the style will be applied.

Image by Mudassar Iqbal from Pixabay

How to use? Examples:

We can link different stylesheets for different screen sizes with media attribute like below,

<link rel="stylesheet" media="screen and (max-width: 1200px)" href="style-lg.css">

Example of media Type:

@media screen {
     #navbar {
      Display: block; 
    }
}

@media print {
    #navbar {
 Display: none;
    }
}

Above media query which uses the print media type, will apply when user open the print preview of web page. And screen media type will apply default to the web page.

Example of media features with operators:

@media screen and (max-width: 768px) {
    .sidemenu {
 Display: none;
    }
}

Above media query will apply to the devices with max screen size 768px or less.

Example of Orientation (media feature):

@media only screen and (orientation: landscape) {
   .sidemenu {
  Display: block;
 }
}

Example of comma separated operator:

@media screen and (min-width: 992px), (min-width: 1200px) {
 #navbar, .sidemenu {
 Display: block;
 }
}

Above media query will apply when the device is at least 992px in size or wider than this. Because we have used the comma operator here. So, it will also apply the style when min-width of the screen is 1200px or wider.

Learn more about CSS.

Do we need media query for each screen size?

Nope, we can’t create, I mean we shouldn’t create the media breakpoints for all devices,

Why?

Because of consistent change in the devices, every day new device comes and old fade-out that means we have to manage all new sizes along with old one.

Let’s make it easy,

We should create breakpoints from where the screen start looks bad and need to make some designs with a range of sizes that can cover a wide range of screen sizes with a single design.

For real-time testing of media query checkout this website CSSMediaQueries

We must know about the standard sizes of the devices that can cover all size. So, to know about breakpoints, read here, at WebsiteDimentions.

If you find this article useful, please comment and share it with your friends.

Satpal

View Comments

Share
Published by
Satpal

Recent Posts

How to Switch PHP Versions in XAMPP Easily: Managing Multiple PHP Versions on Ubuntu

Today we are going to learn about managing multiple PHP versions on ubuntu with xampp.…

1 year ago

How to Use Coding to Improve Your Website’s SEO Ranking?

Let's understand about how to use coding to improve your website's SEO. In today’s computerized…

1 year ago

Most Important Linux Commands for Web Developers

Let's understand the most important linux commands for web developers. Linux, as an open-source and…

1 year ago

Top 75+ Laravel Interview Questions Asked by Top MNCs

Today we are going to discuss top 75+ Laravel interview questions asked by top MNCs.Laravel,…

1 year ago

Mailtrap Integration for Email Testing with Laravel 10

Today we will discuss about the Mailtrap integration with laravel 10 .Sending and receiving emails…

1 year ago

Firebase Cloud Messaging (FCM) with Ionic 6: Push Notifications

Today we are going to integrate FCM (Firebase Cloud Messaging) push notifications with ionic application.Firebase…

1 year ago