HTML Source Sizes Attribute - Dofactory
Có thể bạn quan tâm
The sizes attribute on an <source> element specifies different image widths.
These widths are tied to browser conditions which helps create responsive images.
Example
#
A sizes attribute on a <source> element. Resizing the browser will adjust the image file that will display.
<picture>
<source srcset="/img/html/vangogh-sm.jpg 120w,
/img/html/vangogh.jpg 193w,
/img/html/vangogh-lg.jpg 278w"
sizes="(max-width: 710px) 120px,
(max-width: 991px) 193px,
278px">
<img src="/img/html/vangogh-lg.jpg" alt="Vincent Van Gogh">
</picture>
Try it live The three images of different size.
Code Explanation
A small image is used with a screen between 0 - 710px. The layout width is 120px.
A medium image is used with a screen between 711px - 991px. The layout width is 193px.
A large image is used with a screen 992px and larger. The layout width is 278px.
See below for a detailed description.
Using sizes
#
The srcset and sizes attributes work together to create responsive images.
The srcset attribute points to image URLs.
And sizes specifies image sizes and browser conditions (media queries).
Each size in sizes has the following format: (media-condition) width
- media-condition = Media query (e.g max-width: 540px), followed by a space.
- width = The width of the available space in pixels (px), em, or viewport (vw).
- Multiple sizes are comma-separated. Here is an example:
Syntax
#
<source sizes="(media-condition) width">Note: Multiple values are comma-separated.
Values
#
| Value | Description |
|---|---|
| (media-condition) width | A media-query followed by the image layout width. The layout width can be defined with px, em, or vw. |
Browser support
#
Here is when sizes support started for each browser:
Chrome | 34.0 | Apr 2014 |
Firefox | 38.0 | May 2015 |
IE/Edge | 16.0 | Oct 2017 |
Opera | 25.0 | Oct 2014 |
Safari | 9.0 | Sept 2015 |
You may also like
#
- Our HTML source Tag Reference
- Our HTML picture Tag Reference
- Our HTML sizes Attribute Reference
- Our CSS Images Tutorial
Từ khóa » Html Source Image Size
-
HTML Img Width Attribute - W3Schools
-
HTML Images - W3Schools
-
How To Resize An Image In HTML?
-
Width And Height Of An Image - HTML Tags - TAG Index
-
Responsive Images - Learn Web Development | MDN
-
: The Image Embed Element - HTML - MDN Web Docs - Mozilla
-
Setting Height And Width On Images Is Important Again
-
HTML Tag: Change The Width Of A Picture In HTML »
-
How To Resize Images In HTML
-
How To Set The Width And Height Of An Image Using HTML
-
How To Change Image Size In Html - Javatpoint
-
How To Resize An Image With HTML - Computer Hope
-
4.8.4 Images - HTML Standard - WhatWG
-
How To Change The Size Of An Image In Html Code Example
Chrome
Firefox
IE/Edge
Opera
Safari