S r c s e t a n d s i z e s
M a r c h 2 4 , 2 0 1 4
T r a n s l a t i o n s :
日 本 語 ( J a p a n e s e ) by 鈴 木 丈 ( T a k e r u S u z u k i )
中 文 ( C h i n e s e ) by 陈 三 ( S a m C h e n )
P o r t u g u ê s by T á r c i o Z e m e l
P a r t 1 : W h a t ’ s W r o n g W i t h M e d i a Q u e r i e s ?
S o l e t ’ s s a y y o u ’ r e m a k i n g a w e b p a g e s o m e t i m e b e t w e e n F e b r u a r y 2 3 r d , 1 9 9 3 a n d M a y 2 5 t h , 2 0 1 0 . I m a g e s a r e e a s y ! S i m p l y :
(一) L o o k a t y o u r f i x e d - w i d t h l a y o u t
(二) M e a s u r e e x a c t l y h o w m a n y p i x e l s o n e a c h a n d e v e r y u s e r ’ s s c r e e n y o u w i l l i n v a r i a b l y n e e d t o f i l l w i t h t h e i m a g e a t h a n d
(三) F i r e u p P h o t o s h o p
(四) “ S a v e ” y o u r i m a g e “ f o r w e b … ” a t e x a c t l y t h a t s i z e
(五) M a r k i t u p i n a n < i m g > t a g
(六) P o u r y o u r s e l f a b e e r ( o r m a y b e c r a c k o p e n a f r e s h c a n o f p e a s ) t o c e l e b r a t e a j o b w e l l d o n e .
W h i l e a f e w w i s e p r o p h e t s m a y h a v e o c c a s i o n a l l y s t u m b l e d o u t o f t h e w i l d e r n e s s , s p e a k i n g d e e p t r u t h s a b o u t t h e p r o b l e m s i n h e r e n t i n t h i s a p p r o a c h , i t h a s s e r v e d w o r k a d a y w e b d e s i g n e r s f o r t w e n t y y e a r s .
B u t t h e t i m e s , t h e y a r e a - c h a n g i n g .
F o u r y e a r s a g o , E t h a n M a r c o t t e p u b l i s h e d a n a r t i c l e ; t h i r t e e n d a y s l a t e r , S t e v e J o b s a n n o u n c e d a p h o n e ; s u d d e n l y “ f l u i d ” a n d / o r “ R e t i n a ” i m a g e s w e r e t h i n g s ; s u b s e q u e n t l y t h e r e h a s b e e n m u c h g n a s h i n g o f t e e t h .
There is a quick looping video here of the hole opening up, the block falling through, and then little man gnashing his teeth, but your browser can’t play it. You can try downloading it in the following flavors:
H.264
1080
720
480
320
213
Ogg Vorbis
1080
720
480
320
213
O u r f i r s t i n s t i n c t w h e n f a c e d w i t h i m p l e m e n t i n g f l u i d a k a r e t i n a a k a r e s p o n s i v e i m a g e s w a s t o r e a c h f o r t h e s a m e t o o l w e u s e f o r r e s p o n s i v e l a y o u t s : m e d i a q u e r i e s !
B r o w s e r s c a n ’ t k n o w a n y t h i n g a b o u t a w e b s i t e t h a t t h e y h a v e n ’ t l o a d e d y e t . B u t t h e y ’ r e c o n s t a n t l y a w a r e o f t h e e n v i r o n m e n t t h e y ’ r e r e n d e r i n g w i t h i n : t h e s i z e o f t h e v i e w p o r t , t h e r e s o l u t i o n o f t h e u s e r ’ s s c r e e n , t h a t s o r t o f t h i n g . T h e i d e a o f m e d i a q u e r i e s i s t h i s : l e t w e b d e v e l o p e r s d o s p e c i f i c t h i n g s f o r s p e c i f i c e n v i r o n m e n t s . If t h e v i e w p o r t i s w i d e r t h a n a t h o u s a n d p i x e l s , t h e n s h o w t h e s i d e b a r t o t h e l e f t . O t h e r w i s e , p u s h i t b e l o w o u r m a i n c o n t e n t . If t h e u s e r ’ s s c r e e n i s r e t i n a , t h e n u s e a b i g i m a g e . O t h e r w i s e , u s e a s m a l l e r o n e .
E a s y p e a s .
U n f o r t u n a t e l y w h e n i t c o m e s t o r e s p o n s i v e i m a g e s , i n m a n y c a s e s , i n p r a c t i c e , u s i n g m e d i a q u e r i e s t o p i c k a s o u r c e i s “ f u c k i n g t e r r i b l e ” .
I t ’ s w o r t h a m i n u t e t o e x p l o r e w h y . M e d i a - q u e r y - b a s e d r e s p o n s i v e i m a g e s o u r c e - p i c k i n g i s t e r r i b l e b e c a u s e w h i l e m o s t r e s p o n s i v e d e s i g n e r s h a v e s e t t l e d o n v a r y i n g a p a g e ’ s l a y o u t b a s e d o n o n e v a r i a b l e ( v i e w p o r t w i d t h ) , w h e n d e a l i n g w i t h r e s p o n s i v e i m a g e s , w e ’ r e r e a l l y c o n c e r n e d w i t h t h r e e v a r i a b l e s :
(一) T h e r e n d e r e d s i z e ( in C S S p i x e l s ) o f t h e i m a g e o n o u r l a y o u t
(二) T h e s c r e e n d e n s i t y
(三) T h e d i m e n s i o n s o f t h e v a r i o u s l y - s i z e d f i l e s a t o u r d i s p o s a l
… w h i c h b o i l d o w n t o m e d i a q u e r i e s i n n o n - o b v i o u s w a y s .
O n c e w e k n o w t h e s e t h r e e t h i n g s , a s o l u t i o n i s t r i v i a l . G i v e n a s e t o f s o u r c e s , p i c k t h e s m a l l e s t o n e w h o s e d i m e n s i o n s a r e s t i l l g r e a t e r t h a n r e n d e r e d s i z e × s c r e e n d e n s i t y .
B u t ! U n f o r t u n a t e l y ! r e n d e r e d s i z e i s a t r i c k y t h i n g t o p i n d o w n . W e b d e v e l o p e r s c a n ’ t k n o w i t . F l e x i b l e i m a g e s g o n n a f l e x ; w i t h i n a r e s p o n s i v e l a y o u t a n i m a g e ’ s r e n d e r e d s i z e c o u l d b e a l m o s t a n y t h i n g . A n d p e r h a p s s u r p r i s i n g l y , w h e n t h e b r o w s e r s t a r t s l o a d i n g i m a g e s , i t d o e s n ’ t k n o w t h e r e n d e r e d s i z e y e t e i t h e r — r e n d e r e d s i z e d e p e n d s o n t h e p a g e ’ s C S S , w h i c h b r o w s e r s g e n e r a l l y p a r s e l o n g a f t e r t h e y h a v e k i c k e d o f f i m a g e l o a d s .
H a p p i l y ( i t s e e m e d ) , a t t a c h i n g m e d i a q u e r i e s t o o u r s o u r c e f i l e s s i d e s t e p s t h a t p r o b l e m b y b r e a k i n g r e n d e r e d s i z e i n t w o …
(あ) T h e v i e w p o r t d i m e n s i o n s
(い) H o w t h e i m a g e w i l l b e s i z e d r e l a t i v e t o t h e v i e w p o r t
… a n d r e q u i r i n g a u t h o r s t o s p e c i f y o n l y t h e v i e w p o r t d i m e n s i o n s a n d s c r e e n d e n s i t y , a f t e r h a v i n g c o m p l e t e d a f e w m a n y e a s y d i f f i c u l t c a l c u l a t i o n s i n v o l v i n g e v e r y t h i n g e l s e .
W h a t s o r t o f c a l c u l a t i o n s ? L e t ‘ s t r y a n e x a m p l e .
( N o t e t h a t , w h i l e I ’ v e t r i e d k e e p t h i s s i m p l e , t h i s e x a m p l e ’ s e n t i r e r a i s o n d ' ê t r e i s t o s h o w y o u , d e a r r e a d e r , t h a t t h i s m e d i a - q u e r y - c a l c u l a t i n g p r o c e s s i s t e d i o u s a n d e r r o r - p r o n e . I f y o u f i n d y o u r s e l f q u i c k l y c o n v i n c e d f e e l 1 0 0 % f r e e t o s k i p t o P a r t 2 . )
L e t ’ s s a y y o u h a v e t h r e e v e r s i o n s o f a n i m a g e :
● l a r g e . j p g ( 1 0 2 4 x 7 6 8 )
● m e d i u m . j p g ( 6 4 0 x 4 8 0 )
● s m a l l . j p g ( 3 2 0 x 2 4 0 )
A n d y o u w a n t t o p i c k o n e a n d l o a d i t w i t h i n i n a f l e x i b l e g r i d — a g r i d w h i c h s t a r t s o u t s i n g l e - c o l u m n b u t s w i t c h e s o v e r t o t h r e e - c o l u m n o n l a r g e r v i e w p o r t s , l i k e t h i s .
Y o u w a n t t o s u p p o r t 1 x a n d 2 x d e v i c e - p i x e l - r a t i o s .
H o w t o c o n s t r u c t y o u r m e d i a q u e r i e s ? L e t ’ s s t a r t a t t h e t o p .
l a r g e . j p g s h o u l d o n l y l o a d w h e n a b s o l u t e l y n e c e s s a r y — w h e n b o t h s m a l l . j p g a n d m e d i u m . j p g a r e t o o s m a l l . M o r e p r e c i s e l y , w e o n l y w a n t t o l o a d l a r g e . j p g w h e n :
rendered width × screen density
> next-smallest-file’s width
I n o u r e x a m p l e l a y o u t , r e n d e r e d w i d t h i s a s i m p l e p e r c e n t a g e o f v i e w p o r t w i d t h . T h u s :
rendered width =
image’s width relative to viewport ×
viewport width
T h e n e x t - s m a l l e s t f i l e i s m e d i u m . j p g , s o :
next-smallest-file’s width = 640px
P u t i t a l l t o g e t h e r a n d w e ’ r e l e f t w i t h t h e f o l l o w i n g i n e q u a l i t y :
image’s width relative to viewport ×
viewport width ×
screen density
> 640px
W h i c h w e c a n r e - a r r a n g e i n t e r m s o f v i e w p o r t w i d t h l i k e s o :
viewport width >
640px ÷
( image’s width relative to viewport ×
screen density )
T o c o n s t r u c t o u r m e d i a q u e r i e s , w e s o l v e f o r v i e w p o r t w i d t h , f o r e v e r y p o s s i b l e v a l u e o f b o t h i m a g e ’ s w i d t h r e l a t i v e t o v i e w p o r t a n d s c r e e n d e n s i t y .
i m a g e ’ s w i d t h r e l a t i v e t o v i e w p o r t c a n h a v e o n e o f t w o v a l u e s : 1 0 0 vw b e f o r e w e h i t o u r b r e a k p o i n t ( a t 3 6 e m ) , a n d 3 3 . 3 v w a f t e r i t .
A s f o r s c r e e n d e n s i t y … w e l l , i t c o u l d b e a l o t o f t h i n g s , b u t w e ' v e s t a t e d w e o n l y n e e d t o s u p p o r t d e v i c e - p i x e l - r a t i o s o f 1 x a n d 2 x .
T w o p o s s i b l e i m a g e ’ s w i d t h r e l a t i v e t o v i e w p o r t s × t w o p o s s i b l e s c r e e n d e n s i t y s = f o u r s c e n a r i o s w e ’ l l n e e d t o c o n s i d e r ; l e t ’ s l o o k a t t h e m o n e - a t - a - t i m e .
1 x , b e f o r e b r e a k p o i n t :
B e c a u s e o u r b r e a k p o i n t i s a t 3 6 e m , b y d e f i n i t i o n w e k n o w t h a t :
viewport width < 36em
P l u g g i n g i m a g e ’ s w i d t h r e l a t i v e t o v i e w p o r t = 1 0 0 v w a n d s c r e e n d e n s i t y = 1 x i n t o t h e i n e q u a l i t y w e d e v i s e d e a r l i e r g i v e s :
viewport width >
640px ÷ ( 100vw × 1x ) = 640px = 40em
W h e n w e c o m b i n e t h e t w o w e g e t s o m e t h i n g i m p o s s i b l e :
36em > viewport width > 40em
S o w e c a n t h r o w t h i s s c e n a r i o o u t — w e ’ l l n e v e r n e e d l a r g e . j p g a t 1 x w i t h i n t h e s i n g l e - c o l u m n l a y o u t .
2 x , b e f o r e b r e a k p o i n t :
A g a i n :
viewport width < 36em
B u t n o w w e ’ r e p l u g g i n g i n 2 x :
viewport width >
640px ÷ ( 100vw × 2x ) = 320px = 20em
C o m b i n e t h e t w o a n d w e g e t :
36em > viewport width > 20em
S o w e w a n t t o l o a d l a r g e . j p g o n 2 x s c r e e n s w h e n t h e v i e w p o r t i s i n t h a t r a n g e .
1 x , a f t e r b r e a k p o i n t
W e ’ r e w i d e r t h a n t h e b r e a k p o i n t n o w , s o :
viewport width > 36em
A n d w e ’ r e l o o k i n g a t t h e t h r e e - c o l u m n l a y o u t o n a 1 x s c r e e n :
viewport width >
640px ÷ ( 33.3vw × 1x ) = 1920px = 120em
T h e v i e w p o r t i s a l w a y s g o i n g t o b e w i d e r t h a n 3 6 e m w h e n i t ’ s w i d e r t h a n 1 2 0 e m s o w e c a n t h r o w t h a t 3 6 e m b i t a w a y . W e w a n t t o l o a d l a r g e . j p g o n 1 x s c r e e n s w h e n :
viewport width > 120em
O k a y , l a s t o n e !
2 x , a f t e r b r e a k p o i n t :
viewport width > 36em
… a n d …
viewport width >
640px ÷ ( 33.3vw × 2x ) = 960px = 60em
… s o o n 2 x s c r e e n s l o a d l a r g e . j p g w h e n :
viewport width > 60em
L e t ’ s p u t i t a l l t o g e t h e r i n t o a m e d i a q u e r y :
( (min-device-pixel-ratio: 1.5) and (min-width: 20.001em) and (max-width: 35.999em) ) or
( (max-device-pixel-ratio: 1.5) and (min-width: 120.001em) ) or
( (min-device-pixel-ratio: 1.5) and (min-width: 60.001em) )
W o r k i n g t h r o u g h t h e s e s a m e c a l c u l a t i o n s f o r m e d i u m . j p g i s l e f t a s a n e x e r c i s e t o t h e r e a d e r .
W h a t w e ’ r e l e f t w i d t h , u s i n g t h e o r i g i n a l < p i c t u r e > p r o p o s a l t o m a r k u p o u r i m a g e , i s t h i s :
<picture>
<source src="large.jpg"
media="( (min-device-pixel-ratio: 1.5) and (min-width: 20.001em) and (max-width: 35.999em) ) or
( (max-device-pixel-ratio: 1.5) and (min-width: 120.001em) ) or
( (min-device-pixel-ratio: 1.5) and (min-width: 60.001em) )" />
<source src="medium.jpg"
media="( (max-device-pixel-ratio: 1.5) and (min-width: 20.001em) and (max-width: 35.999em) ) or
( (max-device-pixel-ratio: 1.5) and (min-width: 60.001em) ) or
( (min-device-pixel-ratio: 1.5) and (min-width: 10.001em) )" />
<source src="small.jpg" />
<!-- fallback -->
<img src="small.jpg" alt="A rad wolf" />
</picture>
A n d a h e a d a c h e !
A n d a p i l e o f m a r k u p t h a t i t d o e s n ’ t s u p p o r t d e v i c e - p i x e l - r a t i o s o v e r t w o , o r u n d e r o n e , a n d i m p e r f e c t l y s u p p o r t s t h o s e i n b e t w e e n . I f w e w a n t t o e x t e n d o u r d e v i c e - p i x e l - r a t i o s u p p o r t , t h e n u m b e r o f s c e n a r i o s n e e d t o c o n s i d e r i n c r e a s e s p r e c i p i t o u s l y .
A n d t h e w o r s t p a r t a b o u t t h a t m a r k u p i s t h a t i f w e c h a n g e a n y o f t h e u n d e r l y i n g v a r i a b l e s — t h e s i z e o f o u r s o u r c e i m a g e s , t h e d e v i c e r e s o l u t i o n s w e w a n t t o s u p p o r t … o r a n y a s p e c t o f o f o u r l a y o u t t h a t a f f e c t s t h e s i z e o f o u r i m a g e — w e h a v e t o d o a l l o f t h a t m a t h a g a i n .
Q u i c k , l e t ’ s g e t t o P a r t 2 .
P a r t 2 : s r c s e t + s i z e s = A w e s o m e !
S o m a y b e m e d i a q u e r i e s a r e n ’ t t h e r i g h t t o o l f o r t h e j o b . W h a t n o w ?
L e t ’ s r e t u r n t o o u r l i s t o f f u n d a m e n t a l r e s p o n s i v e - i m a g e v a r i a b l e s , t h i s t i m e t h i n k i n g a b o u t w h e n t h e y v a r y a n d w h o k n o w s w h a t .
Variable
Known by author when she’s writing the code?
Known by browser when it’s loading the page?
viewport dimensions
no
yes
image size relative to the viewport
yes
no
screen density
no
yes
source files’ dimensions
yes
no
N o t e ! W h e n e v e r t h e r e ’ s a y e s i n o n e c o l u m n , t h e r e ’ s a n o i n t h e o t h e r : t h e a u t h o r a n d t h e b r o w s e r k n o w d i f f e r e n t , c o m p l i m e n t a r y t h i n g s . W e ’ r e t h e k e y m a s t e r , t h e y ’ r e t h e g a t e k e e p e r ; w i t h o u r p o w e r s c o m b i n e d , e t c , e t c .
H o w t o b r i d g e t h i s g a p ?
M e d i a q u e r i e s a r e l i k e a s e t o f c o n t i n g e n c y p l a n s . “ L o o k , ” w e s a y t o t h e b r o w s e r , “ I d o n ‘ t k n o w h o w b i g t h a t v i e w p o r t i s g o i n g t o b e , b u t i f i t ’ s t h i s b i g , u s e t h i s f i l e . I f i t ’ s b i g g e r , u s e t h a t o n e . A l s o u s e t h a t o n e i f t h e s c r e e n i s r e t i n a , b u t n o t i f I ’ v e s w i t c h e d o v e r t o t h e 3 - c o l u m n l a y o u t … ” W e ’ r e l a b e l i n g f i l e s w i t h a s m o r g a s b o r d o f p o s s i b i l i t i e s , c o u c h e d i n t e r m s o f t h e t h i n g s t h e b r o w s e r w i l l k n o w , b u t w h i c h b u t w e , w r i t i n g t h e c o d e , c a n ’ t .
A n d a s w e ’ v e s e e n , i n p r a c t i c e , t h a t t u r n s o u t t o a w h o l e l o t o f w o r k .
S o w h a t i f w e f l i p p e d i t ?
W h a t i f i n s t e a d o f s u p p l y i n g t h e b r o w s e r w i t h a m e s s o f c o n t i n g e n c y p l a n s , w e s i m p l y t o l d i t t h e t h i n g s t h a t i t d o e s n ’ t k n o w ? N a m e l y : h o w t h e i m a g e w i l l b e s i z e d r e l a t i v e t o t h e v i e w p o r t , a n d t h e d i m e n s i o n s o f t h e s o u r c e f i l e s . We k n o w b o t h o f t h o s e t h i n g s . I f w e c o u l d s h a r e t h a t k n o w l e d g e w i t h t h e b r o w s e r , w o u l d n ’ t i t h a v e e v e r y t h i n g i t n e e d e d t o p i c k a s o u r c e ?
I t w o u l d ! I n d e e d , t h i s i s w h a t t h e s i z e s a t t r i b u t e a n d w d e s c r i p t o r w i t h i n s r c s e t in t h e l a t e s t , g r e a t e s t d r a f t o f t h e < p i c t u r e > s p e c i f i c a t i o n a r e f o r . H e r e ’ s t h a t t a b l e a g a i n :
Variable
Known by author when she’s writing the code?
Known by browser when it’s loading the page?
viewport dimensions
no
yes
image size relative to the viewport
yes
no yes! via sizes!
screen density
no
yes
source files’ dimensions
yes
no yes! via srcset!
B e f o r e w e d i g i n , l e t ’ s g e t t h r e e t h i n g s s t r a i g h t .
F i r s t a n d f o r e m o s t , n o n e o f t h i s h a s b e e n i m p l e m e n t e d i n a n y b r o w s e r , y e t . P r o s p e c t s a r e l o o k i n g g o o d b u t t h e s p e c i s s t i l l f l u i d ( p u n t o t a l l y i n t e n d e d ) . S o h o l d o f f o n u s i n g a n y o f t h i s . I t w o n ’ t w o r k n o w , a n d i t ’ l l o n l y b r e a k l a t e r .
S e c o n d : o n c e u p o n a t i m e t h e r e w a s a r e s p o n s i v e i m a g e s p r o p o s a l c a l l e d s r c s e t . T h e b r a n d - n e w p r o p o s a l w e ’ r e c o v e r i n g r e l i e s o n a n a t t r i b u t e a l s o c a l l e d s r c s e t . T h e o l d s r c s e t a n d t h e n e w s r c s e t b o t h u s e w d e s c r i p t o r s i n c o m m a - s e p a r a t e d l i s t s o f r e s o u r c e U R L s , b u t t h e o l d a n d t h e n e w w m e a n c o m p l e t e l y d i f f e r e n t t h i n g s ! O l d w w a s a s h o r t h a n d f o r m o f m e d i a q u e r y : t h e w i d t h i t d e s c r i b e d w a s t h e v i e w p o r t w i d t h . N e w w d e s c r i b e s t h e w i d t h o f t h e f i l e . W e ’ l l g o o v e r n e w w i n m o r e d e t a i l s h o r t l y , b u t f o r n o w , l e t m e j u s t g e t o u t o n e o f t h o s e M e n I n B l a c k m e m o r y e r a s e r t h i n g - a - m a j i g s a n d e r a s e e v e r y t h i n g y o u e v e r k n e w a b o u t s r c s e t a n d w .
A l l g o n e ? G o o d .
T h i r d : I f y o u ’ v e b e e n f o l l o w i n g a l o n g a n d m a y b e g e t t i n g y o u r h o p e s u p a b o u t p r e v i o u s i t e r a t i o n s o f t h e < p i c t u r e > s p e c , k n o w t h a t t h e n e w < p i c t u r e > s p e c s t i l l a l l o w s y o u t o s w i t c h s o u r c e s w i t h m e d i a q u e r i e s a n d a t t a c h r e s o l u t i o n d e s c r i p t o r s t o s o u r c e U R L s . I f y o u ’ r e d o i n g a r t d i r e c t i o n , o r f i x e d - s i z e r e s o l u t i o n - s w i t c h i n g , y o u a b s o l u t e l y s h o u l d b e u s i n g t h e s e f e a t u r e s . B u t i f y o u s i m p l y w a n t y o u r i m a g e s t o s q u i s h a n d s t r e t c h t h e r e ’ s a n e w t o o l a t y o u r d i s p o s a l .
O k a y . I t h i n k I ’ v e c l e a n e d t h e s l a t e a n d s e t t h e s t a g e . L e t ’ s t a c k l e o u r e x a m p l e , t h i s t i m e u s i n g s r c s e t a n d s i z e s .
T o r e v i e w , w e ' v e g o t t h r e e v e r s i o n s o f o u r i m a g e …
● l a r g e . j p g ( 1 0 2 4 x 7 6 8 )
● m e d i u m . j p g ( 6 4 0 x 4 8 0 )
● s m a l l . j p g ( 3 2 0 x 2 4 0 )
… a n d a b r e a k p o i n t a t 3 6 e m w h i c h s w i t c h e s o u r g r i d f r o m o n e t o t h r e e c o l u m n s .
H e r e ’ s t h e m a r k u p :
<img src="small.jpg"
srcset="large.jpg 1024w,
medium.jpg 640w,
small.jpg 320w"
sizes="(min-width: 36em) 33.3vw,
100vw"
alt="A rad wolf" />
Y o u m a y n o t i c e t h a t w h i l e t h i s m a r k u p c o m e s f r o m t h e “ p i c t u r e ” s p e c , t h e r e i s n ’ t a < p i c t u r e > e l e m e n t i n s i g h t . T h e s r c s e t a n d s i z e s a t t r i b u t e s w i l l b e i m p l e m e n t e d o n < i m g > t o o , a n d f o r s i m p l e , n o t - a r t - d i r e c t e d , n o n - t y p e - s w i t c h e d c a s e s l i k e t h i s , y o u c a n a n d s h o u l d u s e a s i n g l e i n s t a n c e o f o u r o l d f r i e n d , < i m g > t o m a r k u p y o u r r e s p o n s i v e i m a g e .
S a m e o l d < i m g > , b r a n d n e w a t t r i b u t e s ; l e t ’ s l o o k a t t h e m o n e - b y - o n e .
src="small.jpg"
O o p s , t h i s i s n ’ t n e w a t a l l ! I t ’ s o u r f a l l b a c k s r c , s a m e a s i t e v e r w a s , w h i c h w i l l l o a d i n a n y b r o w s e r t h a t d o e s n ’ t u n d e r s t a n d s r c s e t & s i z e s . N e x t !
srcset="large.jpg 1024w,
medium.jpg 640w,
small.jpg 320w"
T h i s o n e ’ s p r e t t y s e l f - e x p l a n a t o r y t o o . s r c s e t t a k e s a c o m m a - s e p a r a t e d l i s t o f U R L s f o r t h e a v a i l a b l e v e r s i o n s o f t h e i m a g e ; e a c h i m a g e ’ s w i d t h i s s p e c i f i e d u s i n g t h e w d e s c r i p t o r . S o i f y o u “ S a v e f o r W e b … ” a t 1 0 2 4 × 7 6 8 , m a r k t h a t s o u r c e u p i n s r c s e t as 1 0 2 4 w . E a s y .
Y o u ’ l l n o t e t h a t w e ’ r e o n l y s p e c i f y i n g w i d t h s . W h y n o t h e i g h t s , t o o ? T h e i m a g e s i n o u r l a y o u t a r e w i d t h - c o n s t r a i n e d ; t h e i r w i d t h i s s e t e x p l i c i t l y b y t h e C S S b u t t h e i r h e i g h t i s n o t . T h e v a s t m a j o r i t y o f r e s p o n s i v e i m a g e s i n t h e w i l d a r e w i d t h - c o n s t r a i n e d , t o o , s o t h e s p e c k e e p s t h i n g s s i m p l e b y d e a l i n g o n l y i n w i d t h s .
L o o k i n g i n t o t h e f u t u r e , t h e r e a r e a c o u p l e o f ( i n m y o p i n i o n , e x c e l l e n t ) r e a s o n s w h y w e m i g h t l i k e t o u s e h d e s c r i p t o r s t o d e s c r i b e t h e f i l e s ’ h e i g h t s , t o o , b u t — n o t y e t .
A n d a g a i n l e t m e e m p h a s i z e t h a t w h i l e y o u c a n a t t a c h 1 x / 2 x r e s o l u t i o n d e s c r i p t o r s t o s o u r c e s i n s r c s e t i n s t e a d of w d e s c r i p t o r s , 1 x / 2 x & w d o n o t m i x . D o n ’ t u s e b o t h i n t h e s a m e s r c s e t . R e a l l y .
O k a y , s o t h a t ’ s s r c s e t & w .
T h e l a s t b i t t h a t t h e b r o w s e r n e e d s t o p i c k a s o u r c e i s a n i d e a o f h o w l a r g e t h e i m a g e w i l l r e n d e r w i t h i n o u r l a y o u t . F o r t h a t , w e h a v e s i z e s . F r o m o u r e x a m p l e :
sizes="(min-width: 36em) 33.3vw,
100vw"
T h e f o r m a t h e r e i s :
sizes="[media query] [length], [media query] [length] ... etc "
W e ’ r e p a i r i n g m e d i a q u e r i e s w i t h l e n g t h s . T h e b r o w s e r g o e s o v e r e a c h m e d i a q u e r y u n t i l i t f i n d s o n e t h a t m a t c h e s a n d u s e s t h a t q u e r y ’ s p a i r e d l e n g t h a s t h e l a s t p i e c e o f t h e s o u r c e - p i c k i n g p u z z l e : t h e i m a g e ’ s r e n d e r e d w i d t h o n o r r e l a t i v e t o t h e v i e w p o r t .
“ W h a t ’ s t h a t ? ” y o u s a y , “ M e d i a q u e r i e s ? I t h o u g h t y o u s a i d t h e y w e r e t e r r i b l e ?! ”
I s a i d t h a t t h e y w e r e a t e r r i b l e m e c h a n i s m f o r p i c k i n g a s o u r c e . T h a t ’ s n o t w h a t t h e s e q u e r i e s a r e d o i n g ; t h e y ’ r e s i m p l y l e t t i n g t h e b r o w s e r i n o n t h e b r e a k p o i n t s i t ’ s a b o u t t o e n c o u n t e r i n t h e p a g e ’ s C S S a t e e n s y ( a n d c r u c i a l ! ) b i t a h e a d o f t i m e . R e m e m b e r h o w t h e v a r i o u s q u e r i e s i n o u r f i r s t e x a m p l e h a d n o t h i n g t o d o w i t h t h e p a g e ’ s o n l y l a y o u t b r e a k p o i n t ( w h i c h w a s a t 3 6 e m ) ? I m e a n , 6 0 e m , 2 0 e m , 1 0 e m — t h e y w e r e a l l o v e r t h e p l a c e ! T h e b r e a k p o i n t s i n s i z e s s h o u l d m i r r o r y o u r p a g e ’ s b r e a k p o i n t s e x a c t l y . T h e l e n g t h a f t e r e a c h s p e c i f i e s t h e i m a g e ’ s w i d t h o n t h e l a y o u t w h e n t h a t m e d i a q u e r y e v a l u a t e s t o t r u e .
T h e n t h e b r o w s e r , h a v i n g a l l o f t h e n e c e s s a r y i n f o r m a t i o n , d o e s t h e s a m e s o r t o f c a l c u l a t i n g t h a t w e s l o w , l a z y , e r r o r - p r o n e h u m a n s h a d t o d o i n P a r t 1 . M e a n w h i l e w e g e t t o r e l a x a n d e a t p e a s a s G o d i n t e n d e d .
A n d ! R e m e m b e r h o w o u r m e d i a q u e r y e x a m p l e o n l y c o v e r e d 1 x & 2 x s c r e e n s ? T h i s m a r k u p w o r k s w i t h a n y d e v i c e - p i x e l - r a t i o . N o m o r e g u e s s i n g a b o u t w h i c h r e s o l u t i o n s i t m a y o r m a y n o t b e r e a s o n a b l e t o s u p p o r t . W h e n a 4 . 8 6 2 5 x s m a r t w a t c h c o m e s o u t i n 2 0 1 6 , s r c s e t & s i z e s h a v e y o u c o v e r e d .
F u r t h e r m o r e ! T h i s s o l u t i o n g i v e s b r o w s e r s s o m e w i g g l e r o o m . A m e d i a q u e r y a t t a c h e d t o a s o u r c e i s e i t h e r t r u e o r f a l s e ; i f t r u e , t h e b r o w s e r m u s t l o a d t h e a s s o c i a t e d s o u r c e . s i z e s a n d s r c s e t a r e n ’ t s o r i g i d ; t h e s p e c a l l o w s b r o w s e r s t o , s a y , o p t i o n a l l y l o a d s m a l l e r s o u r c e s w h e n b a n d w i d t h i s s l o w o r e x p e n s i v e .
“ W e l l a l l o f t h a t c e r t a i n l y s o u n d s w o n d e r f u l , " y o u s a y , n o d d i n g s l o w l y , b e g i n n i n g t o u n d e r s t a n d t h e b e n e f i t s o f a d e c l a r a t i v e r a t h e r t h a n c o n d i t i o n a l a p p r o a c h . " B u t w a i t … w h a t ’ s a l e n g t h ? ”
A l e n g t h c a n b e a l l s o r t s o f t h i n g s ! A l e n g t h c a n b e a b s o l u t e ( e . g . 9 9 p x , 1 6 e m ) o r r e l a t i v e ( 3 3 . 3 v w , a s i n o u r e x a m p l e ) . Y o u ’ l l n o t e t h a t , u n l i k e o u r e x a m p l e , t h e r e a r e l o t s o f l a y o u t s w h i c h c o m b i n e b o t h a b s o l u t e a n d r e l a t i v e u n i t s . T h a t ’ s w h e r e t h e s u r p r i s i n g l y w e l l - s u p p o r t e d c a l c ( ) f u n c t i o n c o m e s i n . L e t ’ s s a y w e a d d e d a 1 2 e m s i d e b a r t o o u r 3 - c o l u m n l a y o u t . W e ’ d a d j u s t o u r s i z e s a t t r i b u t e l i k e s o :
sizes="(min-width: 36em) calc(.333 * (100vw - 12em)),
100vw"
D o n e !
“ O k a y , o k a y , ” y o u s a y c o n t e m p l a t i v e l y , s t r o k i n g y o u r c h i n , w e a r y f r o m ( b u t e x c i t e d b y ! ) t h i s i n f l u x o f k n o w l e d g e . “ T h e r e i s , h o w e v e r , o n e l a s t t h i n g : w h a t ’ s t h a t d a n g l i n g 1 0 0 v w ? D i d y o u f o r g e t a m e d i a q u e r y ? ”
I n t h e l a n g u a g e o f t h e s p e c , a l e n g t h w i t h o u t a p a i r e d m e d i a q u e r y i s a “ d e f a u l t l e n g t h ” . I f t h e r e a r e n o m e d i a q u e r i e s t h a t m a t c h , t h a t ’ l l g e t u s e d . T h i s m e a n s t h a t f o r , s a y , a g i a n t , f u l l - w i d t h b a n n e r i m a g e , y o u r m a r k u p c a n b e a s s i m p l e a s :
<img src="small.jpg"
srcset="large.jpg 1024w, medium.jpg 640w, small.jpg 320w"
sizes="100vw"
alt="A rad wolf" />
E a s y . P e a s .
(一)
O r a t l e a s t s t r e t c h y i m a g e s .
(二)
A n d y o u ( y e s y o u ! ) c a n m a k e t h e m l o o k e v e n b e t t e r !
⁂
R e d D e s e r t
B u c k y D . “ K i n g ” F i v e l - H o o k - P r e s l e y I V
← I n d e x —