Styling Hyperlinks

Understanding the different states of hyperlinks and how to customise their appearance using CSS.

Learning Goals

At the end of this Tutorial, you will be able to:

  • Understand that hyperlinks can have different interactive states.
  • Use TailwindCSS to change the colour of hyperlinks.
  • Use the TailwindCSS hover: and focus: prefixes to style hyperlinks when users interact with them.
  • Change the colour, thickness and position of hyperlink underlines.

In this Tutorial you will style the hyperlinks in your van Gogh and Irish tourism web pages.

Adding a smooth colour transition

When a hyperlink changes colour, you can add a short transition so that the change appears smoother.

TailwindCSS provides the following utilities:

  • transition-colors — adds a transition when a colour changes.
  • duration-300 — sets the duration of the transition to 300 milliseconds.

For example:

a {
@apply text-blue-700
       hover:text-red-700
       focus:text-red-700
       transition-colors duration-300;
}

Updating your van Gogh and Tourism hyperlinks

Now you will update the hyperlinks in two of your sample exercise files.

Van Gogh web page

  1. In VS Code, open your van Gogh HTML file.
  2. Find the <style type="text/tailwindcss"> block in the <head> of the page.
  3. Add the following style rule:
a {
@apply text-blue-700
       hover:text-yellow-700
       focus:text-yellow-700
       transition-colors duration-300;
}

Save the file and view the page in your browser.

Move your mouse pointer over a hyperlink and note how its colour changes.

Irish tourism web page

For your Irish tourism page, you can use a different colour combination:

a {
@apply text-green-800
       hover:text-green-600
       focus:text-green-600
       transition-colors duration-300;
}

Save your changes and view the page in your browser.

Styling hyperlink underlines

By default, hyperlinks are usually displayed with an underline. This helps users recognise that the text can be clicked.

TailwindCSS provides several utilities for controlling hyperlink underlines.

TailwindCSS Meaning
underline Displays an underline below the hyperlink text.
no-underline Removes the underline.
decoration-2 Sets the thickness of the underline.
decoration-blue-700 Sets the colour of the underline.
underline-offset-4 Creates additional space between the text and its underline.

Adding styled underlines

You can combine the hyperlink colour and underline utilities in one style rule.

For example:

a {
@apply text-blue-700
       underline
       decoration-2
       decoration-blue-700
       underline-offset-4
       hover:text-red-700
       hover:decoration-red-700
       focus:text-red-700
       focus:decoration-red-700
       transition-colors duration-300;
}

This rule:

  • displays the hyperlink text in blue;
  • adds an underline;
  • makes the underline slightly thicker;
  • sets the underline colour to blue;
  • adds space between the text and its underline;
  • changes both the text and underline to red when the hyperlink is hovered over or receives focus.

Removing underlines on hover

You can also remove the underline when the user interacts with a hyperlink.

For example:

a {
@apply text-blue-700
       underline
       decoration-2
       underline-offset-4
       hover:text-red-700
       hover:no-underline
       focus:text-red-700
       focus:no-underline
       transition-colors duration-300;
}

The hover:no-underline utility removes the underline when the mouse pointer is positioned over the hyperlink.

The focus:no-underline utility does the same when the hyperlink receives focus.

Styling hyperlinks: summary

TailwindCSS Purpose
text-blue-700 Sets the normal text colour.
hover:text-red-700 Changes the text colour when the mouse pointer is over the hyperlink.
focus:text-red-700 Changes the text colour when the hyperlink receives focus.
underline Adds an underline.
no-underline Removes an underline.
decoration-2 Controls underline thickness.
decoration-blue-700 Controls underline colour.
underline-offset-4 Controls the space between the text and underline.
transition-colors Adds a smooth transition between colour changes.
duration-300 Sets the transition duration to 300 milliseconds.

Uploading your files to GitHub

You are now ready to upload your work to your account on GitHub.

  1. Open a new tab in your web browser and go to github.com. If you are not already signed in to your GitHub account, sign in now. github-signin
  2. On your GitHub home page, click the ‘repo’ that holds your web pages. Its name will look as follows, where username is your chosen username on GitHub.   username.github.io github-signin
  3. On the next GitHub screen displayed, near the right of the screen, you can see a button named Add file. Click on it. github-upload-portfolio
  4. From the dropdown list displayed, choose the option Upload files. Project Animation Google Fonts
  5. In File Explorer (Windows) or Finder (Apple Mac), drag-and-drop your 📁 exercises sub-folder to upload it io your repository on GitHub. Introduction to HTML
  6. Scroll down to the bottom of the GitHub screen, and accept or edit the short message (Add files via upload) in the Commit changes box.
  7. Finally, click the green Commit changes button to upload your files. Project Animation Google Fonts

Your updated web pages are now published on GitHub at web addresses similar to the following:

https://username.github.io/exercises/page-4.html
https://username.github.io/exercises/page-5.html

It may take a few minutes for your uploaded files to appear on GitHub.

More learning resources

Tutorial Quiz

  Take the test

Tutorial Podcast