Question

I'm trying to style all <select> elements that are a descendant of a <div>. I could have a put a class on them but i was trying to be clever :-)

I believe it doesn't work in ie6 but does it work in ie7 etc?

How do you do it

Here is one of my selects (it has no class or id) but they are all descendents of a div whose id is "content"

<div id="content">
    <select >
        <option></option>
    </select>
</div>

Any ideas?

Was it helpful?

Solution

If you want any select that is a descendant of an element with id="content":

#content select { ... }

If you want any select that is a direct descendant of an element with id="content":

#content > select { ... }

If you want to limit it to only div elements with id="content":

div#content select { ... }

The second one might not work in some older browsers, but the others should work in even an ancient browser as long as it has any css support at all, like Netscape 4.

OTHER TIPS

This should work across browsers:

#content select {
   // Styles for selects inside the div with id `content'
}
.myDiv select {
  font-family:verdana;
}

<div class="myDiv">
  <p><select>...</select></p>
  <p><select>...</select></p>
</div>
div#content > select {
}
.content select {

              css here

}

I believe this is how you can accomplish this. Where "content" is the class of your div. This will format all selects inside of a div that has the class of "content".

Best of Luck!

Nick's solution only gets direct descendants, so it wouldn't get a select in a form in the div. This will get them no matter how many other elements deep they are:

#content select { width: 100px }

If you only want to get, for example, the text inputs you can do this:

#content select[type=text] { width: 100px }

and don't forget that password inputs are different than text inputs!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top