For example:
You have this:
<ul>
<li>zero</li>
<li>one</li>
<li class="selected">two</li>
<li>three</li>
<li>more elements</li>
</ul>
CSS:
.selected:previous {
color: red;
}
.selected:next {
color: green;
}
That's possible?
The element+element selector is used to select an element that is directly after another specific element.
When you group CSS selectors, you apply the same styles to several different elements without repeating the styles in your stylesheet. Instead of having two, three, or more CSS rules that do the same thing (set the color of something to red, for example), you use a single CSS rule that accomplishes the same thing.
class selector selects elements with a specific class attribute. To select elements with a specific class, write a period (.) character, followed by the name of the class.
It's not possible with pure css. You can style next but not previous element.
But you can do a trick with css. Instead of give class selected you can give class to your previous element. Like this:
HTML
<ul>
<li>zero</li>
<li class="previous">one</li>
<li>two</li>
<li>three</li>
<li>more elements</li>
</ul>
CSS
.previous{
color: green;
}
.previous + li{
color: red;
}
.previous + li + li{
color:yellow;
}
Check this http://jsfiddle.net/S5kUM/2/
It's called sibling selector:
.selected + li {
color: red;
}
Fiddle here
However, there is no sibling selector for the previous element.
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