{"id":203,"date":"2020-08-14T18:53:36","date_gmt":"2020-08-14T18:53:36","guid":{"rendered":"https:\/\/datatype.co.in\/blog\/?p=203"},"modified":"2020-09-06T15:39:05","modified_gmt":"2020-09-06T15:39:05","slug":"use-of-css-media-query","status":"publish","type":"post","link":"https:\/\/datatype.co.in\/blog\/use-of-css-media-query\/","title":{"rendered":"Use of CSS Media Query"},"content":{"rendered":"\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"375\" src=\"https:\/\/datatype.co.in\/blog\/wp-content\/uploads\/2020\/09\/css-media-queries-1024x375.jpg\" alt=\"Use of CSS Media Query\" class=\"wp-image-873\" srcset=\"https:\/\/datatype.co.in\/blog\/wp-content\/uploads\/2020\/09\/css-media-queries-1024x375.jpg 1024w, https:\/\/datatype.co.in\/blog\/wp-content\/uploads\/2020\/09\/css-media-queries-300x110.jpg 300w, https:\/\/datatype.co.in\/blog\/wp-content\/uploads\/2020\/09\/css-media-queries-768x281.jpg 768w, https:\/\/datatype.co.in\/blog\/wp-content\/uploads\/2020\/09\/css-media-queries-710x260.jpg 710w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>Media Query is popular CSS feature introduced in CSS3. It is used to display different styles conditionally based on what device or media a web page is being displayed i.e. for Responsive Web Designing (RWD). Using media queries you can define break points to force some web page elements behave differently in different devices based on device width.<\/p>\n\n\n\n<p><strong>Media Query Syntax:<\/strong><\/p>\n\n\n\n<p> A media query consists of an optional&nbsp;<code>media type<\/code>&nbsp;and <code>break-point<\/code> condition. Multiple queries can be combined in various ways by using&nbsp;<em>logical operators<\/em>. Media queries are case-insensitive. <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>@media media type and (condition: break-point) {\n  \/\/ CSS Rules\n}<\/code><\/pre>\n\n\n\n<p> If the break-point condition and\/or media types meet, then the CSS rules inside the media query will be applied, otherwise, they won\u2019t. <\/p>\n\n\n\n<p>There are 4 different <em> <\/em><strong>media types<\/strong>:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>all<\/code><strong>\u200a <\/strong>&#8211; \u200afor all media types<\/li><li><code>print\u200a <\/code>&#8211; \u200afor printers<\/li><li><code>screen\u200a <\/code>&#8211; for computer screens, tablets and, smart-phones<\/li><li><code>speech\u200a <\/code>&#8211; for screen readers (that reads the page out loud)<\/li><\/ul>\n\n\n\n<p>Example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>.text {\n    font-size: 16px;\n}\n@media screen and (max-width: 480px) {\n  .text {\n    font-size: 12px;\n  }\n}<\/code><\/pre>\n\n\n\n<p>In the above example, using media query we are targeting devices having width upto 480px. So, this will reduce the font size of the element with class <code>.text<\/code> to 12px whenever the break-point condition is met (i.e device max-width upto 480px). For all other devices the font size will remain 16px. This is just one simple example. We can write much more complex media queries.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/ * Target all mobile devices *\/\n@media all and (max-width: 480px) and (min-width: 320px) {\n  body {\n    background: #ccc;\n  }\n}<\/code><\/pre>\n\n\n\n<p>Here are few media query break-points for different devices:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>320px &#8211; 480px:<\/strong> Mobile devices.<\/li><li><strong>481px &#8211; 768px:<\/strong> iPads, Tablets.<\/li><li><strong>769px &#8211; 1024px:<\/strong> Small screens, laptops.<\/li><li><strong>1025px &#8211; 1200px:<\/strong> Desktops, large screens.<\/li><li><strong>1201px and more<\/strong> &#8211; Extra large screens, TV.<\/li><\/ul>\n\n\n\n<p>Note:  A<strong>lways place all your media queries at the end of the CSS file<\/strong>. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Media Query is popular CSS feature introduced in CSS3. It is used to display different styles conditionally based on what device or media a web page is being displayed i.e.&nbsp;[ &hellip; ]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[25],"tags":[116,121,118,122,119,120,112],"class_list":["post-203","post","type-post","status-publish","format-standard","hentry","category-css","tag-css-media-queries","tag-css-media-query-breakpoints","tag-css-media-query-example","tag-css-media-query-for-all-devices","tag-css-media-query-mobile-example","tag-responsive-web-design","tag-rwd","list-style-post"],"_links":{"self":[{"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/posts\/203","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/comments?post=203"}],"version-history":[{"count":43,"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/posts\/203\/revisions"}],"predecessor-version":[{"id":874,"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/posts\/203\/revisions\/874"}],"wp:attachment":[{"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/media?parent=203"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/categories?post=203"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/tags?post=203"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}