1

I have

#boxes { visibility: hidden } 
.active { visibility: visible }

I want the div to be hidden unless .active is in use. I tried <div id="boxes" class="active"> but the div was still hidden.

Is there anyway for the .active class to override the hidden visibility??

Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
Friedpanseller
  • 654
  • 3
  • 16
  • 31

4 Answers4

2

CSS selectors have "specificity" or weight (6.4.3 Calculating a selector's specificity ) which defines what selector takes precedence. Selectors which refer to elements by ID have high weight, so to override it you need more specific selector for "active":

#boxes.active { visibility: visible }

Or less specific selector for #boxes in some way. Class selector is more important than element selector, so it will override visibility (jsfiddle ):

div  { visibility: hidden } 
.active { visibility: visible }
Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
0

If your <div> element is being used as a same-page link with multiple #boxes, such as for tabbed pages, you can also use :target. This would allow you to style each one individually:)

<div id="boxes">
    <div class="box1"></div>
    <div class="box2"></div>
</div>

The CSS would change slightly...adding a space after #boxes, since the class is in a decendent <div>.

#boxes .box1:target{visibility: visible;}
#boxes .box1:not(:target){visibility: hidden;}
#boxes .box2:target{visibility: visible;}
#boxes .box1:not(:target){visibility: hidden;}
bcintegrity
  • 213
  • 2
  • 11
0
#boxes.active { visibility: visible !important; }

Actually '!important' is not necessary when pairing a div with class, but if it doesn't works for you, just consider this..

Ganesh Kumar G
  • 185
  • 2
  • 16
0

You can use !important for giving the importance of value. !import simple meaning is, It is important than others so execute only it.

#boxes.active { 
    visibility: visible !important; 
}

Learn more here

SpaceDogCS
  • 2,808
  • 3
  • 20
  • 49
Vivek javiya
  • 168
  • 2
  • 5