HTML links are used to create clickable elements that allow users to navigate to different web pages or sections within the same page. Links are created using the <a> (anchor) element in HTML.

Links in HTML

Here’s the definition and an example of an HTML link:

<a href="https://www.example.com">Visit Example Website</a>

In this demonstratio, an anchor <a> element forms a connection. The destination URL is specified by the href property and in this case is “https://www.example.com”. The clickable link text is the phrase “Visit Example Website” that appears between the opening and closing </a> tags.

When users click on this link, they will be redirected to the specified URL, in this case, “https://www.example.com“. The appearance of the link may vary depending on the CSS styles applied to it.

It’s worth noting that links can also be used for internal navigation within the same webpage by using relative URLs. For example:

<a href="#section1">Go to Section 1</a>

In this case, the href attribute is set to “#section1”, which refers to an HTML element with the id attribute set to “section1” elsewhere on the same page. When users click on this link, the page will scroll to the section with the corresponding id.

HTML links can be styled using CSS to change their appearance, such as text color, underline, hover effects, and more. They are fundamental for creating navigation menus, linking related content, and connecting webpages together.

Different Colors for HTML Links

To specify different colors for HTML links, you can use CSS to style the <a> (anchor) element. CSS provides properties to customize the color of links based on their states, such as normal, hover, active, and visited.

Here’s an example of how you can define different colors for HTML links using CSS:

<style>
  /* Normal link color */
  a {
    color: blue;
  }

  /* Link color on hover */
  a:hover {
    color: red;
  }

  /* Link color when active (while being clicked) */
  a:active {
    color: green;
  }

  /* Link color for visited links */
  a:visited {
    color: purple;
  }
</style>

<a href="https://www.example.com">Visit Example Website</a>

In this example, the CSS code inside the <style> element sets different colors for various link states:

  • The a selector sets the normal link color to blue.
  • The a:hover selector sets the link color to red when the mouse pointer is hovering over the link.
  • The a:active selector sets the link color to green while the link is being clicked.
  • The a:visited selector sets the link color to purple for visited links (links that the user has previously clicked on).

You can customize these colors to your preference by specifying different color values. Additionally, you can apply other CSS properties to the <a> element, such as text-decoration, font-size, and background-color, to further enhance the appearance of the links.

By using CSS to style HTML links, you have the flexibility to define different colors and visual effects based on various link states, providing a more engaging user experience.

Create bookmarks for HTML Links

Bookmarks, also known as anchor links or named anchors, allow users to navigate to specific sections within a webpage. They are created using the <a> (anchor) element and the id attribute.

An demonstration of how to make bookmarks in HTML is provided here:

<a href="#section1">Go to Section 1</a>
...
<h2 id="section1">Section 1</h2>
<p>This is the content of Section 1.</p>

In this example, we have an anchor <a> element with the href attribute set to “#section1”. The # symbol is used to indicate an internal link within the same page. The value “section1” corresponds to the id attribute of the HTML element we want to link to.

Following the anchor link, there is an <h2> heading element with the id attribute set to “section1”. This creates a bookmark or target location within the page.

When users click on the “Go to Section 1” link, the page will scroll to the section with the corresponding id, which in this case is the <h2> heading with the “section1” id. Users can easily jump to different sections within the page by clicking on these bookmarks.

You can create multiple bookmarks within the same webpage by assigning unique id values to different HTML elements and linking to them using anchor links.

Bookmarks are useful for long webpages with multiple sections, allowing users to navigate directly to specific content. They can be combined with CSS styles to provide visual cues or animations when a bookmarked section is reached.

Images in HTML

HTML Images are used to display visual content, such as photos, illustrations, icons, and graphics, on a web page. They are inserted using the <img> element in HTML.

Here’s the definition and an example of an HTML image:

<img src="image.jpg" alt="Image description">

In this demonstration, an image is created with the <img> element. The “image.jpg” source URL is specified via the src property in this example. For accessibility reasons or in the event that the picture cannot be loaded, the alt property offers alternate text that is shown.

When the webpage is loaded, the browser fetches the image file specified in the src attribute and displays it in the designated location on the page. The alt attribute text is shown if the image is not available or if the user is utilizing assistive technologies.

You can also provide additional attributes to customize the image, such as:

<img src="image.jpg" alt="Image description" width="200" height="150">

In this modified example, the width and height attributes specify the dimensions of the image in pixels. This can be useful to control the size of the image and ensure proper spacing within the layout.

It’s important to note that the src attribute should point to the correct file path or URL of the image file. Images can be stored on the same server as the webpage or hosted on external servers.

HTML images can be further enhanced and styled using CSS properties, such as width, height, borders, margins, and more. You can also use the <figure> and <figcaption> elements to add captions or descriptions to images.

By incorporating images into your HTML content, you can visually enhance your webpages and provide a richer user experience.

Image maps in HTML

Create clickable portions on a picture using HTML image maps to associate certain regions with various links or actions. The HTML <map> element and <area> element are used to build image maps and specify the clickable regions.

Here’s the definition and an example of an HTML image map:

<img src="image.jpg" alt="Image description" usemap="#myMap">
<map name="myMap">
  <area shape="rect" coords="0,0,200,200" href="page1.html" alt="Link 1">
  <area shape="circle" coords="300,200,100" href="page2.html" alt="Link 2">
  <area shape="polygon" coords="500,100,700,200,600,300" href="page3.html" alt="Link 3">
</map>

In this case, the src property of the picture is used to show it in the <img> element. The image’s alternate text is provided via the alt property. The #myMap fragment identifier is used by the usemap attribute to refer to the <map> element.

We define many <area> components inside the <map> element, each of which represents a clickable region on the picture. The area’s form is determined by the shape attribute, which has three possible values: “rect” (rectangle), “circle,” or “polygon.” The coordinates of the area’s shape are specified via the coords property.

In the example, we have three <area> elements:

  • The first <area> element is a rectangular area, defined by the coords attribute as “0,0,200,200”, and links to “page1.html”.
  • The second <area> element is a circular area, defined by the coords attribute as “300,200,100”, and links to “page2.html”.
  • The third <area> element is a polygonal area, defined by the coords attribute as “500,100,700,200,600,300”, and links to “page3.html”.

When users click within these defined areas on the image, they will be directed to the corresponding links specified in the <area> elements.

HTML image maps are useful for creating interactive images, such as maps, diagrams, or infographics, where specific regions need to be clickable. They allow you to define multiple links within a single image, providing a way to navigate or trigger actions based on user interactions.

Categorized in: