LESS nesting multuple psuedo elements/classes not working - css

I'm trying to get the output:
#parent a:hover,
#parent a:link,
#parent a:visited {
color: #000;
}
I am using this LESS:
#parent {
a {
:link, :hover, :visited {
color: #000;
}
}
}
it's not working.

You need to use the LESS parent selector &:
#parent a {
&:link, &:hover, &:visited {
color: #000;
}
}

Related

How can I refer to only the parent element in Sass?

Apologies for the vague title; I appreciate suggestions. I've looked for similar questions but none seem (to me) to be asking the exact same thing.
Say I have this piece of CSS:
nav a {
color: blue;
}
nav li.selected a {
color: red;
}
What would be the recommended/preferred/easiest way to achieve this with nesting in Sass? I'd like to target a when it's a child of li.selected (i.e., referring to exactly one level above itself).
nav {
a {
color: blue;
??? {
color: red;
}
}
}
Here's what I've tried to no avail:
nav {
a {
color: blue;
li.selected & {
color: red;
}
}
}
/* results in li.selected nav a, not nav li.selected a */
nav {
$foo: &;
a {
color: blue;
#at-root #{$foo} li.selected & {
color: red;
}
}
}
/* results in nav li.selected nav a, not nav li.selected a */
Sorry if i get it wrong, but is this what you mean?
nav {
a {
color: blue;
}
li.selected a {
color: red;
}
}

Sass: How to get parent selector in hover

Steps to reproduce:
.root {
.parent {
color: red;
&:hover & {
&__child {
background-color: blue;
}
}
}
}
What is actually happening:
.root .parent {
color: red;
}
.root .parent:hover .root .parent__child {
background-color: blue;
}
What is expected:
.root .parent {
color: red;
}
.root .parent:hover .parent__child {
background-color: blue;
}
So, the & in &:hover is not parent selector anymore. How can I get parent selector ?
& is for concatenating selectors together when nesting so you need it only on hover. You don't need the extra &
Try:
/* hover concatenated to 'parent' and the 'parent__child' is nested to hover */
.root {
.parent {
color: red;
&:hover {
.parent__child{
background-color: blue;
}
}
}
}

How to use multiple ampersands for an anchor element?

scss
a {
text-decoration: none;
&:active {
color: $color-secondary;
}
&:visited {
color: $color-primary;
}
&:hover {
color: $color-accent;
}
}
css
a:active {
color: #E4E4E4;
}
a:visited {
color: #333;
}
a:hover {
color: #6DB48B;
}
The compiled css only takes the last property into consideration.
How do I use multiple ampersands for an anchor element?
The :active styles fail to show because they get overridden by the styles that appear lower down in your Sass. To fix this, reorder your Sass in this order:
:visited
:hover
:active

How to combine 3 selectors with a:hover tag?

I have the following CSS rules:
#toolbar1 a:hover {
color: #415682;
}
#toolbar2 a:hover {
color: #415682;
}
#toolbar3 a:hover {
color: #415682;
}
How can I combine these into a single rule? I've tried
#toolbar1 #toolbar2 #toolbar3 a:hover {
color: #415682;
}
but it didn't work. I've tried placing commas between these, also, to no avail.
For wide browser compatibility, you’ll just have to repeat yourself:
#toolbar1 a:hover,
#toolbar2 a:hover,
#toolbar3 a:hover {
color: #415682;
}
Some day, :matches might be usable:
:matches(#toolbar1, #toolbar2, #toolbar3) a:hover {
color: #415682;
}
If you want something similar before then, CSS preprocessors are an option.
In this particular case, though, the structure suggests a class might be appropriate:
.toolbar a:hover {
color: #415682;
}

Sass only selecting parent [duplicate]

