CSS Media Queries Flashcards

1
Q

What are CSS Media Queries?

A

The CSS Media Query gives you a way to apply CSS only when the browser and device environment matches a rule that you specify, for example “viewport is wider than 480 pixels”.

Source MDN

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is the syntax of a Media Query?

A

A media query is composed of an optional media type and any number of media feature expressions, which may optionally be combined in various ways using logical operators. Media queries are case-insensitive.

Media types define the broad category of device for which the media query applies: all, print, screen. The type is optional (assumed to be all) except when using the not or only logical operators.

Media features describe a specific characteristic of the user agent, output device, or environment:

Examples:

Targeting media types

@media print {
  / … /
}

Targeting media features

@media (color) {
  /* … */
}

Complex queries

@media screen and (min-width: 30em) and (orientation: landscape) {
  /* … */
}

@media screen and (min-width: 30em) and (orientation: landscape) {
  /* … */
}

Source MDN

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How can you negate a Media Query?

A

The not keyword inverts the meaning of an entire media query. It will only negate the specific media query it is applied to. (Thus, it will not apply to every media query in a comma-separated list of media queries.)

The not keyword can’t be used to negate an individual feature query, only an entire media query. The not is evaluated last in the following query:

@media not all and (monochrome) {
  /* … */
}

The above query is evaluated like this:

@media not (all and (monochrome)) {
  /* … */
}

As another example, the following media query:

@media not screen and (color), print and (color) {
  /* … */
}

This means that the above query is evaluated like this:

@media (not (screen and (color))), print and (color) {
  /* … */
}

Source MDN

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How can you test for multiple Media Queries?

A

You can use a comma-separated list to apply styles when the user’s device matches any one of various media types, features, or states. For instance, the following rule will apply its styles if the user’s device has either a minimum height of 680px or is a screen device in portrait mode:

@media (min-height: 680px), screen and (orientation: portrait) {
  /* … */
}

Source MDN

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How can you combine multiple types or features in a Media Query?

A

The and keyword combines a media feature with a media type or other media features.

This example combines two media features to restrict styles to landscape-oriented devices with a width of at least 30 ems:

@media (min-width: 30em) and (orientation: landscape) {
  /* … */
}

Source MDN

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How can you improve Media Query compatibility with older browsers?

A

The only keyword prevents older browsers that do not support media queries with media features from applying the given styles. It has no effect on modern browsers.

@media only screen and (color) {
  /* … */
}

Source MDN

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Rewrite this Media Query to the new level 4 syntax

@media (max-width: 30em) {
  /* … */
}
A
@media (width <= 30em) {  /* … */ }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Rewrite this Media Query to the new level 4 syntax

@media (min-width: 30em) and (max-width: 50em) {
  /* … */
}
A
@media (30em <= width <= 50em) {  /* … */ }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How can you negate a feature in Media Queries level 4?

A

Using not() around a media feature negates that feature in the query. For example, not(hover) would match if the device had no hover capability:

@media (not(hover)) {
  /* … */
}

Source MDN

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How can you test for multiple features in Media Queries level 4?

A

You can use or to test for a match among more than one feature, resolving to true if any of the features are true. For example, the following query tests for devices that have a monochrome display or hover capability:

@media (not (color)) or (hover) {
  /* … */
}

Source MDN

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

List all Media Queries level 3 useful features

A
  • width - Width of the viewport including width of scrollbar.
  • height - Height of the viewport.
  • aspect-ratio - Width-to-height aspect ratio of the viewport
  • resolution - Pixel density of the output device.
  • orientation - Orientation of the viewport.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What are Media Queries’ media types?

A

Media types describe the general category of a device. Except when using the not or only logical operators, the media type is optional and the all type is implied.

  • all - Suitable for all devices.
  • print - Intended for paged material and documents viewed on a screen in print preview mode. (Please see paged media for information about formatting issues that are specific to these formats.)
  • screen - Intended primarily for screens.

Source MDN

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

List and explain all Media Queries’ logical operators

A

The logical operators not, and, and only can be used to compose a complex media query. You can also combine multiple media queries into a single rule by separating them with commas.

and Used for combining multiple media features together into a single media query, requiring each chained feature to return true for the query to be true. It is also used for joining media features with media types.

not - Used to negate a media query, returning true if the query would otherwise return false. If present in a comma-separated list of queries, it will only negate the specific query to which it is applied. If you use the not operator, you must also specify a media type.

Note: In Level 3, the not keyword can’t be used to negate an individual media feature expression, only an entire media query.

only - Applies a style only if an entire query matches. It is useful for preventing older browsers from applying selected styles. If you use the only operator, you must also specify a media type.

, (comma) - Commas are used to combine multiple media queries into a single rule. Each query in a comma-separated list is treated separately from the others Thus, if any of the queries in a list is true, the entire media statement returns true. In other words, lists behave like a logical or operator.

or - Equivalent to the , operator. Added in Media Queries Level 4.

Source MDN

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Given this Media Queries:

  @media screen {
	  a {
		  color: cyan;
		  text-decoration: underline;
	  } 
  }
	
  @media print {
	  a::after {
		  content: " (" attr(href) ")";
	  } 
  }

What will the browser print depending on the media type?

A

On screen:
foo

On paper:

