Friday, May 23, 2025
News PouroverAI
Visit PourOver.AI
No Result
View All Result
  • Home
  • AI Tech
  • Business
  • Blockchain
  • Data Science & ML
  • Cloud & Programming
  • Automation
  • Front-Tech
  • Marketing
  • Home
  • AI Tech
  • Business
  • Blockchain
  • Data Science & ML
  • Cloud & Programming
  • Automation
  • Front-Tech
  • Marketing
News PouroverAI
No Result
View All Result

Styling External Links with Attribute Selectors

October 11, 2023
in Front-Tech
Reading Time: 3 mins read
0 0
A A
0
Share on FacebookShare on Twitter



You might notice on some websites you visit that external links display a little icon next to them. This is super helpful for users, as it lets them know that the link is going to take them somewhere off-site. I’ve just recently implemented custom styling for external links within posts on this site. The good news is we don’t need to append a special class to external links or alter the markup in any way. We can simply use an attribute selector.

Using attribute selectors CSS allows us to style HTML elements based on their attributes by wrapping them in square brackets. For example, we can style any element with the hidden attribute to have a display property of none:

“`html
[hidden] {
display: none;
}
“`

We can also style elements when the attribute equals a particular value. For form elements, we could style particular input types without having to use a class:

“`html
input[type=”checkbox”] {
accent-color: deeppink;
}
“`

For our external links we want to apply the styling when the href attribute contains a link to an external site. We don’t know exactly what that value will be (and it wouldn’t be practical to add each individual URL to our stylesheet!), but we know that internal links (links to other posts on the site) will begin with a slash, whereas external links will begin with https://. So we can style only the links that begin with http by inserting a ^ character into our attribute selector:

“`html
a[href^=’http’] {
/* Styles for external links */
}
“`

Alternatively, we can use other operators to determine different styling conditions:

“`html
/* Matches the URL exactly */
a[href=”https://css-irl.info”]{}

/* Has ‘css’ anywhere in the URL */
a[href*=’css’] {}

/* Ends with .info */
a[href$=’.info’] {}

/* Class contains the word ‘link’ */
a[class~=’link’] {}
“`

Additionally, by adding s or i before the end bracket we can control whether they are compared case-sensitively or insensitively:

“`html
/* Case sensitive */
a[href*=’css-irl’ s] {}

/* Case insensitive */
a[href*=’css-irl’ i] {}
“`

Styling the pseudo element

For our external links, we’ll append an icon by styling a pseudo element. Here we’re using the content property with a base64 encoded SVG, as the icon is very simple. But you could use an image URL, another character, or an emoji. We can add a small margin to position it slightly away from the text.

“`html
a[href^=’http’]::after {
content: url(“data:image/svg+xml,%3Csvg xmlns=’http://www.w3.org/2000/svg’ viewbox=’0 0 12.2 12.2′ width=’14’ height=’14’%3E%3Cpath d=’M5.7 0v1.5h4L4.5 6.7l1 1.1 5.3-5.2v3.9h1.4V0z’/%3E%3Cpath fill=’none’ d=’M3.4 6.7l3-2.9H1.5v7h7V5.9l-3 2.9z’/%3E%3Cpath d=’M8.5 5.9v4.9h-7v-7h4.9l1.5-1.6H0v10h10V4.4z’/%3E%3C/svg%3E”);
margin-left: 0.25em;
}
“`

One problem with using content with an SVG is we don’t have full control over the size of the icon. It uses the intrinsic dimensions of the SVG. If we want to apply the icon to any external link regardless of font size (e.g. a heading), we might be better off using the background-image property. We can set a width and height (in ems, which are relative to font size), and use background-size to ensure our SVG covers the entire area. We need to set the content property to an empty string, otherwise the pseudo element won’t render. We also need to set the display property to inline-block. (Note: I’m using a custom property for the image URL, for brevity.)

“`html
a[href^=’http’]::after {
content: ”;
display: inline-block;
width: 1em;
height: 1em;
margin-left: 0.25em;
background-size: 100%;
background-image: url(–var(svgUrl));
}
“`

Preventing an ”orphan” icon

There’s one more thing we can improve about this approach. Currently it’s possible for the icon to wrap onto the next line of text, leaving an undesirable orphan icon all on its own. If we add position: absolute to the pseudo element, and a bit of right padding to the anchor element, then the icon will never wrap by itself.

“`html
a[href^=’http’] {
padding-right: 1.25em;
}

a[href^=’http’]::after {
position: absolute;
content: ”;
display: inline-block;
width: 1em;
height: 1em;
margin-left: 0.25em;
background-size: 100%;
background-image: url(–var(svgUrl));
}
“`

