Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select all elements before element with class?

Tags:

html

css

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.

like image 576
saricden Avatar asked Jan 18 '15 04:01

saricden


People also ask

How do you select before an element in CSS?

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.

How do you select elements with class?

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 (.)

What is :: before and :: after?

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.

What is :: before in CSS?

::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.


4 Answers

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>
like image 61
Chris Burton Avatar answered Nov 02 '22 03:11

Chris Burton


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>
like image 29
Paul Lucius Valor Avatar answered Nov 02 '22 03:11

Paul Lucius Valor


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>
like image 25
toaruScar Avatar answered Nov 02 '22 01:11

toaruScar


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.

like image 29
Baraa Al-Tabbaa Avatar answered Nov 02 '22 02:11

Baraa Al-Tabbaa