CSS Aspect Ratio

A presentation at Web Directions hover in April 2021 in by Anton Ball

Slide 1

Slide 1

CSS Aspect Ratio Anton Ball ● Web Directions Hover 2021 antonjb

Slide 2

Slide 2

CSS Aspect Ratio antonjb

Slide 3

Slide 3

600 800 antonjb

Slide 4

Slide 4

3 4 antonjb

Slide 5

Slide 5

4:3 width height antonjb

Slide 6

Slide 6

1:1 antonjb

Slide 7

Slide 7

4:3 antonjb

Slide 8

Slide 8

16:9 antonjb

Slide 9

Slide 9

1.6180:1 antonjb

Slide 10

Slide 10

The Problem antonjb

Slide 11

Slide 11

antonjb

Slide 12

Slide 12

antonjb

Slide 13

Slide 13

antonjb

Slide 14

Slide 14

antonjb

Slide 15

Slide 15

What we do now antonjb

Slide 16

Slide 16

56.25% Padding Top Hack antonjb

Slide 17

Slide 17

antonjb

Slide 18

Slide 18

FitVids

Slide 19

Slide 19

Yesssss! Soon I will be released from my Open Source prison! FitVids Seeing it working in Edge Dev 89 (M1) as well. Dave Rupert @davatron5000

Slide 20

Slide 20

aspect-ratio antonjb

Slide 21

Slide 21

aspect-ratio: auto | <ratio> property value antonjb

Slide 22

Slide 22

aspect-ratio: 16 / 9 antonjb

Slide 23

Slide 23

Padding Top Hack .16-9-container { position: relative; width: 100%; padding-top: 56.25%; } .content { position: absolute; top: 0; } Aspect Ratio .16-9-container { width: 100%; aspect-ratio: 16 / 9; } antonjb

Slide 24

Slide 24

iframe { width: 100%; aspect-ratio: 16 / 9; } https://codepen.io/antonjb/pen/xxgbxEq antonjb

Slide 25

Slide 25

.grid-item { aspect-ratio: 4 / 3; } https://codepen.io/antonjb/pen/Jjbzzgb antonjb

Slide 26

Slide 26

.square { width: 20vw; aspect-ratio: 1; } https://codepen.io/antonjb/pen/LYbvQVr antonjb

Slide 27

Slide 27

Intrinsic Aspect Ratio antonjb

Slide 28

Slide 28

iframe video embed img antonjb

Slide 29

Slide 29

img, input[type=”image”], video, embed, iframe, marquee, object, table { aspect-ratio: attr(width) / attr(height); } https://developer.mozilla.org/en-US/docs/Web/CSS/aspect-ratio antonjb

Slide 30

Slide 30

https://caniuse.com/mdn-html_elements_img_aspect_ratio_computed_from_attributes antonjb

Slide 31

Slide 31

Preferred Aspect Ratio antonjb

Slide 32

Slide 32

“ The aspect-ratio CSS property sets a preferred aspect ratio for the box, which will be used in the calculation of auto sizes and some other layout functions. https://developer.mozilla.org/en-US/docs/Web/CSS/aspect-ratio antonjb

Slide 33

Slide 33

“ The width CSS property sets an element’s width. https://developer.mozilla.org/en-US/docs/Web/CSS/width antonjb

Slide 34

Slide 34

“ Maybe it’s helpful to think of aspect-ratio as the weakest way to size an element? Chris Coyier @chriscoyier https://css-tricks.com/a-first-look-at-aspect-ratio/ antonjb

Slide 35

Slide 35

800 width: 800px; aspect-ratio: 4 / 3; ? .container { 4:3 } antonjb

Slide 36

Slide 36

800 width: 800px; height: 600px; aspect-ratio: 16 / 9; 600 .container { 16:9 } antonjb

Slide 37

Slide 37

800 max-width: 800px; height: 600px; aspect-ratio: 16 / 9; 600 .container { 16:9 } antonjb

Slide 38

Slide 38

.element { .element { width: 20vw; width: 20vw; aspect-ratio: 16 / 9; min-height: 0; } aspect-ratio: 16 / 9; } https://codepen.io/antonjb/pen/zYoXRNR antonjb

Slide 39

Slide 39

layout.css.aspect-ratio.enabled https://caniuse.com/mdn-css_properties_aspect-ratio antonjb

Slide 40

Slide 40

Safari Technical Preview 118 is out… My new favorite CSS property on this list. !”# ASPECT-RATIO! Jen Simmons @jensimmons https://twitter.com/jensimmons/status/1347287421633892356 antonjb

Slide 41

Slide 41

Web Compat 2021 https://web.dev/compat2021/ antonjb

Slide 42

Slide 42

@supports (aspect-ratio: 1 / 1) { .16-9-element { aspect-ratio: 16/9; } } antonjb

Slide 43

Slide 43

CSS Aspect Ratio antonjb

Slide 44

Slide 44

Thank you antonjb