Le potenzialità che offrono i fogli di stile sono davvero molte, e non si finisce mai di imparare.

Problematica del giorno, trovare il modo di tagliare (fare il crop) dell’immagine che trovate qui sopra al post, ma in modo automatico, senza che debba aprire Photoshop, Paint o altri applicativi. E mi sono messo alla ricerca.

Soluzione trovata!

Tramite lo stile object-fit è possibile indicare come l’immagine si deve adattare al tag img.

object-fit:

The object-fit CSS property sets how the content of a replaced element, such as an <img> or <video>, should be resized to fit its container.

Syntax

The object-fit property is specified as a single keyword chosen from the list of values below.

Values:

  • contain: The replaced content is scaled to maintain its aspect ratio while fitting within the element’s content box. The entire object is made to fill >the box, while preserving its aspect ratio, so the object will be “letterboxed” if its aspect ratio does not match the aspect ratio of the box.
  • cover: The replaced content is sized to maintain its aspect ratio while filling the element’s entire content box. If the object’s aspect ratio does >not match the aspect ratio of its box, then the object will be clipped to fit.
  • fill: The replaced content is sized to fill the element’s content box. The entire object will completely fill the box. If the object’s aspect ratio >does not match the aspect ratio of its box, then the object will be stretched to fit.
  • none: The replaced content is not resized.
  • scale-down: The content is sized as if none or contain were specified, whichever would result in a smaller concrete object size.

ATTENZIONE alla compatibilità con i vari browser: Browser_compatibility …non funziona con Internet Explorer.

Ecco lo stile che ho utilizzato in questo blog:

.post-header img {
    border-radius: 5px;
    object-fit: cover;
    height: 300px;
    width: 100%;
    display: block;
}