Media queries allow you to apply CSS styles depending on a device's media type (such as print vs. screen) or other features or characteristics such as screen resolution or orientation, aspect ratio, browser viewport width or height, user preferences such as preferring reduced motion, data usage, or transparency.
Colt SteeleThe Web Developer Bootcamp 2024
Responsive Web Design
Flexbox & Responsive Web Design Course PDFMedia queries are used for the following:
- To conditionally apply styles with the CSS @media and @import at-rules.
- To target specific media for the <style>, <link>, <source>, and other HTML elements with the media= or sizes=" attributes.
- To test and monitor media states using the Window.matchMedia() and EventTarget.addEventListener() methods.
The most commonly used properties are based on viewport size:
To use media queries, you must fist add the following to the < head> section of your webpage:
- < meta name="viewport" content="width=device-width, initial-scale=1.0">
- MDN Viewport meta tag
Then in CSS you would make a statement like :
- @media (min-width: 600px) {
- enter styling code blocks here
- }
You can also combine queries, like so:
- @media (min-width: 600px) and (max-width: 1000px) {
- enter styling code blocks here
- }
Device Breakpoints
W3Schools Typical Device Breakpoints
At this time in 2024, there are no set standards for "Typical Device Breakpoints", there are a few general observations.
- Extra small devices (phones, 600px and down)
@media only screen and (max-width: 600px)
- Small devices (portrait tablets and large phones, 600px and up)
@media only screen and (min-width: 600px)
- Medium devices (landscape tablets, 768px and up)
@media only screen and (min-width: 768px)
- Large devices (laptops/desktops, 992px and up)
@media only screen and (min-width: 992px)
- Extra large devices (large laptops and desktops, 1200px and up)
@media only screen and (min-width: 1200px)
From the Masyntech class:
- Mobile devices: 480 pixels or less
- Tablets and iPads: 481 to 768 pixels
- Small screens and laptops: 769 to 1024 pixels
- Desktops and large screens: 1025 to 1200 pixels
- Extra large screens and TVs: 1201 pixels and greater
Change the size of the browser window to see the effect.
Media Queries
Here's the code:
HTML
-
< nav class="navbar">
- < a href="#home">Home< /a>
-
< ul>
- < li> < a href="#Home">Learn More< /a> < /li>
- < li> < a href="#Home">About< /a> < /li>
- < a href="#signup">Sign Up< /a>
- < /ul>
- < a href="#Home">Contact< /a>
- < /nav>
- < h1>Media Queries< /h1>
CSS
- h1 {
- font-size: 6em;
- }
- .navbar {
- font-size: 1.5em;
- display: flex;
- justify-content: space-around;
- }
- .navbar ul,
.navbar li {- display: inline-block;
- margin: 0;
- padding: 0;
- }
- .navbar ul {
- flex: 1; - /* makes the "nav" part of the navbar take up all the extra space in the navbar */
- max-width: 50%; - /* Limits that extra space to 50% of the available space */
- display: flex; - /* makes it it's own flex container to control it's spacing */
- justify-content: space-evenly; - /* space the content in it's own flex container */
- }
- @media only screen and (max-width: 1600px) {
- h1 {
- font-size: 4em;
- color: blue;
- }
- .navbar,
.navbar ul {- font-size: 16px;
- }
- h1 {
- }
- @media only screen and (max-width: 1200px) {
- h1 {
- font-size: 3em;
- color: green;
- }
- .navbar,
.navbar ul {- font-size: 14px;
- }
- h1 {
- }
- @media only screen and (max-width: 992px) {
- h1 {
- font-size: 2.5em;
- color: yellow;
- }
- .navbar,
.navbar ul {- font-size: 12px;
- }
- h1 {
- }
- @media only screen and (max-width: 768px) {
- h1 {
- font-size: 2em;
- color: orange;
- }
- .navbar,
.navbar ul {- flex-direction: column;
- align-items: center;
- font-size: 10px;
- }
- h1 {
- }
- @media only screen and (max-width: 600px) {
- h1 {
- font-size: 1.2em;
- color: red;
- }
- .navbar,
.navbar ul {- flex-direction: column;
- align-items: center;
- font-size: 8px;
- }
- h1 {
- }