Here’s the full code:

“`html
See the Pen Untitled by Michelle Barker (@michellebarker) on CodePen.
“`



Source link

Tags: AttributecssExternalfront endLinksSelectorsstylingweb designweb development
Previous Post

Dianne Feinstein successor Laphonza Butler shares her vision for California’s future, avoids question on 2024 Senate election

Next Post

Phones with self-healing displays to arrive by 2028, analysts predict

Related Posts

The essential principles of a good homepage
Front-Tech

The essential principles of a good homepage

June 7, 2024
How to measure and improve user retention
Front-Tech

How to measure and improve user retention

June 6, 2024
Push Animation on Grid Items
Front-Tech

Push Animation on Grid Items

June 5, 2024
How to build a Rails API with rate limiting
Front-Tech

How to build a Rails API with rate limiting

June 4, 2024
Introduction to the B.I.A.S. framework
Front-Tech

Introduction to the B.I.A.S. framework

June 3, 2024
Blue Ridge Ruby is exactly what we need
Front-Tech

Blue Ridge Ruby is exactly what we need

June 3, 2024
Next Post
Phones with self-healing displays to arrive by 2028, analysts predict

Phones with self-healing displays to arrive by 2028, analysts predict

English Newspaper Reading | Newspaper Reading in English Today | Business Standard Newspaper

English Newspaper Reading | Newspaper Reading in English Today | Business Standard Newspaper

State of Crypto in 2023: You NEED To SEE This Report!

State of Crypto in 2023: You NEED To SEE This Report!

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

  • Trending
  • Comments
  • Latest
Is C.AI Down? Here Is What To Do Now

Is C.AI Down? Here Is What To Do Now

January 10, 2024
23 Plagiarism Facts and Statistics to Analyze Latest Trends

23 Plagiarism Facts and Statistics to Analyze Latest Trends

June 4, 2024
Accenture creates a regulatory document authoring solution using AWS generative AI services

Accenture creates a regulatory document authoring solution using AWS generative AI services

February 6, 2024
A faster, better way to prevent an AI chatbot from giving toxic responses | MIT News

A faster, better way to prevent an AI chatbot from giving toxic responses | MIT News

April 10, 2024
Porfo: Revolutionizing the Crypto Wallet Landscape

Porfo: Revolutionizing the Crypto Wallet Landscape

October 9, 2023
Part 1: ABAP RESTful Application Programming Model (RAP) – Introduction

Part 1: ABAP RESTful Application Programming Model (RAP) – Introduction

November 20, 2023
Can You Guess What Percentage Of Their Wealth The Rich Keep In Cash?

Can You Guess What Percentage Of Their Wealth The Rich Keep In Cash?

June 10, 2024
AI Compared: Which Assistant Is the Best?

AI Compared: Which Assistant Is the Best?

June 10, 2024
How insurance companies can use synthetic data to fight bias

How insurance companies can use synthetic data to fight bias

June 10, 2024
5 SLA metrics you should be monitoring

5 SLA metrics you should be monitoring

June 10, 2024
From Low-Level to High-Level Tasks: Scaling Fine-Tuning with the ANDROIDCONTROL Dataset

From Low-Level to High-Level Tasks: Scaling Fine-Tuning with the ANDROIDCONTROL Dataset

June 10, 2024
UGRO Capital: Targeting to hit milestone of Rs 20,000 cr loan book in 8-10 quarters: Shachindra Nath

UGRO Capital: Targeting to hit milestone of Rs 20,000 cr loan book in 8-10 quarters: Shachindra Nath

June 10, 2024
Facebook Twitter LinkedIn Pinterest RSS
News PouroverAI

The latest news and updates about the AI Technology and Latest Tech Updates around the world... PouroverAI keeps you in the loop.

CATEGORIES

  • AI Technology
  • Automation
  • Blockchain
  • Business
  • Cloud & Programming
  • Data Science & ML
  • Digital Marketing
  • Front-Tech
  • Uncategorized

SITEMAP

  • Disclaimer
  • Privacy Policy
  • DMCA
  • Cookie Privacy Policy
  • Terms and Conditions
  • Contact us

Copyright © 2023 PouroverAI News.
PouroverAI News

No Result
View All Result
  • Home
  • AI Tech
  • Business
  • Blockchain
  • Data Science & ML
  • Cloud & Programming
  • Automation
  • Front-Tech
  • Marketing

Copyright © 2023 PouroverAI News.
PouroverAI News

Welcome Back!

Login to your account below

Forgotten Password? Sign Up

Create New Account!

Fill the forms bellow to register

All fields are required. Log In

Retrieve your password

Please enter your username or email address to reset your password.

Log In