In HTML, images are added using the <img>
tag. This is a self-closing tag, meaning it doesn’t require a separate closing tag. Images enhance the content of a webpage, providing visual context or improving aesthetics.
The basic syntax for embedding an image in an HTML document is:
<img src="imageURL" alt="alternative text" />
<img>
Tagsrc
(Source):
https://example.com/image.jpg
)images/photo.jpg
).Example:
<img src="https://example.com/photo.jpg" alt="Description of photo" />
alt
(Alternative Text):
Example:
<img src="images/photo.jpg" alt="A scenic view of mountains during sunset" />
width
and height
:
px
), percentages (%
), or leave it unset to maintain the original image dimensions.Example:
<img src="images/photo.jpg" alt="A mountain view" width="500" height="300" />
title
:
Example:
<img src="images/photo.jpg" alt="A mountain view" title="Mountain view during sunset" />
Common image formats supported by browsers include:
Absolute URL: The full web address of the image, including the protocol (http
or https
), domain, and file path.
Example:
<img src="https://example.com/images/photo.jpg" alt="Example photo" />
Relative URL: A path relative to the current HTML document. It assumes the image is stored within your website’s directory.
Example:
<img src="images/photo.jpg" alt="Example photo" />
Relative paths can be:
<img src="photo.jpg" alt="Example photo" />
<img src="images/photo.jpg" alt="Example photo" />
<img src="../photo.jpg" alt="Example photo" />
In modern web development, making images responsive ensures they adapt to different screen sizes and devices. You can use CSS to handle responsive design:
img {
max-width: 100%;
height: auto;
}
This ensures that the image scales according to the screen size while maintaining its aspect ratio.
Alternatively, HTML5 introduced the <picture>
element and the srcset
attribute for responsive image handling:
<picture>
<source media="(min-width: 650px)" srcset="large-image.jpg">
<source media="(min-width: 465px)" srcset="medium-image.jpg">
<img src="small-image.jpg" alt="Responsive example image">
</picture>
In the above example, different images are loaded based on the screen size.
<img src="images/mountain.jpg" alt="Mountain range at sunset" width="600" height="400" title="Mountain View" />
In this example:
images/mountain.jpg
).alt
attribute provides a description in case the image doesn’t load.width
and height
attributes control the size of the image.title
attribute displays additional information when the user hovers over the image.alt
text for accessibility and SEO benefits.srcset
.JPG
for photos, PNG
for images with transparency).Embedding images in HTML is straightforward using the <img>
tag. By properly using attributes like src
, alt
, and title
, you can ensure your images are accessible, responsive, and optimized for different screen sizes. Always keep in mind the type of image and the context in which it will be displayed for the best user experience.
Links are one of the core components of the web, allowing users to navigate between different pages, documents, or sections of a website. In HTML, links are created using the <a>
tag (which stands for “anchor”).
The basic syntax for adding a link in HTML is:
<a href="URL">Link Text</a>
href
: This is the “Hypertext Reference” attribute, which holds the destination URL.External Links: These links point to resources outside of your website, such as another webpage, social media, or any other external resource.
Example:
<a href="https://www.example.com">Visit Example Website</a>
Internal Links: These links point to other pages within the same website. You use relative URLs to navigate between internal pages.
Example:
<a href="about.html">About Us</a>
If your webpage structure has folders, you’ll need to adjust the path based on file locations:
<a href="services.html">Our Services</a>
<a href="blog/post1.html">Read our latest post</a>
<a href="../index.html">Home</a>
Anchor Links (Internal Page Sections):
Anchor links navigate to a specific section within the same page or another page. You must give an element (usually a heading or section) an id
attribute, and link to it using the #
symbol followed by the id
value.
Example:
<a href="#section1">Go to Section 1</a>
<!-- Later in the page -->
<h2 id="section1">Section 1</h2>
Email Links:
You can use the mailto:
protocol in the href
attribute to create a link that opens the user’s default email client and starts composing an email.
Example:
<a href="mailto:contact@example.com">Email Us</a>
Telephone Links:
Similar to email links, you can use the tel:
protocol to link phone numbers. This is useful for mobile users who can click the link to initiate a call.
Example:
<a href="tel:+1234567890">Call Us</a>
<a>
Taghref
(Hypertext Reference):
Example:
<a href="https://www.example.com">Visit Example</a>
target
:
_self
: (default) Opens the link in the same window/tab._blank
: Opens the link in a new tab or window._parent
: Opens the link in the parent frame._top
: Opens the link in the full body of the window, breaking out of frames.Example:
<a href="https://www.example.com" target="_blank">Open in New Tab</a>
title
:
Example:
<a href="https://www.example.com" title="Click to visit Example">Visit Example</a>
download
:
download
attribute.Example:
<a href="files/document.pdf" download>Download PDF</a>
You can link to various file types such as PDFs, images, videos, and more. When a user clicks the link, their browser will handle the file based on its type:
<a href="files/ebook.pdf">Download Ebook</a>
<a href="images/photo.jpg">View Photo</a>
<a href="videos/demo.mp4">Watch Video</a>
To open a link in a new browser tab, set the target
attribute to _blank
:
<a href="https://www.example.com" target="_blank">Visit Example in New Tab</a>
This is useful when linking to external websites, as it keeps the user on your site while giving them the option to view other content.
You can also make an image a clickable link by wrapping the <img>
tag within an <a>
tag:
<a href="https://www.example.com">
<img src="images/photo.jpg" alt="Example Image" />
</a>
In this case, clicking on the image will navigate the user to the destination specified in the href
.
<a href="https://www.example.com" target="_blank" title="Visit our homepage" download>
Visit Example Website
</a>
In this example:
https://www.example.com
).target="_blank"
ensures it opens in a new tab.title
attribute provides additional information when hovered over.download
attribute will initiate a file download instead of loading the page, assuming the link points to a downloadable file.<a href="example.com">Click here</a>
<a href="example.com">Learn more about our services</a>
Open External Links in New Tabs: For links pointing outside your website, consider using target="_blank"
so users remain on your site.
Use Anchor Links for Better Navigation: Especially in longer pages, anchor links improve usability by letting users jump to specific sections.
Check for Broken Links: Regularly check for and fix any broken links (links leading to dead or incorrect pages) on your site.
title
attributes for users who rely on screen readers and ensure link color or underline contrasts well with the background.Links are an essential part of web navigation, allowing users to move between pages, sections, and external resources. By properly using attributes like href
, target
, and title
, you can create meaningful and functional links that enhance the user experience.
Next -> List and Tables