How to apply different colours for different <a> tags in a set
Categories:
Styling Multiple Links: Applying Different Colors to Tags

Learn various CSS techniques to apply distinct colors to different tags within a set, enhancing visual hierarchy and user experience.
Styling anchor (<a>
) tags with different colors is a common requirement in web development. Whether you're highlighting specific actions, categorizing links, or simply improving visual appeal, CSS offers several powerful methods to achieve this. This article will guide you through various techniques, from basic class-based styling to more advanced attribute selectors and pseudo-classes, ensuring your links are both functional and aesthetically pleasing.
Basic Styling with Classes and IDs
The most straightforward way to apply different colors to <a>
tags is by using CSS classes or unique IDs. This method provides granular control over each link's appearance. Classes are ideal for applying the same style to multiple elements, while IDs are best for unique, one-off styling.
<a href="#" class="primary-link">Primary Action</a>
<a href="#" class="secondary-link">Secondary Info</a>
<a href="#" id="special-link">Special Offer</a>
HTML structure using classes and an ID for different links.
.primary-link {
color: #007bff; /* Blue */
}
.secondary-link {
color: #6c757d; /* Gray */
}
#special-link {
color: #dc3545; /* Red */
font-weight: bold;
}
CSS rules applying different colors based on classes and ID.
primary-action
, danger-link
) rather than purely descriptive color names (e.g., blue-link
). This makes your CSS more maintainable and adaptable if color schemes change.Leveraging Attribute Selectors and Pseudo-classes
For more dynamic or context-dependent styling, CSS attribute selectors and pseudo-classes offer powerful alternatives. Attribute selectors allow you to style links based on their href
attribute or other custom attributes. Pseudo-classes like :nth-child()
or :first-of-type()
can target links based on their position within a parent element.
<nav>
<a href="/home">Home</a>
<a href="/about">About Us</a>
<a href="/contact">Contact</a>
<a href="https://external.com">External Site</a>
</nav>
<div class="icon-links">
<a href="#" data-icon="facebook"><i class="fab fa-facebook"></i></a>
<a href="#" data-icon="twitter"><i class="fab fa-twitter"></i></a>
<a href="#" data-icon="linkedin"><i class="fab fa-linkedin"></i></a>
</div>
HTML with various link types and custom data attributes.
/* Styling based on href content */
a[href*="external.com"] {
color: purple;
}
/* Styling based on custom data attribute */
a[data-icon="facebook"] {
color: #3b5998; /* Facebook blue */
}
a[data-icon="twitter"] {
color: #1da1f2; /* Twitter blue */
}
a[data-icon="linkedin"] {
color: #0077b5; /* LinkedIn blue */
}
/* Styling based on position */
nav a:nth-child(1) {
color: green;
}
nav a:nth-child(2) {
color: orange;
}
CSS using attribute selectors and :nth-child()
for dynamic coloring.
flowchart TD A[Identify Target Links] --> B{Unique Identifier?} B -- Yes --> C[Apply ID Selector] B -- No --> D{Group of Links?} D -- Yes --> E[Apply Class Selector] D -- No --> F{Contextual Styling?} F -- Yes --> G[Use Attribute/Pseudo-Class Selectors] C --> H[Link Styled] E --> H G --> H
Decision flow for choosing the right CSS selector for link styling.
Integrating Font Awesome Icons with Colored Links
When using Font Awesome icons within <a>
tags, you can style both the icon and the link text. Font Awesome icons are typically rendered as <i>
or <span>
elements, and their color can be controlled by the parent <a>
tag's color property, or directly by targeting the icon element itself. This allows for visually distinct interactive elements.
<p>
<a href="#" class="social-link facebook">
<i class="fab fa-facebook-square"></i> Share on Facebook
</a>
</p>
<p>
<a href="#" class="social-link twitter">
<i class="fab fa-twitter-square"></i> Tweet This
</a>
</p>
<p>
<a href="#" class="action-link download">
<i class="fas fa-download"></i> Download File
</a>
</p>
HTML with Font Awesome icons inside anchor tags.
.social-link.facebook {
color: #3b5998;
}
.social-link.twitter {
color: #1da1f2;
}
.action-link.download {
color: #28a745;
font-weight: bold;
}
/* Ensure icons inherit color or set explicitly */
.social-link i, .action-link i {
margin-right: 5px;
}
/* Example of overriding icon color if needed */
.action-link.download i {
color: #1e7e34; /* Slightly darker green for icon */
}
CSS to color links and their Font Awesome icons.