HTML Images - Attributes and Formats


Images in HTML - Tutorial 2023

Learning HTML is fun, and adding images to your HTML pages makes them look so attractive and catchy! It is not wrong to say that a webpage on a website is not complete until it has some images added to them.


The <img> tag in HTML

    The <img> tag, shortform for "image". It is one of the fundamental tag in HTML. <img> tag was introduced in the initial HTML specification, HTML 2.0, which was released in 1995.


The src attribute of <img> tag

    The src attribute is the required attribute that has to be used along with the <img> tag. It is used to add the URL or file path of the image that you want to be displayed.

    Note: Without the src attribute, the <img> tag will not display the image and will not function as intended.


Our HTML First Image Example

    my-dog-page.html
    <html>
    
    <head>
        <title>My Dog's Webpage</title>
    </head>
    
    <body>
        
    <h1>My Dog!</h1>
    
    <img src="https://code2care.org/howto/change-css-background-opacity-examples/images/dog-profile-image.jpg"/>
    
    </body>
    </html>

    Let's see how our first images look on the web browser.

    first-html-image-example-my-dogs-page

Setting width and height to HTML Image

    Though our image looks good, don't you think it needs to be resized? We can do that using the width and the height attributes.

    <img width=300 height=300 src="https://code2care.org/howto/change-css-background-opacity-examples/images/dog-profile-image.jpg"/>

    The image looks perfect now!


Add alternative text on hover on HTML image

    To add an alternate text when you hover on the image or to be displayed if the SRC path or URL is broken we can make use of the alt tag

    <img alt="This is my dog Oscar!" width=300 height=300 src="/broken/images/dog-profile-image.jpg"/>
    This is my dog Oscar!

List of all attributes supported by HTML img tag

    AttributeUsage
    altTo provides alternative text for the image
    srcTo specify the URL or file path of the image
    srcsetTo specify multiple sources for the image
    sizesTo specify image sizes for responsive design
    crossoriginTo specify how the image handles cross-origin requests
    usemapTo specify an image map for clickable regions
    ismapTo specify an image as a server-side image map
    widthTo specify the width of the image
    heightTo specify the height of the image
    loadingTo specify the image loading behavior
    decodingTo specify the image decoding behavior
    alignTo specify the alignment of the image within text
    hspaceTo specify horizontal space around the image
    vspaceTo specify vertical space around the image
    borderTo specify the border size around the image
    longdescProvides a URL to a long description of the image
    usemapTo specify an image map for clickable regions
    ismapTo specify an image as a server-side image map
    referrerpolicyTo specify the policy for sending referrer information
    loadingTo specify the image loading behavior
    decodingTo specify the image decoding behavior
    importanceTo specify the image rendering priority
    intrinsicsizeTo specify the intrinsic size of the image
    titleTo specify a title or tooltip for the image
    aria-*Various attributes for accessibility purposes
    data-*Custom data attributes for storing extra information
    classTo specify one or more class names for the image
    styleTo specify inline CSS styles for the image
    idTo specify a unique identifier for the image
    nameTo specify a name for the image (useful for forms)
    roleTo specify the role of the image in the document
    tabindexTo specify the tabbing order of the image
    accesskeyTo specify a shortcut key for the image
    draggableTo specify whether the image is draggable
    hiddenTo specify whether the image is visible or hidden
    contenteditableTo specify whether the image's content is editable
    spellcheckTo specify whether the browser should check spelling
    autocorrectTo specify whether the browser should auto-correct
    autocapitalizeTo specify how the browser should capitalize the image
    dirTo specify the text direction for the image
    langTo specify the language of the image
    translateTo specify whether the image should be translated
    roleTo specify the role of the image in the document
    aria-*Various attributes for accessibility purposes

List of most common HTML Image file formats

    Image FormatFile Extensions
    JPEG.jpeg, .jpg
    PNG.png
    GIF.gif
    BMP.bmp
    TIFF.tif, .tiff
    WebP.webp
    SVG.svg
    ICO.ico
    APNG.apng
    HDR.hdr
    EXR.exr
    HEIF.heif, .heic
    PDF.pdf
    PSD.psd
    AI.ai
    EPS.eps
    RAW (Camera).raw
    PICT.pict
    XBM.xbm

Facing issues? Have Questions? Post them here! I am happy to answer!

Author Info:

Rakesh (He/Him) has over 14+ years of experience in Web and Application development. He is the author of insightful How-To articles for Code2care.

Follow him on: X

You can also reach out to him via e-mail: rakesh@code2care.org

Copyright © Code2care 2024 | Privacy Policy | About Us | Contact Us | Sitemap