Which @media queries to use to reach the main devices taking into account screen dimensions and resolutions?
How do you scope devices by groups (for example, all tablets) within a media query to apply style rules to them?
The intention is to take a broader view of media queries, not @ rules in general.
Definition
A media query consists of a media type and at least one query that limits style sheets using media characteristics such as width, height, and color. It is understood as a CSS3 module that allows adapting the representation of the content to the characteristics of the device.
Added in CSS3, media queries allow the presentation of content to adapt to a specific range of output devices without having to change the content itself. It is what is called in English a responsive design , that is, it adapts to each device according to the size of the screen.
Syntax
Media queries consist of a media type, represented in CSS by
@media
, and one or more expressions, implying media characteristics, which resolve to true or false. The query result is true if the media type specified in the media query matches the type of device being displayed and all expressions in the media query are true.Queries are case insensitive. Media queries containing unknown media types will always return false.
Like any other style element, media queries can be used in an
<link />
HTML element, which will refer to a file with a .css extension:Or directly, without going through the link element:
When a media query is true, the corresponding style sheet or style rules are applied, following the normal cascading rules.
When within a media query neither is used
not
nor does itonly
mean that the media type is optional and will be interpreted asall
.Logical operators
Queries can be written using logical operators, including
not
,and
, andonly
.Additionally you can combine multiple queries into a multiple comma separated list; if any of the queries in the list is true, the associated style sheet is applied. This is equivalent to a logical operation "
or
".and
The operator
and
is used to put together multiple multimedia functions. A basic query with the media type specified asall
might look like this:If you want to apply this query only if the screen is in horizontal format, you can use the operator
and
and place the following string:The following query will only return true if the window is 700px wide or more and the screen is landscape. Now if you want to apply this option only if the type of media is a television (tv), it would be done with the following channel:
This query only applies if the media type is TV, the window is 700px wide or larger, and the screen is landscape.
comma separated list
Comma separated lists behave like the operator
or
when used in media queries. When you use a comma separated list and one of the queries returns true, the style or style sheet will be applied. Each query in a comma separated list is treated as an individual query and any operator applied to one medium will not affect the others. This means that each query in a comma-separated list can target different media, types, and states.If you want to apply a series of styles to a device with a minimum width of 700px or if the device is placed horizontally, it would be done like this:
So if it's on a screen with a window 800px wide, the middle statement will return true because of the first part of the list, if we apply this to a device it
@media all and (min-width: 700px)
might return true despite the fact that the screen fails. the media type check of the handheld in the second query. On the other hand if it were on a handheld with a window width of 500px, the first part of the list would fail but the second part would return true because of the middle declaration.not
The operator
not
applies to the entire query and returns true if possible and false if not (such as monochrome on a color monitor or a window with a minimum widthmin-width: 700px
of 600px on a monitor). Anot
will negate a query if possible but not all possible queries if they are located in a comma-separated list. The operatornot
cannot be used to negate an individual query, only for an entire query. For example, thenot
in the following query is evaluated last:This means that the query is evaluated as follows:
... and not like this:
Another example:
It is evaluated as follows:
only
The operator
only
prevents older browsers that don't support queries with functions from applying the assigned styles:Media queries by device groups
Here are the media queries used in the main devices currently on the market. This code is found at css-tricks.com
Note that for the same brand of device, the queries vary because these devices do not have the same screen sizes.
phones
iPhone
Galaxy
google-pixel
HTC
windows phone
tablets
iPad
Galaxy
nexus
Kindle Fire
laptop
Media queries only taking into account whether they have a retina screen or not.
watches
Apple Watch
Moto 360 Watch
Links:
En aras de la completud de la respuesta de Cedano, existen algunas otras posibilidades para media queries:
Publicaciones
Se usa generalmente para la versión para imprimir una página, muy deseable si tu sitio es un libro, revista o peródico
Es ideal para eliminar los elementos que no conviene imprimir. Para márgenes se usa en conjunto con la regla
@page
que no es exclusiva de medios de tipoprint
por lo que puede ir dentro o afuera.Para verificar el demo anterior se recomienda seleccionar todo el texto y oprimir las teclas Ctrl+P en Windows o Cmd+P en Mac.
El resultado visual será similar a este:
Lectores de pantalla
Para dispositivos que soporten la lectura del contenido, a la fecha el mejor soporte de esto existía en el navegador Safari hasta que por cuestiones de patentes retiraron la funcionalidad hace ya varios años.
El css para personas débiles visuales o que no cuentan con el sentido de la visión fue retomado por la especificación de CSS3 con el advenimiento de HTML5 pero a la fecha no existe ninguna implementación, en firefox se solicitó hace casi 20 años esta característica sin avances significativos.
speech es el remplazo de la versión ahora deprecada de CSS2
Con esto se completan los media queries especificados en Media Queries Nivel 4
En Media Queries Nivel 3 que está en proceso de obsolesencia, se hallaban incluso más características adicionales para otros medios como:
Proyectores
Televisiones
Lectores de Braile
HTML4 hizo obsoletos los media queries para lectores de braile paginados de CSS2
@media embossed
, para ello se definió el media querySi se medita un pococ en el ejemplo se verá que está pensado como otra forma de distribución de contenido, pero no está pensado en facilitar la vida a quien use esta tecnología asistiva.
Terminales como navegador
Como el HTML se originó con propósitos en mente muy diferentes a los que ahora cumple, también en CSS2 se tiene el media query
tty
para navegadores web de solo texto como lynx¿Qué media queries soporta el navegador con que estoy leyendo este artículo?
Si deseas saber que tipos de media queries reconoce el navegador que usas es recomendable acceder a esta página
Fuentes