I have the following SCSS for styling links in my menu:
nav {
ul {
li {
a {
color: red
}
}
}
ul.opened {
li {
a {
color: green
}
}
}
}
Which generates the following (correct) CSS:
nav ul li a {
color: red;
}
nav ul.opened li a {
color: green;
}
I tried modifying my JavaScript to apply the class to the nav element instead, and use selector-append() in Sass to append the class. But that seems to do the appending in the wrong order (and if the arguments are reversed, the class is appended to the last element!):
nav {
ul {
li {
a {
color: red;
#at-root #{selector-append('.opened', &)} {
color: green;
}
}
}
}
}
Output (incorrect!):
nav ul li a {
color: red;
}
.openednav ul li a {
color: green;
}
Is there a way the SCSS can be rewritten so that the class can be correctly appended without having to duplicate selectors (similar to the selector-append() method)?
The short answer
Since the element we want to replace has a unique name, what we're looking for is this:
nav {
ul {
li {
a {
color: red;
#at-root #{selector-replace(&, 'ul', 'ul.opened')} {
color: green;
}
}
}
}
}
The long answer
Manipulating selectors is extremely dirty, and I would advise against it unless you absolutely had to. If you're overqualifying your selectors by specifying things like table tr td or ul li, then start by simplifying: tr and ul are both redundant in these selectors (unless you're trying to avoid styling elements under an ordered list). Adjust your nesting to be simpler, etc.
Starting with Sass version 3.4, there are 2 important features that allow you to modify selectors.
Selector functions
The parent selector can be stored in a variable
Example:
.foo ul > li a, .bar {
$sel: &;
#debug $sel;
}
You'll always get a list of list of strings because selectors can be chained together with a comma, even when you have only one selector.
.foo ul > li a, .bar { ... }
(1 2 3 4 5), (1)
You'll note that the descendant selector is being counted here (lists in Sass can be either space or comma delimited). This is extremely important to remember.
When selector-replace() doesn't work
The selector-replace() function does not work in the following cases:
The selector you want to replace is not unique (eg. ul ul li)
You want to insert one or more selectors (eg. ul ul li -> ul ul ul li)
You want to remove a selector (eg. ul > li -> ul li)
In this case, you'll need to loop over the selectors and you'll need to know which position you want to modify. The following function will take a function and apply it to a specific position in your selector using the magic of the call() function.
#function selector-nth($sel, $n, $f, $args...) {
$collector: ();
#each $s in $sel {
$modified: call($f, nth($s, $n), $args...);
$collector: append($collector, set-nth($s, $n, $modified), comma);
}
#return $collector;
}
Append a class (when the selector isn't unique or you don't know its name)
The function we need here takes 2 arguments: the original selector and the selector you'd like to append to it. Uses simple interpolation to do the job.
#function append-class($a, $b) {
#return #{$a}#{$b};
}
.foo, .bar {
ul > li a {
color: red;
#at-root #{selector-nth(&, -2, append-class, '.baz')} {
color: blue;
}
}
}
Output:
.foo ul > li a, .bar ul > li a {
color: red;
}
.foo ul > li.baz a, .bar ul > li.baz a {
color: blue;
}
Insert a selector
This function also takes 2 arguments: the original selector and the selector you'd like to insert before it.
#function insert-selector($a, $b) {
#return $b $a;
}
.foo, .bar {
ul > li a {
color: red;
#at-root #{selector-nth(&, -2, insert-selector, '.baz')} {
color: blue;
}
}
}
Output:
.foo ul > li a, .bar ul > li a {
color: red;
}
.foo ul > .baz li a, .bar ul > .baz li a {
color: blue;
}
Remove a selector
Removing a selector is as simple as replacing your selector with an empty string.
#function remove-selector($sel) {
#return '';
}
.foo, .bar {
ul > li a {
color: red;
#at-root #{selector-nth(&, -2, remove-selector)} {
color: blue;
}
}
}
Output:
.foo ul > li a, .bar ul > li a {
color: red;
}
.foo ul > a, .bar ul > a {
color: blue;
}
TL;DR
Selectors are just a lists. Any list manipulation functions will work on it and you can loop over it to modify it as necessary.
So yeah, don't do it unless you really really really need to. If you've decided you still need it, I've packaged these functions up into the selector-nth library.
I made a mixin that solves this problem.
Github: https://github.com/imkremen/sass-parent-append
Example: https://codepen.io/imkremen/pen/RMVBvq
Usage (scss):
.ancestor {
display: inline-flex;
.grandparent {
padding: 32px;
background-color: lightgreen;
.parent {
padding: 32px;
background-color: blue;
.elem {
padding: 16px;
background-color: white;
#include parent-append(":focus", 3) {
box-shadow: inset 0 0 0 8px aqua;
}
#include parent-append(":hover") {
background-color: fuchsia;
}
#include parent-append("p", 0, true) {
background-color: green;
}
}
}
}
}
Result (css):
.ancestor {
display: inline-flex;
}
.ancestor .grandparent {
padding: 32px;
background-color: lightgreen;
}
.ancestor .grandparent .parent {
padding: 32px;
background-color: blue;
}
.ancestor .grandparent .parent .elem {
padding: 16px;
background-color: white;
}
.ancestor:focus .grandparent .parent .elem {
box-shadow: inset 0 0 0 8px aqua;
}
.ancestor .grandparent .parent:hover .elem {
background-color: fuchsia;
}
.ancestor .grandparent .parent p.elem {
background-color: green;
}

Resources