Styling the Links in CSS - css

In CSS properties, there are four pseudo selectors available to style your links. The selectors are a:link, a:visited, a:active, and a:hover.
Are you suppose to do this...
a:link{color:red;}
a:visited{color:blue;}
a:hover{color:green;}
a:active{color:yellow;}
This worked, but is there a shorter and/or simpler way.

If you want different rules for each pseudo target, then the way you have it written is the shortest you can make it. Had the rulesets been the same, you could simply comma delimit the selectors:
a:link, a:visited, a:hover, a:active { color: red; }
In the above example the ruleset for each pseudo is the same, so it in turn can be shortened to:
a { color: red; }
When using these specific pseudo classes, be aware that order matters when declaring rulesets:
To style links appropriately, put the :link rule before all other link-related rules, as defined by the LVHA-order: :link — :visited — :hover — :active.

In plain css that's the simplest way, if you want to style the 'a' tag for different sections of your app you can use classes or ids, an example using classes is:
HTML
<div class='classOfSomeSection'>
This link is red
</div>
<div class='classOfAnotherSection'>
This link is green
</div>
CSS
.classOfSomeSection a:link{
color:red;
}
.classOfAnotherSection a:link{
color:green;
}
There are other methods involving Javascript to change pseudo classes of specified elements but in my opinion, css is simpler.

If you use SCSS, you can do it by creating a mixin. It seems longer up front, but the upside is that you write the mixin once and then just include it with one line wherever you need it.
// set up vars for each state you want
$link: #00a400; // green
$visited: #870808; // deep red
$hover: #8ee1ff; // blue
$active: #df42f4; //pink
// set up mixin
#mixin link-color($color) {
color: $link;
&:visited {
color: $visited;
}
&:hover {
color: $hover;
}
&:active {
color: $active;
}
}
// use mixin
a {
#include link-color($link);
}
Fiddle
So the answer to your question is yes, in that you can write it shorter, and no, in that you have to write something a little bit long somewhere. Ideally, squirrel your mixins away in a file all their own so you they don't clutter your view.

Related

Adding class selector after pseudo selector in css

Can I add a class selector after a pseudo class in my CSS rule, e.g:
a:hover.my-class {
background: red;
}
So if I hover over my anchor tag, <a class="my-class">link</a>, will the background be red in all browsers? is this valid CSS?
Why I need this
I have this problem because it is generated from a mixin in SASS:
#mixin focus($classA, $classB) {
&:focus {
&#{$classA} {
background: blue;
}
&#{$classB} {
background: yellow;
}
}
}
a {
#include focus('.class-a', '.class-b')
}
There is no such thing as a "pseudo-selector".
There are two features in selectors that start with "pseudo-": pseudo-class and pseudo-element. They are completely different features with different syntax rules.
You can place a class selector after a pseudo-class such as :hover, because they are both simple selectors and order of simple selectors in a compound selector does not matter (type and universal selectors are the only exceptions to this rule — they always have to come first, such as the a in your example).
You cannot place a class selector after a pseudo-element such as ::before, because a pseudo-element is not a simple selector. Your question may not be about pseudo-elements, but this distinction has to be made because of the common use of the term "pseudo-selector", which incorrectly groups both features into a single umbrella term (and frankly makes the question more complicated than it really needs to be).
Yep, you can add a class to a pseudo class.
This css is valid and it works:
a:hover.hoverme {
background:blue;
color:white;
}
This works too:
a.hoverme:hover {
background:blue;
color:white;
}
Or you can add a pseudo class after a class.
.hoverme:hover {
background:blue;
color:white;
}
Hover me!
You can check if your CSS is valid at W3C's CSS Validator page.

What is the simplest way to clear all pseudo classes on an element?

I am writing a stylesheet to extend a base stylesheet whose CSS has many pseudo classes applied to certain elements. I would like my stylesheet to override some of these styles with a single style that is applied to an element no matter what state it is in, whether hovered on, focussed etc.
For example, the base stylesheet might have the styles
.classname {
color:#f00;
}
.classname:hover {
color:#0f0;
}
.classname:active {
color:#00f;
}
but adding the following after these styles does not override the pseudo states...
.classname {
color:#fff;
}
The following works, but it feels a lot of code for something that seems simple.
.classname,
.classname:active,
.classname:hover,
.classname:focus,
.classname:visited,
.classname:valid{
color:#fff;
}
Likewise, I know an !important would work, but that's normally a warning sign of a poorly structured stylesheet.
Is there anything along the lines of a .classname:* that would cover every possible state, or some way to simply remove all pseudo classes?
If you are able to put the classes inside some wrapper id you can prevent the pseudo-classes to take effect due to specificity:
body {
background: black;
}
.classname {
color:#f00;
}
.classname:hover {
color:#0f0;
}
.classname:active {
color:#00f;
}
#a .classname {
color:#fff;
}
<div class="classname">all pseudo works</div>
<div id="a">
<div class="classname">none of the pseudo works</div>
</div>
I think, it could be solved with :any pseudo-class.
Google
<style>
a:link { color: blue; }
a:hover { color: red; }
a:-webkit-any(a) { color: green; }
</style>
https://jsfiddle.net/ycfokuju
Browser support is not perfect: https://developer.mozilla.org/en/docs/Web/CSS/:any
Edit:
Actually, as I discovered, this answer isn't very accurate. (Despite it was upvoted 4 times, lol).
First of all, you don't need :any fot this task. You need :any-link.
The second point is that :any itself is a former name of :matches. So, in our terminology we should use terms :any-link and :matches and don't use term :any.
Example of using :any-link: https://developer.mozilla.org/en-US/docs/Web/CSS/:any-link
Examples of using :mathes: https://css-tricks.com/almanac/selectors/m/matches/
I haven't edited the code itself, so fix it yourself according to this new information.

