In CSS is it possible to select all elements before an element with a given class?
Example HTML:
<div>
<a href>One</a>
<a href>Two</a>
<a href>Three</a>
<a href class="active">Four</a>
<a href>Five</a>
</div>
And CSS:
.active:(all-before) {
border-left: solid 1px #C0FFEE;
}
So links 'One', 'Two', and 'Three' would would have a left border but 'Four' and 'Five' would not.
The CSS ::before selector can be used to insert content before the content of the selected element or elements. It is used by attaching ::before to the element it is to be used on. In the example above we are prepending an asterisk and a space before every paragraph element on the page.
To select elements with a specific class, write a period (.) character, followed by the name of the class. You can also specify that only specific HTML elements should be affected by a class. To do this, start with the element name, then write the period (.)
Definition and UsageThe ::before selector inserts something before the content of each selected element(s). Use the content property to specify the content to insert. Use the ::after selector to insert something after the content. Version: CSS2.
::before (:before) In CSS, ::before creates a pseudo-element that is the first child of the selected element. It is often used to add cosmetic content to an element with the content property. It is inline by default.
a {
text-decoration: none;
border-left: 1px solid black;
}
a.active, a.active ~ * {
border: none;
}
<div>
<a href>One</a>
<a href>Two</a>
<a href>Three</a>
<a href class="active">Four</a>
<a href>Five</a>
</div>
Okay. What is really going to work is the use of flex-direction
in combination with the ~
selector.
.container {
display: flex;
/* could also be column-reverse*/
flex-direction: row-reverse;
justify-content: flex-end;
}
.item {
/* just styling */
margin: 0 8px;
border: 1px solid gray;
border-radius: 3px;
padding: 4px 8px;
transition: all .1s ease-in-out;
}
/* we are still selecting every element after according to HTML
* but as they have reversed order, we're applying styles to all
* elements that were rendered before */
.item:hover ~ .item {
color: coral;
border-color: coral;
}
<div class="container">
<!-- notice how children are ordered in reverse direction -->
<div class="item">Third</div>
<div class="item">Second</div>
<div class="item">First</div>
</div>
Baraa Al-Tabbaa's answer has a drawback that all other elements' value will be set to none.
To avoid this, you can use multiple :not
selectors, so the rest of the elements can still inherent properties from the parent.
a:not(a.active ~ a):not(a.active) {
border-left: solid 5px #C0FFEE;
}
<div>
<a href>One</a>
<a href>Two</a>
<a href>Three</a>
<a href class="active">Four</a>
<a href>Five</a>
</div>
Well, Normally it's not possible, but you can hack it someway.
So for example if you want to do this:
.active:(all-before) {
border-left: solid 1px #C0FFEE;
}
then you can do this:
a {
border-left: solid 1px #C0FFEE;
}
a.active, a.active~a {
border-left: none;
}
So you put the style you want in the first selector, and then you disable that design in the second selector.
Working example: http://jsfiddle.net/prrd14u2/
Also you can use javascript, jquery as another solution.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With