{"id":4089,"date":"2015-11-06T16:55:17","date_gmt":"2015-11-06T16:55:17","guid":{"rendered":"http:\/\/www.crimsondesigns.com\/blog\/?p=4089"},"modified":"2019-07-31T12:30:56","modified_gmt":"2019-07-31T12:30:56","slug":"css3-media-queries-for-beginners","status":"publish","type":"post","link":"https:\/\/www.crimsondesigns.com\/blog\/css3-media-queries-for-beginners\/","title":{"rendered":"CSS3 Media Queries for Beginners"},"content":{"rendered":"<h2>Media Queries are Essential to\u00a0Responsive Web Design.<\/h2>\n<p>With CSS3 media queries, we can write CSS that\u00a0delivers an optimal web experience for visitors viewing our websites at\u00a0different screen sizes.\u00a0CSS3 media queries also help\u00a0us fine-tune our designs for our ever-expanding mobile audience. Let&#8217;s take a look at the details.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone\" src=\"http:\/\/www.crimsondesigns.com\/blog-images\/css3-media-queries.jpg\" alt=\"CSS3 Media Queries for Beginners - Learn Responsive Web Design\" width=\"650\" height=\"600\" \/><\/p>\n<p>If a\u00a0visitor\u00a0has a web browser that supports media queries (IE9 and above), we can write CSS specifically for certain situations. For example, we can detect that a visitor\u00a0has a small device like a smart phone and give them a specific layout.<\/p>\n<p><!--more--><\/p>\n<p>We can add specific CSS for small screens or large screens (inside our cascading stylesheets). By putting our CSS media queries in the bottom of our stylesheets, we can simply overwrite any styles rules we have declared\u00a0earlier in our CSS.<\/p>\n<h3>Media Queries in a Nutshell<\/h3>\n<div class=\"highlight\">\n<ul>\n<li>A media query is an expression that evaluates to either True or False.<\/li>\n<li>The media query syntax allows for the creation of rules.<\/li>\n<li>A media query consists of a test, followed by as many CSS rules as we want, with the rule block enclosed in a set of braces. If the test condition is false, the browser simply ignores the rule block and moves on.<\/li>\n<li>The media queries in CSS3 can be used to check for a particular condition such as the width and height (of the browser window), device width and height, orientation or resolution.<\/li>\n<li>Media queries allow for subtle or drastic changes in the appearance of a website controlled entirely within the site&#8217;s CSS.<\/li>\n<li>If our media queries come last in our CSS, they will overwrite previous CSS rules, as long as they\u00a0resolve to TRUE.<\/li>\n<li>Media queries have curly braces.<\/li>\n<\/ul>\n<\/div>\n<h3>Media Query Rule Example<\/h3>\n<div class=\"example\">@media screen\u00a0and (min-width: 800px) { &#8230; }<\/div>\n<h3>Media Query Parts<\/h3>\n<p><strong>A media query consists of 3 Parts:<\/strong> the Media Type, an Expression, and the Style Rules contained within the media query itself.<\/p>\n<div class=\"highlight\">\n<ol>\n<li>The <strong>Media Type<\/strong> (example: screen) allows us to declare what type of of media the rules should be applied to. There are four options you can declare: all, print, screen, and speech.<\/li>\n<li><strong>Expressions<\/strong> (example: (min-width: 800px) allow you to further target devices based on specific conditions you pass to the media query. Expressions test media features, which describe different features of a device, such as width and height.<\/li>\n<li>\u00a0<strong><a href=\"http:\/\/www.crimsondesigns.com\/blog\/smooth-strides-from-css-to-css3\/\">CSS Style Rules<\/a><\/strong>\u00a0can be be listed\u00a0inside\u00a0the media query\u2019s curly braces.<\/li>\n<\/ol>\n<\/div>\n<h3>Colored Pencils, Pencil Boxes and Drawers<\/h3>\n<p><img decoding=\"async\" class=\"alignright\" src=\"http:\/\/www.crimsondesigns.com\/blog-images\/website-color-with-css.jpg\" alt=\"CSS3 Media Queries for Beginners\" width=\"300px\" height=\"268px\" \/>Let&#8217;s think of Each CSS Style Rule as a colored pencil, since we often use CSS Style Rules to add color to a website.\u00a0Let&#8217;s think of CSS3 Media Queries as a box that holds the\u00a0colored pencils.<\/p>\n<p>We can have a bunch of pencils in a box and several\u00a0boxes in a drawer. Let&#8217;s think of the CSS stylesheet that holds everything as the drawer.<\/p>\n<p>Of course, there may be some pencils in the drawer that are not inside the pencil boxes. So not every CSS Rule has to be in the CSS Media Queries, only the ones that are targeting specific browser widths etc..<\/p>\n<h4>Here&#8217;s a CSS Style Rule Example (the colored pencil):<\/h4>\n<div class=\"example\">\n<p class=\"special\">h3 {color: red;}<\/p>\n<\/div>\n<p>We can add color to our website with the CSS Style Rule above. Here we\u00a0are making all our h3 headings red.\u00a0Then we can limit that color to small screens, if we like, by wrapping that style rule in a\u00a0CSS Media Query.<\/p>\n<h4>Here&#8217;s the CSS3 Media Query (the pencil box)<br \/>\nwith a CSS Style Rule inside (the colored pencil).<\/h4>\n<div class=\"example\">\n<p>@media screen and (min-width: 800px) {<\/p>\n<p class=\"special\">h3 {color: red;}<\/p>\n<p>}<\/p>\n<\/div>\n<h3>Calling Media Queries<\/h3>\n<p>There are two ways to call a media query: using an external stylesheet, or writing directly inside a stylesheet.\u00a0The syntax of a media query is the same no matter if you use it inside a link element, after an @import instruction, or inside a CSS stylesheet.<\/p>\n<p>One way to use media queries is to include them inside your single stylesheet.\u00a0You can use a media query to add a breakpoint at 768px and target a computer screen.<\/p>\n<div class=\"example\">@media only screen and (max-width: 768px) { &#8230;\u00a0}<\/div>\n<h3>Breakpoints<\/h3>\n<p>We can add breakpoints so that\u00a0certain parts of our\u00a0design will behave differently on each side of the breakpoint.\u00a0Common breakpoints include the 320px (for smartphones), 768px (for tablets), and 1024px (for desktop monitors).<\/p>\n<h3>Complex Expressions<\/h3>\n<p>You can compose complex media queries using logical operators, including <strong>not,<\/strong> <strong>and,<\/strong> and <strong>only.<\/strong> You can also combine multiple media queries in a <strong>comma-separated list;<\/strong> if any of the media queries in the list is true, the entire media statement returns true. (This is equivalent to an <strong>or<\/strong> operator.)<\/p>\n<h4>AND (and keyword)<\/h4>\n<p>The AND\u00a0operator is the one you&#8217;ll probably use the most. It is used for combining multiple media features together into a single media query. It requires that all conditions specified must be met before the styling rules will take effect.<\/p>\n<p>Here is a media query\u00a0example\u00a0containing the AND operator:<\/p>\n<div class=\"highlight\">\n<div class=\"example\">@media (min-width:800px) and (max-width:1200px) { &#8230; }<\/div>\n<p>(NOTE: CSS Style Rules are not included in the above example (so it actually does nothing). But they are shown in the ones below.)<\/p>\n<\/div>\n<p>Media Query Examples with CSS Rules inside (highlighted in red):<\/p>\n<div class=\"highlight\">\n<div class=\"example\">\n<p>@media (min-width: 768px) {<\/p>\n<p class=\"special\">body {<br \/>\nmargin: 0 auto;<br \/>\nmargin-top: 20px;<br \/>\n}<\/p>\n<p>}<\/p>\n<\/div>\n<div class=\"example\">\n<p>@media only screen and (min-width: 601px) and (max-width: 1200px) {<\/p>\n<p class=\"special\">body { background: yellow; }<br \/>\nh3 {color: red;}<\/p>\n<p>}<\/p>\n<\/div>\n<\/div>\n<p>In the last\u00a0example, the two CSS rules inside the body of the media query will only be applied if the browser window width is at or between 601 pixels and 1200 pixels.<\/p>\n<p>Media Queries include &#8220;curly braces.&#8221; CSS style rules also include \u201ccurly braces,\u201d so you will see CSS \u201ccurly braces\u201d nested inside media query \u201ccurly braces.\u201d<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignright\" src=\"http:\/\/www.crimsondesigns.com\/blog-images\/media-queries-and-css-style-rules.jpg\" alt=\"CSS3 Media Queries contain CSS style rules\" width=\"301\" height=\"340\" \/><\/p>\n<p>(IMPORTANT: Don\u2019t forget to include all the curly braces. If you miss one, you may\u00a0have unexpected results in the browser. Curly braces can be written on\u00a0different lines, such as\u00a0in the first example or made more compact, such as in the second example. Just be sure they are <strong>nested correctly<\/strong> inside the media query curly braces.)<\/p>\n<p>In summary, basic CSS3 media queries are actually simple to learn and easy to add to cascading stylesheets to target different web devices or screen sizes. They just take a little practice.<\/p>\n<p>To learn more about CSS Style Rules, see\u00a0<a href=\"http:\/\/www.crimsondesigns.com\/blog\/smooth-strides-from-css-to-css3\/\">Smooth Strides from CSS to CSS3<\/a>.<\/p>\n<p>To learn more about CSS Media Queries and test your own examples, see <a href=\"http:\/\/cssmediaqueries.com\/what-are-css-media-queries.html\" target=\"_blank\" rel=\"noopener noreferrer\">What are CSS Media Queries?<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Media Queries are Essential to Responsive Web Design. With CSS3 media queries, we can write CSS that delivers an optimal web experience for visitors viewing our websites at different screen sizes.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5,8,9],"tags":[51,132],"class_list":{"0":"post-4089","1":"post","2":"type-post","3":"status-publish","4":"format-standard","6":"category-html-and-css","7":"category-web-browsers","8":"category-web-design","9":"tag-css3-media-queries","10":"tag-media-queries","11":"cat-5-id","12":"cat-8-id","13":"cat-9-id"},"_links":{"self":[{"href":"https:\/\/www.crimsondesigns.com\/blog\/wp-json\/wp\/v2\/posts\/4089","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.crimsondesigns.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.crimsondesigns.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.crimsondesigns.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.crimsondesigns.com\/blog\/wp-json\/wp\/v2\/comments?post=4089"}],"version-history":[{"count":0,"href":"https:\/\/www.crimsondesigns.com\/blog\/wp-json\/wp\/v2\/posts\/4089\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.crimsondesigns.com\/blog\/wp-json\/wp\/v2\/media?parent=4089"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.crimsondesigns.com\/blog\/wp-json\/wp\/v2\/categories?post=4089"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.crimsondesigns.com\/blog\/wp-json\/wp\/v2\/tags?post=4089"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}