Site icon Tosolini.info

CSS3: Media Queries

Con l’avvento dei dispositivi mobili si è resa necessaria l’adozione di nuovi strumenti per i fogli di stile. Ne abbiamo visto uno molto importante con l’utilizzo del viewport. Un secondo metodo, da perseguire con l’utilizzo di viewport medesimo, sono le media queries o media rules.

Questo strumento è supportato dai maggiori browser, compreso Internet explorer dalla versione 9. In realtà il comando permette molte altre funzionalità, come ad esempio costruire delle regole CSS per versione stampabile, quello che vedremo in questo articolo è la possibilità di costruire regole CSS diverse a seconda dello schermo utilizzato dall’utente finale. Queste regole, combinate assieme permetteranno la creazione di un tema responsive.

Vediamo questa porzione di codice:

/* Desktop PC */
@media only screen and (max-width: 1210px) {
     .div { width:80vw; }
}

/* Tablet */
@media only screen and (max-width: 970px) {
     .div { width:90vw; }
}

/* Smartphone 6" */
@media only screen and (max-width: 600px) {
     .div { width:98vw; }
}

/* Smartphone 4" */
@media only screen and (max-width: 300px) {
     .div { width:100vw; }
}

Come si può vedere viene presentato il comando @media only screen seguito da una opzione che è il max-width e una indicazione in pixel. In sostanza il foglio di stile legge l’ampiezza del display e se questa ha una misurazione massima entro tale valore numerico, eseguirà il codice CSS in esso contenuto. Infatti faccio notare che il comando @media prevede l’utilizzo delle graffe. All’interno di queste il codice CSS classico, che a sua volta prevede altre graffe nidificate.

Il codice così composto con vari livelli di massima larghezza, permette di fatto di creare regole personalizzate a seconda della larghezza che ha lo schermo. E’ come se stessimo costruendo implicitamente un costrutto switch o if else concatenato. Se lo schermo è inferiore a 300px utilizzeremo la regola più bassa, a 650px quella relativa ai tablet fino ad arrivare alla regola per i desktop. Nel caso il display sia superiore a 1210px verrà comunque eseguita quest’ultima poiché non ne esiste una superiore.

All’interno di ognuna di queste regole possiamo giocare con i vari parametri a nostra discrezione, ovvero possiamo modificare il div content per tutti, oppure avere delle classi che possono essere resettate a livelli superiori o inferiori.

L’opzione max-width ovviamente non è l’unica, benché di fatto sia la più utilizzata, ma esiste una larga scelta di opzioni:

ValueDescription
any-hoverDoes any available input mechanism allow the user to hover over elements?
any-pointerIs any available input mechanism a pointing device, and if so, how accurate is it?
aspect-ratioThe ratio between the width and the height of the viewport
colorThe number of bits per color component for the output device
color-gamutThe approximate range of colors that are supported by the user agent and output device
color-indexThe number of colors the device can display
gridWhether the device is a grid or bitmap
heightThe viewport height
hoverDoes the primary input mechanism allow the user to hover over elements?
inverted-colorsIs the browser or underlying OS inverting colors?
light-levelCurrent ambient light level
max-aspect-ratioThe maximum ratio between the width and the height of the display area
max-colorThe maximum number of bits per color component for the output device
max-color-indexThe maximum number of colors the device can display
max-heightThe maximum height of the display area, such as a browser window
max-monochromeThe maximum number of bits per “color” on a monochrome (greyscale) device
max-resolutionThe maximum resolution of the device, using dpi or dpcm
max-widthThe maximum width of the display area, such as a browser window
min-aspect-ratioThe minimum ratio between the width and the height of the display area
min-colorThe minimum number of bits per color component for the output device
min-color-indexThe minimum number of colors the device can display
min-heightThe minimum height of the display area, such as a browser window
min-monochromeThe minimum number of bits per “color” on a monochrome (greyscale) device
min-resolutionThe minimum resolution of the device, using dpi or dpcm
min-widthThe minimum width of the display area, such as a browser window
monochromeThe number of bits per “color” on a monochrome (greyscale) device
orientationThe orientation of the viewport (landscape or portrait mode)
overflow-blockHow does the output device handle content that overflows the viewport along the block axis
overflow-inlineCan content that overflows the viewport along the inline axis be scrolled
pointerIs the primary input mechanism a pointing device, and if so, how accurate is it?
resolutionThe resolution of the output device, using dpi or dpcm
scanThe scanning process of the output device
scriptingIs scripting (e.g. JavaScript) available?
updateHow quickly can the output device modify the appearance of the content
widthThe viewport width
Exit mobile version