If HTML we use img tag to insert image in our web page.
Image tags img do not have closing tags. The two main attributes you give to the img tag are src, the image source and alt, which is alternative text describing the image.
Parameters | Details |
---|---|
src | Specifies the URL of the image |
srcset | Images to use in different situations (e.g., high-resolution displays, small monitors, etc) |
sizes | Image sizes between breakpoints |
crossorigin | How the element handles crossorigin requests |
usemap | Name of image map to use |
ismap | Whether the image is a server-side image map |
alt | Alternative text that should be displayed if for some reason the image could not be displayed |
width | Specifies the width of the image (optional) |
height | Specifies the height of the image (optional) |
To add an image to a page, use the image tag
Image tags img do not have closing tags. The two main attributes you give to the img tag are src, the image source and alt, which is alternative text describing the image.
<img src="images/hello.png" alt="Hello World">
Copy
You can also get images from a web URL:
<img src="https://i.stack.imgur.com/ALgZi.jpg?s=48&g=1" alt="StackOverflow user Caleb Kleveter">
Copy
Note: Images are not technically inserted into an HTML page, images are linked to HTML pages. The img tag creates a holding space for the referenced image. It is also possible to embed images directly inside the page using base64:
<img src="data:image/png;base64,iVBOR..." alt="Hello World">
<img sizes="(min-width: 1200px) 580px,(min-width: 640px) 48vw,98vw"
srcset="img/hello-300.jpg 300w,
img/hello-600.jpg 600w,
img/hello-900.jpg 900w,
img/hello-1200.jpg 1200w"
src="img/hello-900.jpg" alt="hello">
Copy
sizes are like media queries, describing how much space the image takes of the viewport.
srcset is just telling the browser what images we have available, and what are their sizes
src is always mandatory image source. In case of using with srcset, src will serve fallback image in case browser is not supporting srcset.
<img src="img/hello-300.jpg" alt="hello"
srcset="img/hello-300.jpg 1x,
img/hello-600.jpg 2x,
img/hello-1200.jpg 3x">
Copy
srcset provides list of available images, with device-pixel ratio x descriptor.
src is always mandatory image source. In case of using with srcset, src will serve fallback image in case browser is not supporting srcset.
<picture>
<source media="(min-width: 600px)" srcset="large_image.jpg">
<source media="(min-width: 450px)" srcset="small_image.jpg">
<img src="default_image.jpg" style="width:auto;">
</picture>
Copy
To display different images under different screen width, you must include all images using the source tag in a picture tag as shown in the above example.