-3

I have a ul list whose li has a specific class. I want to change the back ground color of that ul.

Eg:

<ul class="dropdown-menu">
    <li class="">About us</li>
    <li class="">Why Join?</li>
    <li class="">Constitution</li>

</ul>
<ul class="dropdown-menu">
    <li class="active">2016 Conference</li>
    <li class="">Member Home Page</li>
    <li class="">Bulletin Board</li>
</ul>

I want to change second ul background color.

I have tried following code

ul.dropdown-menu li.active {
    background-color: #e9124a !important;
}

5 Answers5

1

Check out this...

I think you cannot do it without the help of Javascript

document.getElementsByClassName('active')[0].parentElement.style.background = '#e9124a';
<ul class="dropdown-menu">
  <li class="">About us</li>
  <li class="">Why Join?</li>
  <li class="">Constitution</li>

</ul>
<ul class="dropdown-menu">
  <li class="active">2016 Conference</li>
  <li class="">Member Home Page</li>
  <li class="">Bulletin Board</li>
</ul>
Sankar
  • 6,182
  • 2
  • 23
  • 43
0

If you want to select the 2nd ul element:

.dropdown-menu:nth-child(2) {
  background-color: #F0D0D0;
  }
<ul class="dropdown-menu">
    <li class="">About us</li>
    <li class="">Why Join?</li>
    <li class="">Constitution</li>

</ul>
<ul class="dropdown-menu">
    <li class="active">2016 Conference</li>
    <li class="">Member Home Page</li>
    <li class="">Bulletin Board</li>
</ul>

If you want to select all the ul who contains a li with class active, as far as I know it is not possible with only CSS.

Al Foиce ѫ
  • 3,844
  • 10
  • 34
  • 45
0

with CSS ( Cascading Style Sheets ) you can select ONLY from TOP to BOTTOM of the HTML structure. not the other way. so you CAN'T select an element based on it's children .

you can do this with a simple jQuery see snippet below

$("ul").has("li.active").css("background-color","#e9124a ")
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul class="dropdown-menu">
    <li class="">About us</li>
    <li class="">Why Join?</li>
    <li class="">Constitution</li>

</ul>
<ul class="dropdown-menu">
    <li class="active">2016 Conference</li>
    <li class="">Member Home Page</li>
    <li class="">Bulletin Board</li>
</ul>
Mihai T
  • 15,254
  • 2
  • 16
  • 30
-2

See this question here

There is currently no way to select the parent of an element in CSS. In the meantime, you'll have to resort to JavaScript if you need to select a parent element.

Community
  • 1
  • 1
Aleksandar Matic
  • 779
  • 1
  • 9
  • 21
-2

you can add a new class for the second ul. e.g.

<ul class="dropdown-menu">
    <li class="">About us</li>
    <li class="">Why Join?</li>
    <li class="">Constitution</li>

</ul>
<ul class="dropdown-menu new-color">
    <li class="active">2016 Conference</li>
    <li class="">Member Home Page</li>
    <li class="">Bulletin Board</li>
</ul>

ul.dropdown-menu li.active {
    background-color: #e9124a !important;
}

//setting the color of the second ul
.new-color {
    background-color: #c6ffb3 !important;
}