Appunti CSS3 nuove unità di misura VW E VH e Justify content Flex

Concentrati di gusto | Dins

Appunti CSS3 nuove unità di misura VW E VH e Justify content Flex

VW (VIEWPORT WIDTH) E VH (VIEWPORT HEIGHT)

VW (Viewport Width) e VH (Viewport Height) sono due nuove unità di misura introdotte dalle specifiche CSS 3 che possono rivelarsi particolarmente utili e preziose nei design responsive perché forniscono un rapporto diretto con le dimensioni della Viewport (dove con viewport si intende identificare la dimensione della finestra del browser), evitando quindi, in molti casi, di dover scrivere apposite media query.

Quando viene modificata l’altezza o la larghezza della finestra del browser (ossia della viewport) la dimensione degli elementi gestita mediante queste unità di misura varierà di conseguenza in maniera completamente automatica.

Nello specifico l’unità di misura

  • VW (Viewport Width): gestisce il dimensionamento di un elemento in relazione alla larghezza della finestra del browser. L’unità vw è pari all’1% della larghezza della viewport.

In queste condizioni, dunque, per rendere un elemento ampio sempre quanto l’intera larghezza della finestra del browser sarà necessario impostare la sua width sul valore 100vw.

Allo stesso modo, la larghezza della viewport fosse ad esempio di 1920px, impostando la dimensione del font di un elemento sul valore 1vw il suo font-size sarebbe esattamente di 19.2 px. Restringendo le dimensioni della finestra a 1024px il font size di quello stesso elemento passerà in maniera automatica al valore 10.24px.

  • VH (Viewport Height): gestisce il dimensionamento di un elemento in relazione all’altezza della finestra del browser. L’unità vh è pari all’1% dell’altezza della viewport.

In queste condizioni, dunque, per rendere un elemento alto sempre come l’intera finestra del browser sarà necessario impostare la sua height sul valore 100vh.

In sostanza dunque il grande vantaggio che si può avere nell’utilizzare queste unità di misura è rappresentato dal fatto che esse creano un rapporto diretto con le dimensioni della finestra del browser e, di fatto, con le diverse risoluzioni dei dispositivi di visualizzazione, permettendoci, ad esempio di scalare le dimensione del testo per ogni formato di schermo senza il bisogno di utilizzare apposite media query.

Inoltre essendo delle unità di misura possono essere utilizzate, secondo lo stesso principio, non solo per il font del testo ma anche su di una qualsiasi altra proprietà CSS che accetti come valore una lunghezza.

Il lato negativo della medaglia è rappresentato dalla compatibilità con i diversi browser, che comunque ha raggiunto al giorno d’oggi, valori più che accettabili come è possibile verificare al seguente indirizzo:

In ogni caso è sempre possibile fornire un ripiego ai browser che non supportano queste unità di misura scrivendo regole CSS che i browser più vecchi possano comprendere, e che vengano invece sovrascritte per i browser più moderni dall’analoga regola che utilizza però i valori di vw e vh come nell’esempio di seguito riportato

h1 {
font-size: 36px; font-size: 5.4vw;
margin-top: 21px; margin-top: 7vw;
}

FLEX

http://www.flexboxfroggy.com/

Property Values

justify-content: flex-start|flex-end|center|space-between|space-around|space-evenly|initial|inherit;

ValueDescriptionPlay it
flex-startDefault value. Items are positioned at the beginning of the container
flex-endItems are positioned at the end of the container
centerItems are positioned in the center of the container
space-betweenItems will have space between them
space-aroundItems will have space before, between, and after them
space-evenlyItems will have equal space around them
initialSets this property to its default value. Read about initial
inheritInherits this property from its parent element. Read about inherit
https://www.w3schools.com/cssref/css3_pr_justify-content.php

CSS Syntax

justify-items: legacy|normal|stretch|positional alignment|overflow-alignment|baseline alignment|initial|inherit;

Property Values

ValueDescriptionPlay it
legacyDefault value. Grid items with justify-self value ‘auto’ only inherits grid container justify-items property value if it starts with ‘legacy’. It exists to implement the legacy alignment behavior of HTML’s 〈center〉 element and align attribute.
normalDependant on layout context, but similar to ‘stretch’ for grid layout
stretchStretches to fill the grid cell if inline-size (width) is not set.
startAlign items at the start in the inline direction
leftAlign items to the left
centerAlign items to the center
endAlign items at the end in the inline direction
rightAlign items to the right
overflow-alignment‘safe’ sets alignment of the item to ‘start’ if the content overflows ‘unsafe’ keeps the alignment value regardless of wether or not the item content overflows
baseline alignmentThe element is aligned with the baseline of the parent.
initialSets this property to its default value. Read about initial
inheritInherits this property from its parent element. Read about inherit
https://www.w3schools.com/cssref/css_pr_justify-items.php

CSS Syntax

direction: ltr|rtl|initial|inherit;

Property Values

ValueDescriptionDemo
ltrText direction goes from left-to-right. This is default
rtlText direction goes from right-to-left
initialSets this property to its default value. Read about initial
inheritInherits this property from its parent element. Read about inherit

CSS Syntax

display: value;

Property Values

ValueDescriptionPlay it
inlineDisplays an element as an inline element (like <span>). Any height and width properties will have no effect. This is default.
blockDisplays an element as a block element (like <p>). It starts on a new line, and takes up the whole width
contentsMakes the container disappear, making the child elements children of the element the next level up in the DOM
flexDisplays an element as a block-level flex container
gridDisplays an element as a block-level grid container
inline-blockDisplays an element as an inline-level block container. The element itself is formatted as an inline element, but you can apply height and width values
inline-flexDisplays an element as an inline-level flex container
inline-gridDisplays an element as an inline-level grid container
inline-tableThe element is displayed as an inline-level table
list-itemLet the element behave like a <li> element
run-inDisplays an element as either block or inline, depending on context
tableLet the element behave like a <table> element
table-captionLet the element behave like a <caption> element
table-column-groupLet the element behave like a <colgroup> element
table-header-groupLet the element behave like a <thead> element
table-footer-groupLet the element behave like a <tfoot> element
table-row-groupLet the element behave like a <tbody> element
table-cellLet the element behave like a <td> element
table-columnLet the element behave like a <col> element
table-rowLet the element behave like a <tr> element
noneThe element is completely removed
initialSets this property to its default value. Read about initial
inheritInherits this property from its parent element. Read about inherit

CSS Syntax

flex: flex-growflex-shrinkflex-basis|auto|initial|inherit;

Property Values

ValueDescriptionDemo
flex-growA number specifying how much the item will grow relative to the rest of the flexible items
flex-shrinkA number specifying how much the item will shrink relative to the rest of the flexible items
flex-basisThe length of the item. Legal values: “auto”, “inherit”, or a number followed by “%”, “px”, “em” or any other length unit
autoSame as 1 1 auto.
initialSame as 0 1 auto. Read about initial
noneSame as 0 0 auto.
inheritInherits this property from its parent element. Read about inherit

CSS Syntax

flex-direction: row|row-reverse|column|column-reverse|initial|inherit;

Property Values

ValueDescriptionPlay it
rowDefault value. The flexible items are displayed horizontally, as a row
row-reverseSame as row, but in reverse order
columnThe flexible items are displayed vertically, as a column
column-reverseSame as column, but in reverse order
initialSets this property to its default value. Read about initial
inheritInherits this property from its parent element. Read about inherit

 

Back to Top