Foo (http://foo.com)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Is it possible to nest Media Queries?

A

Yes.

For example you could do:

@media screen and (orientation: portrait), print {
  @media (min-width: 20em) and (max-width: 40em) {
    …
  }
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What are the interaction media features?

A

Allows a media query to be set based on the presence and accuracy of the user’s pointing device, and whether they have the ability to hover over elements on the page. This includes the pointer, any-pointer, hover, and any-hover media features.

any-hover - Does any available input mechanism allow the user to hover over elements? Added in Media Queries Level 4.

any-pointer - Is any available input mechanism a pointing device, and if so, how accurate is it? Added in Media Queries Level 4.

pointer - Is the primary input mechanism a pointing device, and if so, how accurate is it? Added in Media Queries Level 4.

hover - Does the primary input mechanism allow the user to hover over elements? Added in Media Queries Level 4.

Source w3.org and MDN

16
Q

What is the range syntax of media queries level 4?

A

Syntax improvements to make media queries using features that have a “range” type (like width or height) less verbose. Can be used with ordinary mathematical comparison operators: >, <, >=, or <=.

For example:

@media (100px <= width <= 1900px) is the equivalent of @media (min-width: 100px) and (max-width: 1900px)

Source w3.org

17
Q

Explain the pointer CSS media feature

A

The pointer CSS media feature tests whether the user has a pointing device (such as a mouse), and if so, how accurate the primary pointing device is.

Note: If you want to test the accuracy of any pointing device, use any-pointer instead.

Syntax
The pointer feature is specified as a keyword value chosen from the list below.

none - The primary input mechanism does not include a pointing device.

coarse - The primary input mechanism includes a pointing device of limited accuracy.

fine - The primary input mechanism includes an accurate pointing device.

Example:

@media (pointer: fine) {
  input[type="checkbox"] {
    width: 15px;
    height: 15px;
    border-width: 1px;
    border-color: blue;
  }
}

@media (pointer: coarse) {
  input[type="checkbox"] {
    width: 30px;
    height: 30px;
    border-width: 2px;
    border-color: red;
  }
}

Source MDN

18
Q

Explain the hover CSS media feature

A

The hover CSS media feature can be used to test whether the user’s primary pointing mechanism can hover over elements.

Syntax

The hover feature is specified as a keyword value chosen from the list below.

none - The primary input mechanism cannot hover at all or cannot conveniently hover (e.g., many mobile devices emulate hovering when the user performs an inconvenient long tap), or there is no primary pointing input mechanism.

hover - The primary input mechanism can conveniently hover over elements.

Example:

@media (hover: hover) {
  /* when hover is supported */
  a:hover {
    color: white;
    background: black;
  }
}

Source MDN

19
Q

Explain the update CSS media feature

A

The update CSS media feature can be used to test how frequently (if at all) the output device is able to modify the appearance of content.

Syntax
The update feature is specified as a single keyword value chosen from the list below.

none - Once it has been rendered, the layout can no longer be updated. Example: documents printed on paper.

slow - The layout may change dynamically according to the usual rules of CSS, but the output device is not able to render or display changes quickly enough for them to be perceived as a smooth animation. Examples: e-book readers or severely underpowered devices.

fast - The layout may change dynamically according to the usual rules of CSS, and the output device is not unusually constrained in speed, so regularly-updating things like CSS Animations can be used. Example: computer screens.

Note: this feature is still considered experimental. Check its browser compatibility before using it.

Source MDN

20
Q

Explain the overflow-block CSS media feature

A

The overflow-block CSS media feature can be used to test how the output device handles content that overflows the initial containing block along the block axis.

Syntax
The overflow-block feature is specified as a keyword value chosen from the list below.

none - Content that overflows the block axis is not displayed.

scroll - Content that overflows the block axis can be seen by scrolling to it.

optional-paged - Content that overflows the block axis can be seen by scrolling to it, but page breaks can be manually triggered (such as via break-inside, etc.) to cause the following content to display on the following page.

paged - Content is broken up into discrete pages; content that overflows one page in the block axis is displayed on the following page.

NOTE: support for this feature is still limited. Check its browser compatibility before using it

Source MDN

21
Q

Explain the scripting CSS media feature

A

The scripting CSS media feature can be used to test whether scripting (such as JavaScript) is available.

Syntax
The scripting feature is specified as a keyword value chosen from the list below.

none - Scripting is completely unavailable on the current document.

initial-only - Scripting is enabled during the initial page load, but not afterwards.

enabled - Scripting is supported and active on the current document.

NOTE: This media feture is part of level 5 specification and not yet supported. Please check browser compatibility before using it.

Source MDN

22
Q

Explain the difference between pointer and any-pointer CSS media features

A

The any-pointer CSS media feature tests whether the user has any pointing device (such as a mouse) while pointer tests the primary pointing device.

Everything else remains the same.

Source MDN

23
Q

Explain the difference between hover and any-hover CSS media features

A

The any-hover CSS media feature can be used to test whether any available input mechanism can hover over elements. While hover CSS media tests whether the user’s primary input mechanism can hover over elements.

Everything else remains the same

Source MDN

24
Q

When should you add a break point with CSS Media Queries?

A

You should add a breakpoint when your design breaks.

NOTE: avoid antipatterns like creating breakpoints for popular devices.

25
Q

Why should you avoid breakpoints based on popular devices?

A

Because popular devices change from time to time and from once version to the next version. And you will need to keep updating the breakpoints

For example:

@media (width: 320px) { /* iPhone design */ }
@media (width: 320px), (width: 375px) { /* iPhone design */ }
@media (width: 320px), (width: 375px), (width: 414px) { /* iPhone design */ }

Instead you should add breakpoints whenever your design breaks/changes

@media (width < 20em) { /* narrow design*/ }
26
Q

What units should you use on your Media Queries?

A

You should favor em units instead of px units.

They will give a consistent result if the user has larger text that you expected.

27
Q

How can you do progressive enhancement with media queries?

A

Testing for media features:

@media (hover) {
    .can-i-hover::after {
        content: "You look like you can hover.";    
    } 
}

@media  (hover:none) {
    .can-i-hover::after {
        content: "I don't think you can hover.";    
    } 
}