{"id":1506,"date":"2020-02-16T16:15:13","date_gmt":"2020-02-16T21:15:13","guid":{"rendered":"http:\/\/newmediaproduction.mynmi.net\/?page_id=1506"},"modified":"2022-07-19T20:33:49","modified_gmt":"2022-07-19T20:33:49","slug":"media-queries","status":"publish","type":"page","link":"https:\/\/nmi.cool\/web\/media-queries\/","title":{"rendered":"Media Queries"},"content":{"rendered":"\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1000\" height=\"666\" src=\"https:\/\/nmi.cool\/newmediaproduction\/wp-content\/uploads\/sites\/3\/2020\/02\/christopher-gower-m_HRfLhgABo-unsplash.jpg\" alt=\"\" class=\"wp-image-1507\" srcset=\"https:\/\/nmi.cool\/web\/wp-content\/uploads\/sites\/3\/2020\/02\/christopher-gower-m_HRfLhgABo-unsplash.jpg 1000w, https:\/\/nmi.cool\/web\/wp-content\/uploads\/sites\/3\/2020\/02\/christopher-gower-m_HRfLhgABo-unsplash-300x200.jpg 300w, https:\/\/nmi.cool\/web\/wp-content\/uploads\/sites\/3\/2020\/02\/christopher-gower-m_HRfLhgABo-unsplash-768x511.jpg 768w\" sizes=\"auto, (max-width: 1000px) 100vw, 1000px\" \/><figcaption>Photo by&nbsp;<a href=\"https:\/\/unsplash.com\/@cgower?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText\">Christopher Gower<\/a>.<\/figcaption><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">What are Media Queries? <\/h2>\n\n\n\n<p>Media queries are one of the primary components of responsive design<a href=\"#footnote-1-1506\" id=\"note-1-1506\" rel=\"footnote\">1<\/a>. A media query is a specific CSS component that that lets you set up extra CSS rules for different viewports or screen sizes. For example, you might want your <code>&lt;p&gt;<\/code> element&#8217;s font size to be 28 pixels when viewed on a desktop, but only 18 pixels when viewed on a mobile phone. A media query will let you achieve that result. <\/p>\n\n\n\n<p>Let&#8217;s get started by looking at the syntax for a media query. Remember, this will be part of your CSS document. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Media Query Syntax<\/h2>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1020\" height=\"396\" src=\"https:\/\/nmi.cool\/newmediaproduction\/wp-content\/uploads\/sites\/3\/2020\/02\/Screen-Shot-2020-02-16-at-1.27.50-PM.png\" alt=\"Media query reads: When viewed on a screen with a max width of 600px the main element will have a background color of aquamarine. \" class=\"wp-image-1509\" srcset=\"https:\/\/nmi.cool\/web\/wp-content\/uploads\/sites\/3\/2020\/02\/Screen-Shot-2020-02-16-at-1.27.50-PM.png 1020w, https:\/\/nmi.cool\/web\/wp-content\/uploads\/sites\/3\/2020\/02\/Screen-Shot-2020-02-16-at-1.27.50-PM-300x116.png 300w, https:\/\/nmi.cool\/web\/wp-content\/uploads\/sites\/3\/2020\/02\/Screen-Shot-2020-02-16-at-1.27.50-PM-768x298.png 768w\" sizes=\"auto, (max-width: 1020px) 100vw, 1020px\" \/><figcaption>Media query example.<\/figcaption><\/figure>\n\n\n\n<p>First we will notice <code>@media<\/code>. All media queries will start this way; <code>@media<\/code> is a signal to the browsers that the browsers will need to apply new CSS rules if certain conditions are true. In this case, the conditions we are using are <code>screen<\/code> and <code>max-width: 600px<\/code>.<\/p>\n\n\n\n<p>After the <code>@media<\/code> declaration we get the &#8220;<strong>media type<\/strong>.&#8221; In this example the media query will only been applied when the media type is <code>screen<\/code>. There are several other media types we could choose from, including: <\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>all<\/code>: This is the <strong>default<\/strong> and means that the rule will be applied to all types of media. <\/li><li><code>print<\/code>: If print is selected, the CSS rules will only apply when you are trying to print a webpage. For example, perhaps you want a background image or color applied when a user is looking at the screen, but you don&#8217;t want that image or color to show up when the user prints out the webpage. <\/li><li><code>screen:<\/code> This rule will apply when we are viewing the page on the screen of any given device. <\/li><li><code>speech:<\/code> This allows us to set up rules specifically for screenreaders. <\/li><\/ul>\n\n\n\n<p>Moving on, the code inside the parenthesis, in this case, the <code>max-width<\/code>, is referred to as the <strong>expression<\/strong>. It&#8217;s another condition we&#8217;re applying to the media query. In this case, we&#8217;re saying that this rule applies only when the screen size is less than 600 pixels. Once it reaches the maximum width of 600px the rule will no longer apply. If this is hard to visualize, don&#8217;t worry\u2014 we&#8217;ll try out some examples in a minute. <\/p>\n\n\n\n<p>The next thing you&#8217;ll notice is that we have a CSS selector (<code>&lt;main&gt;<\/code>) inside the opening and closing brackets of our media query. You can think of media queries as a <strong>mini stylesheet<\/strong> within our original style sheet. In other words,  the rules within the media query will override the default rules of the stylesheet when the conditions described by the media query are met. It helps to see this in action so let&#8217;s try it out!<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Media Query Example <\/h2>\n\n\n\n<p>To get started, create a new folder called &#8220;mediatest&#8221; and save it in your webdev folder in Dropbox. Within that folder create another folder called &#8220;css.&#8221; This will just be a practice exercise, so no need to upload it to your server. <\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Add some content! <\/h3>\n\n\n\n<p>Next, open Visual Studio Code, create a new html file called &#8220;mediaquery.html&#8221; and save it in your &#8220;mediatest&#8221; folder. Add your <a href=\"https:\/\/nmi.cool\/web\/html-boilerplate\/\" target=\"_blank\" rel=\"noreferrer noopener\">HTML boilerplate<\/a>. <\/p>\n\n\n\n<p>Copy the following code and paste it <strong>between<\/strong> the body tags in your HTML document and then hit save.  <\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">    &lt;main&gt;\n        &lt;h1&gt;I Love Responsive Web Design!&lt;\/h1&gt;\n        &lt;p&gt;Media queries are one of the primary components of responsive design. Web design is responsive when it changes based on things like viewport size, device, and screen orientation. &lt;\/p&gt;\n    &lt;\/main&gt;<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Add some CSS! <\/h3>\n\n\n\n<p>Create a new file in Visual Studio code and save it as &#8220;media.css&#8221; in the &#8220;css&#8221; folder that you created inside of the &#8220;mediatest&#8221; folder. Next, link your stylesheet by adding the following to the <code>&lt;head&gt;<\/code> below any other metadata: <\/p>\n\n\n\n<p><code>&lt;link rel=\"stylesheet\" href=\"css\/media.css\"&gt;<\/code><\/p>\n\n\n\n<p>Okay, now go ahead and paste the following CSS into your stylesheet. <\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">main {\n    background-color: darkcyan;\n    text-align: center;\n}\n\nh1 {\n    font-size: 60px;\n}\n\np {\n    font-size: 32px;\n}<\/pre>\n\n\n\n<p>Save everything and open your HTML file in Chrome to take a look at the very simple webpage that we&#8217;ve created. If everything worked, you should see something that looks like this: <\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"2146\" height=\"1176\" src=\"https:\/\/nmi.cool\/newmediaproduction\/wp-content\/uploads\/sites\/3\/2020\/02\/Screen-Shot-2020-02-16-at-3.44.21-PM.png\" alt=\"An image of a webpage with a large dark cyan square, a heading, and text inside it. \" class=\"wp-image-1512\" srcset=\"https:\/\/nmi.cool\/web\/wp-content\/uploads\/sites\/3\/2020\/02\/Screen-Shot-2020-02-16-at-3.44.21-PM.png 2146w, https:\/\/nmi.cool\/web\/wp-content\/uploads\/sites\/3\/2020\/02\/Screen-Shot-2020-02-16-at-3.44.21-PM-300x164.png 300w, https:\/\/nmi.cool\/web\/wp-content\/uploads\/sites\/3\/2020\/02\/Screen-Shot-2020-02-16-at-3.44.21-PM-1024x561.png 1024w, https:\/\/nmi.cool\/web\/wp-content\/uploads\/sites\/3\/2020\/02\/Screen-Shot-2020-02-16-at-3.44.21-PM-768x421.png 768w, https:\/\/nmi.cool\/web\/wp-content\/uploads\/sites\/3\/2020\/02\/Screen-Shot-2020-02-16-at-3.44.21-PM-1536x842.png 1536w, https:\/\/nmi.cool\/web\/wp-content\/uploads\/sites\/3\/2020\/02\/Screen-Shot-2020-02-16-at-3.44.21-PM-2048x1122.png 2048w, https:\/\/nmi.cool\/web\/wp-content\/uploads\/sites\/3\/2020\/02\/Screen-Shot-2020-02-16-at-3.44.21-PM-1568x859.png 1568w\" sizes=\"auto, (max-width: 2146px) 100vw, 2146px\" \/><figcaption>Our HTML and CSS at work! <\/figcaption><\/figure>\n\n\n\n<p>Okay, that&#8217;s great\u2014 at this point we have basic HTML and basic CSS. Try grabbing the corner or side of your browser window and drag it to make it smaller or larger. Nothing much changes, right? Our webpage <strong>isn&#8217;t<\/strong> responding to the screen size. Well let&#8217;s add a media query and see what we can do to make our page responsive! <\/p>\n\n\n\n<p>Copy and paste the following media query into your CSS document <strong>below<\/strong> the CSS we already added. Again, we&#8217;re not erasing anything\u2014 just adding the media query below what we already have. <\/p>\n\n\n\n<p><strong>This is important:<\/strong> Your browser will read your CSS from top to bottom, so media queries should always come below your default CSS styles. <\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">@media screen and (max-width: 800px) {\n    main {\n      background-color: aquamarine;\n    }\n    h1 {\n        font-size: 42px; \n    }\n    p {\n        font-size: 24px; \n    }\n  }<\/pre>\n\n\n\n<p>Save everything and refresh your browser window. Now try adjusting the size of your window. You should see a different color background and smaller text when your screen is anywhere from zero to 800 pixels wide. Once it hits 800 pixels\u2014 the maximum width of our media query\u2014 the color reverts to dark cyan and the text size increases. <\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1774\" height=\"1190\" src=\"https:\/\/nmi.cool\/newmediaproduction\/wp-content\/uploads\/sites\/3\/2020\/02\/Screen-Shot-2020-02-16-at-3.55.31-PM.png\" alt=\"The same webpage at 800 pixels with smaller text and an aqua background. \" class=\"wp-image-1513\" srcset=\"https:\/\/nmi.cool\/web\/wp-content\/uploads\/sites\/3\/2020\/02\/Screen-Shot-2020-02-16-at-3.55.31-PM.png 1774w, https:\/\/nmi.cool\/web\/wp-content\/uploads\/sites\/3\/2020\/02\/Screen-Shot-2020-02-16-at-3.55.31-PM-300x201.png 300w, https:\/\/nmi.cool\/web\/wp-content\/uploads\/sites\/3\/2020\/02\/Screen-Shot-2020-02-16-at-3.55.31-PM-1024x687.png 1024w, https:\/\/nmi.cool\/web\/wp-content\/uploads\/sites\/3\/2020\/02\/Screen-Shot-2020-02-16-at-3.55.31-PM-768x515.png 768w, https:\/\/nmi.cool\/web\/wp-content\/uploads\/sites\/3\/2020\/02\/Screen-Shot-2020-02-16-at-3.55.31-PM-1536x1030.png 1536w, https:\/\/nmi.cool\/web\/wp-content\/uploads\/sites\/3\/2020\/02\/Screen-Shot-2020-02-16-at-3.55.31-PM-1568x1052.png 1568w\" sizes=\"auto, (max-width: 1774px) 100vw, 1774px\" \/><figcaption>We should see smaller text and a bright aqua background when our screen is less than 800 pixels. <\/figcaption><\/figure>\n\n\n\n<p>Are you starting to get the hang of this? If things aren&#8217;t working you can check your code against the sample code on page 2, but do your best to troubleshoot on your own first. <\/p>\n\n\n\n<p>Go ahead and try adding another media query on your own this time. Maybe try using a <code>min-width<\/code> condition instead of a <code>max-width<\/code> and make your font size and background color change at the new breakpoint<a href=\"#footnote-2-1506\" id=\"note-2-1506\" rel=\"footnote\">2<\/a> that you establish. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\">To wrap up\u2026 <\/h2>\n\n\n\n<p>Media queries are a powerful component of responsive web design. We&#8217;ll see the same basic concept of breakpoints when we dig into the <a href=\"https:\/\/nmi.cool\/web\/bootstrap#applying-grid\" target=\"_blank\" rel=\"noreferrer noopener\">CSS grid<\/a> that underpins Bootstrap&#8217;s responsive design. <\/p>\n\n\n\n<p>You may not be coding many media queries by hand in Bootstrap, but it&#8217;s important to understand how media queries allow us to create websites that easily respond to different screen sizes and media types. <\/p>\n\n\n\n<p>To read more about media queries, check out these great references! <\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Reference Roundup: <\/h3>\n\n\n\n<p><strong>Media Query Syntax<\/strong>: The <a rel=\"noreferrer noopener\" aria-label=\"CSS @media Rule (opens in a new tab)\" href=\"https:\/\/www.w3schools.com\/cssref\/css3_pr_mediaquery.asp\" target=\"_blank\">CSS @media Rule<\/a> from w3schools. This is great if you need a reminder of the correct syntax for a media query. <\/p>\n\n\n\n<p><strong>A Deeper Dive into Media Queries<\/strong>: Mozilla Developer Network has more in-depth information on <a rel=\"noreferrer noopener\" aria-label=\"using media queries (opens in a new tab)\" href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/CSS\/Media_Queries\/Using_media_queries\" target=\"_blank\">using media queries<\/a> here. <\/p>\n\n\n\n<!--nextpage-->\n\n\n\n<h2 class=\"wp-block-heading\">HTML Example<\/h2>\n\n\n\n<pre class=\"wp-block-preformatted\">&lt;!DOCTYPE html&gt;\n&lt;html lang=\"en\"&gt;\n&lt;head&gt;\n    &lt;meta charset=\"UTF-8\"&gt;\n    &lt;meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\"&gt;\n    &lt;title&gt;Media Query&lt;\/title&gt;\n    &lt;link rel=\"stylesheet\" href=\"css\/media.css\"&gt;\n&lt;\/head&gt;\n&lt;body&gt;\n    &lt;main&gt;\n        &lt;h1&gt;I Love Responsive Web Design!&lt;\/h1&gt;\n        &lt;p&gt;Media queries are one of the primary components of responsive design. Web design is responsive when it changes based on things like viewport size, device, and screen orientation. &lt;\/p&gt;\n    &lt;\/main&gt;\n&lt;\/body&gt;\n&lt;\/html&gt;<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">CSS Example<\/h2>\n\n\n\n<pre class=\"wp-block-preformatted\">main {\n    background-color: darkcyan;\n    text-align: center;\n}\n\nh1 {\n    font-size: 60px;\n}\np {\n    font-size: 32px;\n}\n\n@media screen and (max-width: 800px) {\n    main {\n      background-color: aquamarine;\n    }\n    h1 {\n        font-size: 42px; \n    }\n    p {\n        font-size: 24px; \n    }\n  }<\/pre>\n<div class=\"footnotes\"><hr \/><ol><li id=\"footnote-1-1506\" class=\"footnote\"><p> Remember, responsive<strong>&nbsp;<\/strong>web design means a website&nbsp;<em>responds<strong>&nbsp;<\/strong><\/em>to the user\u2019s screen size, screen orientation, and platform.<a href=\"#note-1-1506\" class=\"footnote-return\">&#8617;<\/a><\/p><\/li><!--\/#footnote-1.footnote--><li id=\"footnote-2-1506\" class=\"footnote\"><p> A breakpoint is the name for the screen width that prompts the change. In our example, the breakpoint was 800px.<a href=\"#note-2-1506\" class=\"footnote-return\">&#8617;<\/a><\/p><\/li><!--\/#footnote-2.footnote--><\/ol><\/div><!--\/#footnotes-->","protected":false},"excerpt":{"rendered":"<p>What are Media Queries? Media queries are one of the primary components of responsive design. A media query is a specific CSS component that that lets you set up extra CSS rules for different viewports or screen sizes. For example, you might want your &lt;p&gt; element&#8217;s font size to be 28 pixels when viewed on &hellip; <a href=\"https:\/\/nmi.cool\/web\/media-queries\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Media Queries<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-1506","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/nmi.cool\/web\/wp-json\/wp\/v2\/pages\/1506","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nmi.cool\/web\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/nmi.cool\/web\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/nmi.cool\/web\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nmi.cool\/web\/wp-json\/wp\/v2\/comments?post=1506"}],"version-history":[{"count":4,"href":"https:\/\/nmi.cool\/web\/wp-json\/wp\/v2\/pages\/1506\/revisions"}],"predecessor-version":[{"id":3746,"href":"https:\/\/nmi.cool\/web\/wp-json\/wp\/v2\/pages\/1506\/revisions\/3746"}],"wp:attachment":[{"href":"https:\/\/nmi.cool\/web\/wp-json\/wp\/v2\/media?parent=1506"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}