HTML

Introduction to Web and HTML

How the Web Works What is HTML Why Learn HTML

Image Attributes

Image Attributes

Images in HTML can be customized and enhanced using various attributes. These attributes allow you to control the image's appearance, behavior, and accessibility. Here are some of the most commonly used image attributes:

src Attribute

The src (source) attribute is the most fundamental image attribute. It specifies the URL or file path to the image you want to display on your web page. This attribute is mandatory, and without it, the image won't be displayed.

<img src="image.jpg" alt="A beautiful landscape">

alt Attribute

The alt (alternative text) attribute provides a textual description of the image. It is essential for accessibility and is displayed when the image cannot be loaded or when using screen readers. Always include a meaningful alt attribute for every image.

<img src="flower.jpg" alt="A vibrant red rose in full bloom">

width and height Attributes

The width and height attributes allow you to specify the dimensions of the image. These attributes are useful for controlling the image's size and layout on your web page. Ensure that you maintain the image's aspect ratio to prevent distortion.

<img src="photo.jpg" alt="A scenic landscape" width="400" height="300">

title Attribute

The title attribute provides additional information about the image when users hover their mouse pointer over it. It's often used for tooltips to give users more context about the image.

<img src="icon.png" alt="An icon" title="Click for more information">

border Attribute

The border attribute is used to add a border around the image. You can specify the border thickness in pixels. However, it's considered outdated, and it's recommended to use CSS for styling instead.

<img src="image.png" alt="A graphic" border="2">

align Attribute

The align attribute determines the alignment of the image concerning the surrounding text. It's commonly used for aligning images to the left or right of text content. However, this attribute is also considered outdated, and CSS is preferred for alignment.

<img src="logo.png" alt="Company Logo" align="left">

style Attribute

The style attribute allows you to apply inline CSS styles to the image. You can use this attribute to customize the image's appearance, such as changing its size, margins, or adding borders. Inline styles are useful for making specific, one-off changes.

<img src="illustration.jpg" alt="Artwork" style="width: 300px; border: 1px solid #0077b6;">

Accessibility Considerations

When using image attributes, remember to provide descriptive and meaningful values for attributes like alt. Good accessibility practices ensure that all users, including those with disabilities, can access and understand your content.

In summary, image attributes in HTML allow you to control the appearance, behavior, and accessibility of images on your web page. Using these attributes effectively enhances the user experience and makes your content more informative and engaging.