Is it possible to have text links styled, but without any actual hyperlinking?

Today has been rough, and I'm struggling to find a solution to my problem.

Here's the issue: I have some nice CSS for links, but I only want it to apply to text links, not images or divs. Just plain text.

My current code looks like this:

a {
    display: inline-block;
    position: relative;
    color: #ffffff;
}

a:after {
    content: '';
    position: absolute;
    width: 100%;
    transform: scaleX(0);
    height: 2px;
    bottom: 0;
    left: 0;
    background-color: #ffffff;
    transform-origin: bottom right;
    transition: transform 0.25s ease-out;
}

a:hover:after {
    transform: scaleX(1);
    transform-origin: bottom left;
}

Currently, every link has this animation effect, but I only want it on text links. Images linked to something also get this underline effect, causing issues. So, my question is:

Is there a way to specify 'only text links, not everything'? I need a global solution without having to create new classes for each specific element like images or divs. Something simple like:

a:text {
color: #ffffff;
}

This is important because many people will be working on content without access to the code. They won't be able to add classes to elements. If they insert an image, they can't specify that it shouldn't have the underline effect as a link. This restriction needs to be set up universally and forgotten by everyone except two individuals who can manage the CSS.

I've come across some inadequate solutions like 'create a class with a custom hover effect that does nothing', but this isn't ideal as it would require creating custom classes for potential links in various elements.

I hope someone can provide a better solution, thank you.

Answer №1

Arriving slightly tardy to the gathering, but with the unveiling of the :has selector, it is now feasible to design links devoid of images (and/or any other elements you wish to exclude) using a method similar to this:

a:not(:has(img,div)) {
  /* styling for links not containing <img> or <div> */
  /* e.g. a bold red outline for demonstration purposes */
  outline: 6px solid red;
}
<a href="#"><img src="https://picsum.photos/140" /></a><br/>
<a href="#">This link has only text</a><br/>
<a href="#"><div>This link has a div in it and will not be targeted</div></a>

Note: At present, this won't function on Firefox. The :has selector is compatible with all other major browsers, so hopefully Firefox will catch up soon.

Edit: The :has selector is presently supported by all major browsers.

Answer №2

Regrettably, HTML does not support conditional statements.

To ensure that only a href tags with a paragraph will be affected by the css.

a p{}

Instead of applying a universal style to link tags, consider creating separate styles for image tags and other elements.

a img {}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Show all entries belonging to a specific ID on individual rows for each ID using PHP

I have a table generated from inner joins that displays different articles associated with outfit IDs. id_outfit | name | article ---------------------------------------- 56 | one | shoe.png 56 | one | dress.png 56 | one ...

Issues with Bootstrap Table Column Widths not Adjusting Properly

I am facing an issue with my Bootstrap 4 table where the fixed column widths specified in the header row are not being respected when the table is rendered. The Time column, for example, should only be 5% of the width but it is taking up more space than ex ...

Strange Vertical Offset Issue with Custom Fonts in CSS

I have implemented a custom font in my CSS using the following method: @font-face { font-family: 'Gabriola'; src: url('Gabriola.eot'); src: url('Gabriola.eot?#iefix') format('embedded-opentype'), ...

CSS: text positioning issue observed in Mozilla browser

I am facing an issue with the text position inside the box/button element on my website. The text appears correctly in Chrome, but in Firefox, it is positioned above the box, making it difficult to read. Can someone assist me with fixing the CSS or identi ...

Rearrange the following <div> to appear before the previous one on medium and small devices using Bootstrap

I'm working with this div from the Bootstrap framework: <div class="card-header py-md-0 py-lg-0 py-xl-0 pl-0 pr-0"> <div class="d-flex justify-content-between align-items-center"> <div class="h5 mb-0">Text text</div&g ...

How to effectively manage an overflowing navigation bar

I am currently facing a challenge with the main navigation bar on my website, specifically with how it handles an overflow of links on smaller screen resolutions. I have attempted to adjust the height of the navigation bar at smaller media queries, but I&a ...

CSS: Stack elements vertically based on their height (column-wise)

Looking for some help with designing a menu where the list items float in columns instead of rows. For example, This is how my current menu looks like: I want to change the layout to look like this: My code for the list items is as follows: .mnu-third ...

Deciphering the principles of Bootstrap

I'm looking to understand more about what Bootstrap is. I know it's commonly used for styling web pages, but could you provide me with a clear explanation? Can big companies like Twitter, Facebook, or YouTube utilize Bootstrap for their platforms ...

Issue with the Animated Skill Bar not functioning properly when scrolling

I've been trying to implement an animated skill bar in my Portfolio using JQuery, but I'm facing some challenges. Despite following various tutorials, the code doesn't seem to work as expected. I've tried calculating section scroll posi ...

Creating a dynamic website with user-friendly editing capabilities

Having a good understanding of html5 and css3, I am currently exploring ways to create a website that enables visitors to edit content in real time for all other users to see. While aware of the contenteditable attribute, I have found that it does not reta ...

Breaking Points in Tailwind are crucial for creating responsive designs

I am attempting to create a simple div where I want px-5 for mobile screens and px-20 for larger screens. However, only px-5 is working and px-20 is not applying on large screens. Can someone help me understand why? Code Output Image <div class=" ...

Applying the .active class to a button which reveals the corresponding div

I've been working on a menu that functions to open and hide divs within a single page. My objective is to have the (.active) class applied to the currently selected menu item only when its connected div is open. When the div is closed or another menu ...

Tips for efficiently moving through divs in a personalized single page application?

Exploring the world of JS frameworks and single page app functionality is a bit of a mystery to me. Currently, I have a pseudo single page app set up without any specific framework in place. The setup involves 3 tabs that toggle visibility for 3 different ...

A comparison between centering an SVG element: Chrome versus Safari

Attempting to align an SVG element in the center of its parent container is proving challenging due to inconsistencies between Chrome and Safari. In Chrome, the text is perfectly centered inside the square, but not so in Safari: <svg width="200px" heig ...

Stylish CSS animation enhancing an image

Can a flash effect like the one in this image be created using only CSS? I attempted to use -webkit-filter, but it did not produce satisfactory results. https://i.sstatic.net/Eg49c.jpg ...

`Why are my overflow-x and flex-wrap not functioning as expected in React JS?`

Having trouble aligning all the movie posters in a single line? It appears that your overflow-x and flex-wrap properties aren't functioning as expected in your App.css. Here's a snippet of your App.css: body { background: #141414; color: ...

Exploring the world of child theme customization in WordPress

I am looking to make some modifications to my website. Working with a child theme, I have plans to adjust the CSS and make some structural changes. Modifying the CSS seems pretty straightforward as I just need to reference the classes or IDs and input new ...

`Place the image in the middle of the page`

I'm attempting to align the image directly before the text within the same div. I've applied CSS with text-align:center to the div, but it only seems to affect the text. Any suggestions on how to position the image right before the text? http:// ...

Left-align the text above the div (in relation to it)

My current issue involves aligning h2 text to the left relative to a centered div in the mobile view. How can I achieve this alignment with a media query in the provided CSS? https://i.sstatic.net/sIJb4.jpg CodePen Here is the relevant HTML: <sectio ...

Trouble implementing media queries (code available)

Having trouble with my media queries, they're not working. Can someone please help me understand what's happening? Current screen size: 241 (chrome, ie, ff) CSS @media screen and (max-width: 500px) { body { width: 100%; ...