(68) (RWD Media Queries)

Ace your homework & exams now with Quizwiz!

Orientation: Portrait / Landscape

Media queries can also be used to change layout of a page depending on the orientation of the browser. You can have a set of CSS properties that will only apply when the browser window is wider than its height, a so called "Landscape" orientation: <!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <style> body { background-color: lightgreen; } @media only screen and (orientation: landscape) { body { background-color: lightblue; } } </style> </head> <body> <p>Resize the browser window. When the width of this document is larger than the height, the background color is "lightblue", otherwise it is "lightgreen".</p> </body> </html>

What is a media query?

Media query is a CSS technique introduced in CSS3. It uses the @media rule to include a block of CSS properties only if a certain condition is true. Example If the browser window is 600px or smaller, the background color will be lightblue: <!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <style> body { background-color: lightgreen; } @media only screen and (max-width: 600px) { body { background-color: lightblue; } } </style> </head> <body> <p>Resize the browser window. When the width of this document is 600 pixels or less, the background-color is "lightblue", otherwise it is "lightgreen".</p> </body> </html>

Always Design for Mobile First

Mobile First means designing for mobile before designing for desktop or any other device (This will make the page display faster on smaller devices). This means that we must make some changes in our CSS. Instead of changing styles when the width gets smaller than 768px, we should change the design when the width gets larger than 768px. This will make our design Mobile First: <!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <style> * { box-sizing: border-box; } .row::after { content: ""; clear: both; display: table; } [class*="col-"] { float: left; padding: 15px; } html { font-family: "Lucida Sans", sans-serif; } .header { background-color: #9933cc; color: #ffffff; padding: 15px; } .menu ul { list-style-type: none; margin: 0;

Typical Device Breakpoints

There are tons of screens and devices with different heights and widths, so it is hard to create an exact breakpoint for each device. To keep things simple you could target five groups: @media only screen and (min-width: 768px) { .example {background: blue;} } /* Large devices (laptops/desktops, 992px and up) */ @media only screen and (min-width: 992px) { .example {background: orange;} } /* Extra large devices (large laptops and desktops, 1200px and up) */ @media only screen and (min-width: 1200px) { .example {background: pink;} } </style> </head> <body> <h2>Typical Media Query Breakpoints</h2> <p class="example">Resize the browser window to see how the background color of this paragraph changes on different screen sizes.</p> </body> </html>

Hide Elements With Media Queries

Another common use of media queries, is to hide elements on different screen sizes: <!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width, initial-scale=1"> <style> div.example { background-color: yellow; padding: 20px; } @media screen and (max-width: 600px) { div.example { display: none; } } </style> </head> <body> <h2>Hide elements on different screen sizes</h2> <div class="example">Example DIV.</div> <p>When the browser's width is 600px wide or less, hide the div element. Resize the browser window to see the effect.</p> </body> </html>

Add a Breakpoint

Earlier in this tutorial we made a web page with rows and columns, and it was responsive, but it did not look good on a small screen. Media queries can help with that. We can add a breakpoint where certain parts of the design will behave differently on each side of the breakpoint. Use a media query to add a breakpoint at 768px: Example When the screen (browser window) gets smaller than 768px, each column should have a width of 100%: <!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <style> * { box-sizing: border-box; } .row::after { content: ""; clear: both; display: block; } [class*="col-"] { float: left; padding: 15px; } html { font-family: "Lucida Sans", sans-serif; } .header { background-color: #9933cc; color: #ffffff; padding: 15px; } .menu ul { list-style-type: none; margin: 0; padding: 0; } .menu li { padding: 8px; margin-bottom: 7px;

Another Breakpoint

You can add as many breakpoints as you like. We will also insert a breakpoint between tablets and mobile phones. We do this by adding one more media query (at 600px), and a set of new classes for devices larger than 600px (but smaller than 768px): Example Note that the two sets of classes are almost identical, the only difference is the name (col- and col-s-): /* For mobile phones: */ [class*="col-"] { width: 100%; } @media only screen and (min-width: 600px) { /* For tablets: */ .col-s-1 {width: 8.33%;} .col-s-2 {width: 16.66%;} .col-s-3 {width: 25%;} .col-s-4 {width: 33.33%;} .col-s-5 {width: 41.66%;} .col-s-6 {width: 50%;} .col-s-7 {width: 58.33%;} .col-s-8 {width: 66.66%;} .col-s-9 {width: 75%;} .col-s-10 {width: 83.33%;} .col-s-11 {width: 91.66%;} .col-s-12 {width: 100%;} } @media only screen and (min-width: 768px) { /* For desktop: */ .col-1 {width: 8.33%;} .col-2 {width: 16.66%;} .col-3 {width: 25%;} .col-4 {width: 33.33%;} .col-5 {width: 41.66%;} .col-6 {width: 50%;} .col-7 {width: 58.33%;} .col-8 {width: 66.66%;} .col-9 {width: 75%;} .col-10 {width: 83.33%;} .col-11 {width: 91.66%;} .col-12 {width: 100%;} } It might seem odd that we have two sets of identical classes, but it gives us the opportunity in HTML, to decide what will happen with the columns at each breakpoint: HTML Example For desktop: The first and the third section will both span 3 columns each. The middle section will span 6 columns. For tablets: The first section will span 3 columns, the second will span 9, and the third section will be displayed below the first two sections, and it will span 12 columns: <!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <style> * { box-sizing: border-box; } .row::after { content: ""; clear: both; display: table; } [class*="col-"] { float: left; padding: 15px; } html { font-family: "Lucida Sans", sans-serif; } .header { background-color: #9933cc; color: #ffffff; padding: 15px; } .menu ul { list-style-type: none; margin: 0; padding: 0; } .menu li { padding: 8px; margin-bottom: 7px; background-color: #33b5e5; color: #ffffff; box-shadow: 0 1px 3px rgba(0,0,0,0.12), 0 1px 2px rgba(0,0,0,0.24); } .menu li:hover { background-color: #0099cc; } .aside { <div class="col-6 col-s-9"> <h1>The City</h1> <p>Chania is the capital of the Chania region on the island of Crete. The city can be divided in two parts, the old town and the modern city.</p> </div> <div class="col-3 col-s-12"> <div class="aside"> <h2>What?</h2> <p>Chania is a city on the island of Crete.</p> <h2>Where?</h2> <p>Crete is a Greek island in the Mediterranean Sea.</p> <h2>How?</h2> <p>You can reach Chania airport from all over Europe.</p> </div> </div> </div> <div class="footer"> <p>Resize the browser window to see how the content respond to the resizing.</p> </div> </body> </html>

Change Font Size With Media Queries

You can also use media queries to change the font size of an element on different screen sizes: Variable Font Size. <!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width, initial-scale=1"> <style> div.example { background-color: lightgrey; padding: 20px; } @media screen and (min-width: 600px) { div.example { font-size: 80px; } } @media screen and (max-width: 600px) { div.example { font-size: 30px; } } </style> </head> <body> <h2>Change the font size of an element on different screen sizes</h2> <div class="example">Example DIV.</div> <p>When the browser's width is 600px wide or less, set the font-size of DIV to 30px. When it is 601px or wider, set the font-size to 80px. Resize the browser window to see the effect.</p> </body> </html>


Related study sets

Chapter 16 - Market Research and Product Testing

View Set

US History Since 1877 Final Exam Review

View Set

Chapter 33 children and adolescents, Chapter 24: Children and Adolescents, Chapter 24: Children and Adolescents, Chapter 26 - Children & Adolescents

View Set

Health Assessment Test #1 COMBINED OFFICIAL

View Set

Pediatric Nursing HESI Case Study Cleft Lip and Cleft Palate (33 Questions with Background Info Given)

View Set