I am stuck with a problem to underline a element in html
Please find the code here :
<select>
<option>select</option>
<option style="text-decoration: underline;">one </option>
<option>two</option>
<option>three</option>
</select>
I am stuck with a problem to underline a element in html
Please find the code here :
<select>
<option>select</option>
<option style="text-decoration: underline;">one </option>
<option>two</option>
<option>three</option>
</select>
Typically you cannot style option elements. However there are some plugins/hacks which can be used, but you cannot be sure they will be compatible across all platforms.
Refer this answer How to style the option of a html "select"?
When it comes to cross browser most of css can't be applied to native controls like select
in this case. Alternate way is to use jquery-ui instead behind scene they hide select element and show a look a like usually they are either <ul>/<li>
,<span>
or <div>
. Through css you can change it's look and feel too.
Prerequisite:
<script src="//code.jquery.com/jquery-1.11.3.min.js"></script> /*Jquery Library*/
<script src="//code.jquery.com/ui/1.11.3/jquery-ui.min.js"></script> /*Jquery UI Library*/
<script src="//code.jquery.com/ui/1.11.3/themes/smoothness/jquery-ui.css"></script> /*Jquery UI CSS*/
HTML MarkUp:
<select>
<option>select</option>
<option>one</option>
<option>two</option>
<option>three</option>
</select>
Javascript:
$(document).ready(function(){
$('select').selectmenu();
})
StyleSheet (CSS):
li, span
{
text-decoration: underline;
}
.ui-selectmenu-button{
width:150px !important;
}
JSFiddle Demo: Working Demo
While it is possible to style <select>
elements and <option>
elements with a few hacks in the HTML or by throwing Javascript on those, I would never recommend doing so.
Accessibility is always an issue on the web and by turning this standardized method of an input (users can select an option) into something completely different just for visual appeal, you won't help the user.
There are a few people who have written about this subject and I agree with all of them: "Leave the select alone. Style what's styleable if you have to." https://www.aaron-gustafson.com/notebook/native-vs-stylable-tug-of-war/
If you still want to style the select (not the options, that's impossible with CSS alone), there is a wonderful library by FilamentGroup you can just throw into your project: https://github.com/filamentgroup/select-css/