You can't do this with the <select>
element. You can't have endless nested subcategory like you suggest, only one, with <optgroup>
. Styling will also be difficult as the ability to style form elements differs from browser to browser.
However, depending on your needs, the following solution might work:
For this, we are recreating the select
element using HTML lists. The markup would look like this:
<div id="select">
<p>Select your Answer</p>
<ul>
<li><a href="#">Apple</a></li>
<li><a href="#">Arts and Entertainment</a>
<ul>
<li><a href="#">Amusement</a></li>
<li><a href="#">Art</a></li>
<li><a href="#">Artist</a>
<ul>
<li><a href="#">A</a></li>
<li><a href="#">B</a></li>
</ul></li>
</ul>
</li>
</ul>
</div>
Then, with CSS, style it such that it fits your purpose:
#select {
border: 1px solid #999;
background-color: rgba(0, 0, 0, 0.1);
position: absolute;
width: 300px;
font-family: Arial, sans-serif;
font-size: 11px;
}
#select:hover {
background-color: #efefef;
}
#select > ul {
display: none;
list-style: none;
}
#select:hover > ul {
display: block;
}
#select > ul ul {
margin-left: 20px;
}
#select > ul a, #select > p {
display: block;
padding: 3px 8px 4px;
color: #333;
text-decoration: none;
}
#select > ul a:hover {
background-color: #ddd;
}
Play around with it here: http://jsfiddle.net/thHFS/