0

I want to select one element before and one element after the given class with CSS only. I'm wondering if that's possible ... Here is my list:

<ul>
    <li>one</li>
    <li>two</li>
    <li class='active'>three</li>
    <li>four</li>
    <li>five</li>
    <li>six</li>
</ul>

I want to apply some styles to one element before and one after .active. I can do it with javascript - no problem, but I was wondering if something like this could be possible:

/* before .active */
ul li.active-element(.active - 1) {
  background:red;
}
/* after .active */
ul li.active-element(.active + 1) {
  background:blue;
}
Mohammad Usman
  • 37,952
  • 20
  • 92
  • 95
bukowski
  • 1,893
  • 7
  • 34
  • 54

4 Answers4

1

You Can't select previous sibling. But yes you can select next sibling by

ul li.active+ li{
  background:blue;
}

Js fiddle Link

You can select previous sibling by Javascript.

priya_singh
  • 2,478
  • 1
  • 14
  • 32
1

With Pure CSS you cannot select previous sibling with css.

But you can select next sibling with next sibling + selector i.e

li.active + li {
    background: blue;
}

However in some cases you can make previous sibling to look like selected (But it has its limitations).

While for previous sibling if you just wants to apply background-color on previous item, you can make previous items looks like selected with pseudo class i.e :before or :after of active element.

.list {
  list-style: none;
  overflow: hidden;
  padding: 0;
  margin: 0;
}
.list li {
  position: relative;
  padding: 3px 10px;
}
.list li.active {
  background: green;
}
.list li.active:before {
  position: absolute;
  background:red;
  height: 100%;
  bottom: 100%;
  content: '';
  z-index: -1;
  right: 0;
  left: 0;
}
.list li.active + li {
  background:blue;
}
<ul class="list">
  <li>one</li>
  <li>two</li>
  <li class='active'>three</li>
  <li>four</li>
  <li>five</li>
  <li>six</li>
</ul>

With JavaScript (jQuery Framework) you can select previous select previous sibling.

You can use jQuery's .prev() method to select previous sibling i.e.

$('.list li.active').prev().addClass('prev');

$(function() {
  $('.list li.active').prev().addClass('prev');
});
.list {
  list-style: none;
  padding: 0;
  margin: 0;
}
.list li {
  padding: 3px 10px;
}
.prev {
  background: red;
}

.active {
  background: green;
}
.active + li {
  background: blue;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<ul class="list">
  <li>one</li>
  <li>two</li>
  <li class='active'>three</li>
  <li>four</li>
  <li>five</li>
  <li>six</li>
</ul>
Mohammad Usman
  • 37,952
  • 20
  • 92
  • 95
  • This will not work in my case as I need to select other elements inside
  • , background was just for simple example – bukowski Oct 13 '16 at 07:13
  • @fjckls I've suggested this because in your question requirement was only to change `background-color` of previous sibling. And yes, it has its limitations. You can apply styles to parent only and not on child elements. – Mohammad Usman Oct 13 '16 at 07:16