This question already has answers here:
Why doesn't this a:visited css style work?
(6 answers)
Why are certain CSS properties not applied to a:visited? [duplicate]
(1 answer)
Why did browsers limit :visited selector?
(1 answer)
Closed 12 months ago.
I want it such that an a element that is visited and of the class, myclass is lightgreen and not clickable. I am able to make it lightgreen, but it is still clickable.
My code:
a:visited.upvote {
pointer-events: none;
cursor: default;
color: lightgreen;
}
and when that code is applied to all a elements, regardless of class and visited status (a {...}), the link is disabled as it should be.
The pointer-events property can't be applied to the :visited CSS pseudo-class due to:
Privacy restrictions
Because of privacy reasons, browsers strictly limit which
styles you can apply using this pseudo-class, and how they can be
used:
Allowable CSS properties are color, background-color, border-color, border-bottom-color, border-left-color,
border-right-color, border-top-color, column-rule-color,
outline-color, text-decoration-color, and
text-emphasis-color.
More info here.
A workaround would be adding a click event listener to the tags and then add to it a class that would apply the pointer-events: none; like so:
const unclickable = document.getElementById("unclickable")
unclickable.addEventListener("click", makeitso)
function makeitso() {
unclickable.className = "notSoClickableLink"
}
div{
display: flex;
flex-direction: column;
gap: 1rem;
}
.notSoClickableLink{
pointer-events: none;
color: lightgrey;
}
<div>
The first Link
<a id="unclickable" href="#2">Make this a visited Link</a>
</div>
This solution would not track your link tag's state, to circumvent that you can try referring to this post: How can I detect visited and unvisited links on a page?
Related
This question already has answers here:
Is there a CSS selector by class prefix?
(4 answers)
CSS attribute selector for class name
(1 answer)
attribute selector for class starts with and ends with
(1 answer)
CSS attribute selector class starts with but not equals to
(2 answers)
Closed 3 years ago.
I set dynamically classes on elements with class names "displayIfYes_%%" where %% comes from a database and can have a lot of values.
I am trying to set a simpler CSS selector for the classes I don't want to display, but I can't find how to do it.
I have a working solution to display elements only when value is "yes" using this CSS:
.displayIfYes_yes {visibility: inherit !important;}
.displayIfYes_na,
.displayIfYes_no,
.displayIfYes_scaled,
.displayIfYes_raw
/* ... and so on for any additionnal value */
{display: none !important;}
I want a selector to select any element which has class which begins with "displayIfYes" but does not end with "yes".
you can use selector [attribute|="value"] in this case your attribute can be class.
So:
[class|="displayIfYes"]{
/* */
}
will select class attribute which starts with that. The only complication is class attribute can have more than 1 class so this solution might not always work.
In that case I recommend using different classes for different scenarios from the database. You can create a class for each scenario such as;
.na,
.no,
.scaled,
.raw {
/* other styles */
}
.displayIfYes {
display: none !important;
}
The traditional way to solve this problem is to use a “base” class, then override with more specific classes. In your case, this would be:
.display-if-yes {
display: none;
/* Other styles which apply to all types */
}
.display-if-yes-yes {
display: unset;
visibility: inherit;
}
<div class="display-if-yes display-if-yes-yes">Yes</div>
<div class="display-if-yes display-if-yes-no">No</div>
<div class="display-if-yes display-if-yes-other">Other</div>
If you are unable to change your class structure for some reason, this should work for your specific requirements:
.displayIfYes_yes {
/* visibility: inherit; */
color: red;
}
*[class^='displayIfYes_']:not(.displayIfYes_yes),
*[class*=' displayIfYes_']:not(.displayIfYes_yes) {
/* display: none; */
color: green;
}
<div class="displayIfYes_yes">displayIfYes_yes</div>
<div class="displayIfYes_no">displayIfYes_no</div>
<div class="displayIfYes_other">displayIfYes_other</div>
I’ve commented out your specific styles just for the sake of the demo.
Here's a solution without using the :not() selector, instead only relying on attribute and class selectors and the underlying specificity.
Also, you can't override display: none; with visibility: inherit. Use display: initial instead.
[class^="displayIfYes_"] {display: none;}
.displayIfYes_yes {display: initial;}
<div class="displayIfYes_yes">div.displayIfYes_yes</div>
<div class="displayIfYes_na">div.displayIfYes_na</div>
<div class="displayIfYes_no">div.displayIfYes_no</div>
<div class="displayIfYes_scaled">div.displayIfYes_scaled</div>
<div class="displayIfYes_raw">div.displayIfYes_raw</div>
This question already has an answer here:
Why did browsers limit :visited selector?
(1 answer)
Closed 3 years ago.
I'm able to select child of a like so:
a > img {
/*change something*/
}
But I want first select a:visited, than its child. Something like:
a:visited > img {
/*change something*/
}
But the latter seems not working.
Example of HTML. Want change appearance of the image (adding border border: 2px solid; for example), if it is visited.
<!DOCTYPE html>
<body id="body-html">
<a href="https://stackoverflow.com/questions/56418220/css-selectors-avisited-childs?noredirect=1#comment99432271_56418220" class="test">
<img src="https://i.stack.imgur.com/nzzXb.png">
</a>
</body>
How can I achieve this?
Most CSS rules on :visited links have been blocked for security reasons.
However, you can still apply border-color to them.
The only gotcha here is that the border must be also applied on non-visited links, since you can only change the border-color.
a img {
border: 2px solid white;
}
a:visited img {
border-color: green;
}
fiddle
Though direct styling for :visited links is limited, there are lots of clever ways to extend your options for styling visited links. In 2015 there was a bumper crop of blog posts sharing new ideas for styling :visited links:
https://css-tricks.com/almanac/selectors/v/visited/
This question already has answers here:
text-decoration not working for visited state link
(3 answers)
Closed 3 years ago.
Please could someone explain why this doesn't seem to work? It's pretty straight-forward. I want links on the page to default to blue without an underline, and one they're clicked, I want it to change to purple with a line-through. However, my code doesn't work.
a {
text-decoration: none;
color: blue;
}
a:visited {
text-decoration: line-through;
color: purple;
}
Google (click me) <-- Once clicked, it should have a line through it<br>
<!-- Had to add an onclick event as clicking the link doesn't seem to work without it -->
Non-existant Site
https://jsfiddle.net/thefuzzy0ne/hfqdsr4z/
I've tried varying combinations of text-decoration-line and text-decoration-style and still nothing seems to work. I normally don't have much trouble with CSS, but this is driving me nuts.
You're limited to the attributes of the visited pseudo-class selector you can change for privacy reasons, so you can only style the following:
color
background-color
border-color
border-bottom-color
border-left-color
border-right-color
border-top-color
column-rule-color
outline-color
This question already has answers here:
Apply CSS Style on all elements except with a SPECIFIC ID
(3 answers)
Closed 6 years ago.
How do I cancel the changes to one individual element in CSS?
Example:
a {
text-decoration: line-through;
color: green;
}
Now I would like at the end, a.test to ignore all rules and be displayed in the default way.
The normal colors and decorations of a link would show up on a page without any CSS influence.
All I found was to change every property that changes the element to initial. Is there a universal command that would exempt a.test from all changes?
You can exclude with the :not() CSS pseudo selector
a:not(.test) {
text-decoration: line-through;
color: green;
}
Demo: https://jsfiddle.net/azizn/d17vdf35/
This question already has answers here:
CSS negation pseudo-class :not() for parent/ancestor elements
(2 answers)
Closed 7 years ago.
i have a hover effect for the links on my website. i want these to apply to every link EXCEPT ones in a particular div.
Example HTML
<div id="menu">
<div class="menu_item">
<a href="index.html" title="Home" target="_self">
<img src="_images/_menu/Home.png"
onmouseover="this.src='_images/_menu/homeHover.png'"
onmouseout="this.src='_images/_menu/Home.png'"
onclick="this.src='_images/_menu/homePressed.png'" alt=""/></a>
</div>
</div>
The CSS i have been trying to us
a:hover:not(.menu_item) {
background-color: #D6910E;
color: #FFE1A7;
} *no change*
a:hover:not(#menu) { *no change*
a:hover:not(#menu.menu_item) { *turns off hover on all links*
a:hover:not(#menu .menu_item) { *turns off hover on all links*
want these to apply to every link EXCEPT ones in a particular div
The standard approach to such problems in CSS is to give the general rule first, then the specific rule to override it. Using :not is a slippery slope and should be reserved for special cases. So:
/* State the general rule first */
a:hover {
background-color: #D6910E;
color: #FFE1A7;
}
/* Give the exception */
.menu_item a:hover {
background-color: transparent;
color: inherit;
}
If you do want to use :not, you have to understand that the predicate applies to the current element:
a:hover:not(#menu)
does not mean a tags being hovered which are not children of #menu; it means a tags being hovered which are not themselves #menu (which will always match). To do what you are trying to do with :not, you would want to try something like
:not(#menu) a:hover
However, this will also not work, because it means "a tags being hovered which have any ancestor which is not #menu", which will also almost always match.
Why you don't make it easier ?
Like
a:hover {
background-color:red;
color:red;
}
#menu .menu_item:hover{
/* Default color */
}
In your case , you can repair it by change the position of "hover"
a:not(.menu_item):hover {
background-color: #D6910E;
color: #FFE1A7;
} /*no change*/
a:not(#menu):hover { /*no change*/ }
a:not(#menu.menu_item) :hover { /*turns off hover on all links*/
a:not(#menu .menu_item):hover { /*turns off hover on all links*/
Hope it 'll help you