Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to bold the first option of a select across all browsers?

I have tried to bold the first option of the select box but it's only working in Firefox, not other browsers (Chrome, IE). Here is the code I've tried.

HTML

<select id="htmldisabled">
    <option class="bold">test1</option>
    <option>test2</option>
    <option>test3</option>
    <option>test4</option>
    <option>test5</option>
    <option>test6</option>
</select>

CSS

.bold {
     font-weight:bold;   
}

See demo at jsfiddle.

like image 876
Mr.T.K Avatar asked Dec 02 '11 13:12

Mr.T.K


People also ask

How do you make bold options in HTML?

To bold the text in HTML, use either the strong tag or the b (bold) tag. Browsers will bold the text inside both of these tags the same, but the strong tag indicates that the text is of particular importance or urgency. You can also bold text with the CSS font-weight property set to “bold.”

How do I select the first option in CSS?

The :first-of-type selector in CSS allows you to target the first occurence of an element within its container. It is defined in the CSS Selectors Level 3 spec as a “structural pseudo-class”, meaning it is used to style content based on its relationship with parent and sibling content.

How to select element in HTML?

Definition and Usage. The <select> element is used to create a drop-down list. The <select> element is most often used in a form, to collect user input. The name attribute is needed to reference the form data after the form is submitted (if you omit the name attribute, no data from the drop-down list will be submitted) ...


3 Answers

#htmldisabled:first-child{
    font-weight:bold;
}
like image 191
antonpug Avatar answered Sep 26 '22 06:09

antonpug


As you've discovered, it can't be done cross-browser ;)

Best to use a selectbox replacement script if you need special formatting or display

like image 27
danwellman Avatar answered Sep 22 '22 06:09

danwellman


To my knowledge it doesnt work in IE unless you style the select tag:

select{
    font-weight:bold;
}

However this will influence all options. I dont know of another css-only solution, but would be interested in one as-well :)

like image 39
r0skar Avatar answered Sep 25 '22 06:09

r0skar