Css Style not working?

It may just be me, but sometimes I think im starting to get the hang of this css stuff' and then it goes back to 'I dont have a clue.'
so, I have a default style
a:focus,
a:hover,
a:active {
outline: 0 none;
text-decoration: none;
color: #fff;
}
but on a couple of <a href..> I need to overwrite the style,
so I have added the following to my css
a.myBlue a.myBlue:hover {
color: #3078ef ;
}
.myBlue a:hover {
color: #3078ef ;
}
(Yes, I've done this twice)
and applied
But in Chrome, looking at developer tools its still applying the standard style, it does not even pull "myBlue" down?
Where am I going wrong?
The css selector:
a.myBlue a.myBlue:hover
Means "Any a of class myBlue that is being hovered over and is a child element of an a of class myBlue.
If you wish to apply the same style to multiple selectors, you need to separate each selector with a comma:
a.myBlue, a.myBlue:hover
There is a typo mistake in your code...Use below code. it works...
a.myBlue, a.myBlue:hover {
color: #3078ef ;
}
Your CSS selector is wrong, how you've got it present means it only applies the rule when you hover over an element with the class of .myBlue which is the child of another element with the class of .myBlue. So instead your selector needs to be
a.myBlue, a.myBlue:hover { /* notice the comma */
color: #3078ef ;
}

Do I need to specify each link color state?

I'm using the following pseudo classes:
a.recentposttitle:link,a.recentposttitle:visited {color:#000;}
a.recentposttitle:hover {color:#56A49F;}
a.recentposttitle:active {color:#000;}
Do I need to be that explicit or is there a more compressed way to get the same result?
No, there is no shorthand. But your selectors can be:
a {}
to select all links, or:
.recentposttitle {}
to get all .recentposttitle elements (we know that they are links already).
And another thing, :link is not needed really, you can write:
a {}
a:visited {}
a:hover {}
a:active {}
When you write a {}, you will set the declaration for all possible situations, so:
a {}
Is identical to:
a:link, a:visited, a:hover, a:active {}
And remember, the order of pseudo classes are importent:
:link
:visited
:hover
:active
Or simply remember LoVe HAte.
There is no shorthand selector in CSS for the pseudo-classes of anchors/links. So what you have is pretty well as terse as you can get.
W3 Link Training
a:link{ Declarations }
a:visited{ Declarations }
...
a:hover{ Declarations }
a:active{ Declarations }
I need to find the reference I've previously read to confirm this, but AFAIK, the :link pseudo-selector is only necessary if you are using old-style page anchors (<a name="..."></a>), so you should be able to safely eliminate that. Since your :active and :visited rules are the same, you could probably cut down what you've shown to this:
a.recentposttitle:active, a.recentposttitle:visited {color:#000;}
a.recentposttitle:hover {color:#56A49F;}
But you're not really saving that many bytes, so hard to say if it's worth it.
You can compress it by using CSS Frameworks like LESS or SASS.
Like from your example,
The CSS:
a.recentposttitle:link,a.recentposttitle:visited {color:#000;}
a.recentposttitle:hover {color:#56A49F;}
a.recentposttitle:active {color:#000;}
For example if you use SASS you can compress it to..
a.recentposttitle {
color: #000;
&:link{ color: #000; }
&:hover { color: #56A49F; }
&:visited { color: #000; }
&:active{ color: #000; }
}
You can also use Emmet, previously known as Zen coding for maximum code compressions.
Hope this helps.

CSS Applying wildcard to pseudo classes

Hopefully this isn't a stupid question but I can't seem to work out how to do this. Can you apply a wildcard to an anchor hover/focus so that the style is applied to all classes?
Something like
a:hover * { color: #ff0000; }
Say I have
a { color: #DD0000; }
a.link { color: #ffffff; }
a.link2 { color: #000000; }
a.user { ...
a.anything { ...
The easiest way to explain what I'm looking for is to have a global :hover style, but multiple :link styles.
Thanks
There are a number of ways you can do this. As mentioned by others, you can apply the same style to multiple classes like so:
div a.class1:hover, div a.class2:hover, div a.class3:hover { ... }
You can also create a custom class just for the style you want to apply:
div a.customClass:hover { ... }
You could use * like you mentioned in the question, but apply hover to it:
div *:hover { ... }
There's also this option, where you just apply the style for all a's, although you probably know about this option already:
a:hover { ... }
Edit: If your style is being "overwritten" by something else, a quick and easy way to check would be to use your browser's developer tools to inspect the element. You can even apply pseudo-classes (ie. apply :hover pseudo-class even when you're not hovering over the element) with the developer tools included with Chrome and Firefox (you may need to download Firebug to do this with Firefox).
Another option would be to use !important to increase the selector's specificity. For example:
a:hover { background: red !important; }
You can read more about how the specificity is calculated here.
If you want to apply a global css rule for a specific tag, write (for anchors):
a:link{/*your styles go here*/}
a:hover{/*your styles go here*/}
a:active{/*your styles go here*/}
a:visited{/*your styles go here*/}
If you would like a special link styled in a different way (maybe making it a button), just apply a class to it and style the class:
a.customlink{/*your styles go here*/}
EDIT: if you want only some properties of the link to change on hover, which are going to be the same for two different links (let's say one ha yellow, while the other red colored background, and you wanted them both to have a black background), add another same class to the two links, and stylize it.
JsFiddle Example
You could separate them by commas like a:hover link, a:hover link2, a:hover etc { color: #ff0000; }
Does a:hover { color: #ff0000; } not do what you want it to?

Resources