Explain Codes LogoExplain Codes Logo

Remove ALL styling/formatting from hyperlinks

html
css-inheritance
best-practices
responsive-design
Nikita BarsukovbyNikita Barsukov·Sep 23, 2024
TLDR

Here's the quick magic trick to strip hyperlink styling:

a { all: unset; /* Abracadabra! All styles: gone. */ }

This CSS line exorcises every trace of style from the spooky <a> tags, giving them a fresh slate.

To keep text properties intact while eliminating hyperlink decorations:

a { color: inherit; /* Paints with the same color as dad and mom */ text-decoration: none; /* Skipped school the day they taught about underlines */ background-color: transparent; /* Hides magic invisibility cloak */ cursor: pointer; /* Not rude, still points when you hover */ }

Consistency is key. Maintain the same styling across different hyperlink states:

a, a:hover, a:visited { color: inherit; text-decoration: none; }

This prevents sudden mutations in your link's appearance.

Keep the looks, not issues across different browsers

Check the browser runway before flying with all: unset;. To dress up your links in supported styles:

a { color: inherit; text-decoration: none; background-color: transparent; cursor: auto; /* Defaults to a pointer due to being a link */ }

Ensure your links grace every browser's screen flawlessly.

While on your style-stripping spree, remember to keep some essential attributes:

  1. Cursor: Maintains pointer to indicate clickable links.
  2. Color: Inherits parent text color for seamless readability.
  3. Underline: Consider keeping it for users who rely on underlines to spot links.

Maintaining code sanity with best practices

Avoid !important like a vampire avoids` garlic—it could lead to style conflicts and maintenance nightmare. Leverage classes or IDs for styling specific sections for a healthier codebase.

Gradually rebuilding styles

Post style reset, incorporating strategic modifications to fit your design:

  • Pseudo-classes for interaction cues.
  • Focus states increase accessibility.
  • Backgrounds or borders to emphasise elements.
  • Style icons within links for differentiation.

Embracing the power of CSS inheritance

Leverage inheritance in CSS:

  • Set defaults on the body tag which links can inherit.
  • Override defaults when necessary using class selectors.
  • Use class or ID selectors for more specificity.