The Wayback Machine - https://web.archive.org/web/20240717024727/https://www.geeksforgeeks.org/css-links/
Open In App

CSS Links

Last Updated : 12 Apr, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

A link is a connection from one web page to another web page. CSS property can be used to style the links in various different ways.
States of Link: Before discussing CSS properties, it is important to know the states of a link. Links can exist in different states and they can be styled using pseudo-classes.

There are four states of links given below: 

  • a:link => This is a normal, unvisited link.
  • a:visited => This is a link visited by user at least once
  • a:hover => This is a link when mouse hovers over it
  • a:active => This is a link that is just clicked.

Syntax: 

a:link {
color:color_name;
}

color_name can be given in any format like color name (green), HEX value (#5570f0) or RGB value rgb(25, 255, 2). There is another state ‘a:focus’ which is used to focused when a user uses the tab key to navigate through the links. 

The default value of links: 

  • By default the links created are underlined.
  • When the mouse is hovered above a link, it changes to a hand icon.
  • Normal/unvisited links are blue.
  • Visited links are colored purple.
  • Active links are colored red.
  • When a link is focused, it has an outline around it.

Example: This example shows the basic use of links in CSS.

html
<!DOCTYPE html>
<html>

<head>
    <title>CSS links</title>
    <style>
        p {
            font-size: 25px;
            text-align: center;
        }
    </style>
</head>

<body>
    <p>
        <a href="https://www.geeksforgeeks.org/">
            GeeksforGeeks Simple Link
        </a>
    </p>
</body>

</html>

Output: 

links

GIF Output showing the appearance of links in different states using CSS

CSS Properties of Links: Some basic CSS properties of links are given below: 

  • color
  • font-family
  • text-decoration
  • background-color

color: This CSS property is used to change the color of the link text. 

Syntax:

a {
color: color_name;
}

Example: This example shows the use of the color property in links.

html
<!DOCTYPE html>
<html>

<head>
    <title>Link color property</title>
    <style>
        p {
            font-size: 20px;
            text-align: center;
        }

        /*unvisited link will appear green*/
        a:link {
            color: red;
        }

        /*visited link will appear blue*/
        a:visited {
            color: blue;
        }

        /*when mouse hovers over link it will appear orange*/
        a:hover {
            color: orange;
        }

        /*when the link is clicked, it will appear black*/
        a:active {
            color: black;
        }
    </style>
</head>

<body>
    <p>
        <a href="https://www.geeksforgeeks.org/">
            GeeksforGeeks
        </a>
        This link will change colours with different states.
    </p>
</body>

</html>

Output: 



statee

GIF Output



font-family: This property is used to change the font type of a link using font-family property. 

Syntax: 

a {
font-family: "family name";
}

Example: This example shows the use of the font-family property in links.

html
<!DOCTYPE html>
<html>

<head>
    <style>
        /*Initial link font family arial*/
        a {
            font-family: Arial;
        }

        p {
            font-size: 30px;
            text-align: center;
        }

        /*unvisited link font family*/
        a:link {
            color: Arial;
        }

        /*visited link font family*/
        a:visited {
            font-family: Arial;
        }

        /*when mouse hovers over it will change to times new roman*/
        a:hover {
            font-family: Times new roman;
        }

        /*when the link is clicked, it will changed to Comic sans ms*/
        a:active {
            font-family: Comic Sans MS;
        }
    </style>
</head>

<body>
    <p>
        <a href="https://www.geeksforgeeks.org/" id="link">
            GeeksforGeeks
        </a>
        a Computer Science Portal for Geeks.
    </p>
</body>

</html>

Output: 


es

GIF Output



Text-Decoration: This property is basically used to remove/add underlines from/to a link. 

Syntax: 

a {
text-decoration: none;
}

Example: This example shows the use of the text-decoration property in links.

html
<!DOCTYPE html>
<html>

<head>
    <title>Text decoration in link</title>
    <style>
        /*Set the font size for better visibility*/
        p {
            font-size: 2rem;
        }

        /*Removing underline using text-decoration*/
        a {
            text-decoration: none;
        }

        /*underline can be added using
        text-decoration:underline;
        */
    </style>
</head>

<body>
    <p>
        <a href="https://www.geeksforgeeks.org/" id="link">
            GeeksforGeeks
        </a>
        a Computer Science Portal for Geeks.
    </p>
</body>

</html>

Output: 


abc

GIF Output



background-color: This property is used to set the background color of the link. 

Syntax: 

a {
background-color: color_name;
}

Example: This example shows the use of the background-color property in links.

html
<!DOCTYPE html>
<html>

<head>
    <title>background color</title>
    <style>
        /*Setting font size for better visibility*/
        p {
            font-size: 2rem;
        }

        /*Designing unvisited link button*/
        a:link {
            background-color: powderblue;
            color: green;
            padding: 5px 5px;
            text-decoration: none;
            display: inline-block;
        }

        /*Designing link button when mouse cursor hovers over it*/
        a:hover {
            background-color: green;
            color: white;
            padding: 5px 5px;
            text-align: center;
            text-decoration: none;
            display: inline-block;
        }
    </style>
</head>

<body>
    <p>
        <a href="https://www.geeksforgeeks.org/" id="link">
            GeeksforGeeks
        </a>
        a Computer Science Portal for Geeks.
    </p>
</body>

</html>

Output: 


pp

GIF Output



CSS Link Button: CSS links can also be styled using buttons/boxes. The following example shows how CSS links can be designed as buttons. 

Example: This example shows the use of links as a button.

html
<!DOCTYPE html>
<html>

<head>
    <title>Link button</title>
    <style>
        /*Setting font size for better visibility*/
        p {
            font-size: 2rem;
        }

        a {
            background-color: green;
            color: white;
            padding: 5px 5px;
            border-radius: 5px;
            text-align: center;
            text-decoration: none;
            display: inline-block;
        }
    </style>
</head>

<body>
    <p>
        <a href="https://www.geeksforgeeks.org/" id="link">
            GeeksforGeeks
        </a>
        a Computer Science Portal for Geeks.
    </p>
</body>

</html>

Output: 


giff

GIF Output




Previous Article
Next Article

Similar Reads

Difference between normal links and active links
Websites are designed to point you to different resources. You can move from one website to another through links. Links help you to get information from different resources. Links are established in simple HTML web pages through &lt;a&gt; tag.Links are categorized into three types. Typically a Link is displayed in three different colors based on t
2 min read
How to Remove Firefox's Dotted Outline on Buttons and links using CSS ?
In Firefox browser, dotted outline appears around the buttons, links as well as inputs. The ability to tell which piece is now in focus makes this outline useful for people who browse the web using a keyboard. This is a feature of Firefox's accessibility tools, which seek to make it simpler for users to navigate online pages using keyboard shortcut
3 min read
How to create Right Aligned Menu Links using HTML and CSS ?
The right-aligned menu links are used on many websites. Like hotels website that contains lots of options in the menu section but in case of emergency to make contact with them need specific attention. In that case, you can put all the menu options on the left side of the navigation bar and display contact us option on the right side of the bar. Th
3 min read
How to create animated banner links using HTML and CSS?
Links are one of the most important parts of any component that is used in website making. Almost every component had some form of links in it. A common example is a menu/nav-bar. All we see is some button like home, about, etc. but under the hood they all are links. Sometimes there comes a situation where you don't want to wrap the link inside a b
2 min read
Primer CSS Marketing Links
Primer CSS is a free open-source CSS framework that is built upon systems that create the foundation of the basic style elements such as spacing, typography, and color. This systematic method makes sure our patterns are steady and interoperable with every other. Its approach to CSS is influenced by Object-Oriented CSS principles, functional CSS, an
2 min read
How to Display the file format of links using CSS ?
In this article, we will explain the approach to display the file formats on a webpage. Many times while browsing, we need to download a document file but in PDF format, however, a problem might occur that multiple links of downloading the file are there, but each link contains different file formats. One may contain a document file and another may
2 min read
How to style a href links in React using Tailwind CSS ?
In this article, we will learn how we can style a href link in Reactjs. Since we use Tailwind CSS, Tailwind CSS describes itself as the first applicable CSS framework. Rather than focusing on the performance of a style object, Tailwind focuses on how it should be displayed. This makes it easier for the developer to explore new styles and transform
2 min read
Foundation CSS Base Typography Links
Foundation CSS is an open-source &amp; responsive front-end framework built by ZURB foundation in September 2011, that makes it easy to design beautiful responsive websites, apps, and emails to look amazing &amp; can be accessible to any device. It is used by many companies such as Facebook, eBay, Mozilla, Adobe, and even Disney. The framework is b
3 min read
Primer CSS Links
Primer CSS is a free and open-source CSS framework. It is built upon the systems that create the foundation of the basic style elements such as spacing, typography, and colour. Created with GitHub’s design system, it is highly reusable and flexible. Primer CSS Links are used to decorate links in the webpage. Primer CSS has lots of customizations fo
3 min read
How to select all links within a Paragraph using CSS?
To style all links within a paragraph using CSS, you can use a simple selector. The following code selects all anchors (a) elements that are descendants of paragraph (p) elements. This rule allows you to apply specific styles, such as color and text decoration, exclusively to links within paragraphs, offering a targeted and efficient way to customi
1 min read
three90RightbarBannerImg