Why has SELECT different height if only one empty option exists? - html

In this sample http://jsfiddle.net/2SuN8/1/
<select>
<option value="0"></option>
</select>
<select>
<option value="0"></option>
<option value="1">A</option>
</select>
there is a 1px height difference between those two drop-downs.
I assume this happens when one select contains only one empty option.
Should I avoid empty default options or do You know any way to work around this?
I've seen this issue in FF 27.0.1 (Ubuntu 13.10 and Windows Server 2008) and with IE 10 (Windows Server 2008).
Thanks
Hannes

This is happening because you aren't specifying a height for the select element, so by default its height is determined by its content. In the case of your select element with only one empty option, as there is no content, there is no height.
CSS Solution
The CSS solution would be to simply add a default height to your select element:
select {
height: 20px;
}
JSFiddle demo.
HTML Solution
The HTML solution, as mentioned by others here, is to populate your empty option with some content:
<select>
<option value="0"> </option>
</select>
JSFiddle demo.
In this case, is the non-breaking space entity.
Personally I'd go with the CSS option.

now there is no different height :D
change
<option value="0"></option>
to
<option value="0"> </option>
http://jsfiddle.net/2SuN8/2/

Yes, you should avoid empty select options. Instead you can set empty value for first option.:
<select>
<option value=""> - - select - -</option>
<option value="a"> A </option>
<option value="b"> B </option>
</select>

Set the width and/or the height and they will be the same size.
<select style="width:100px; height:20px;">
<option value="0"></option>
</select>
<select style="width:100px; height:20px;">
<option value="0"></option>
<option value="1">A</option>
<option value="2">B</option>
</select>
Edit: My version is the quick and dirty version, if you want the right way to do it, use a separate CSS file or a space substitute as others have described.

The reason that it happens is that in some browsers (for example Firexox) the select element for some reason will grow to accomodate its largest option value, eventhough the value may not be shown in the select in the same way as in the option.
For example:
<select>
<option value="0"></option>
</select>
<select>
<option value="0"></option>
<option value="1">A</option>
<option value="2" style="font-size:20px">B</option>
</select>
Demo: http://jsfiddle.net/Guffa/2SuN8/3/
In firefox the select element will be tall enough to accomodate a 20px text, but if you choose the option B it will not be shown with the font size specified for the option, but with the font size specified for the select.
One workaround would be to set a specific height to the select. A workaround for the specific case of the empty option could be to use a space inside it, like Sharky showed.

Related

On OSX a <select> element always has first option checked by default [duplicate]

I have a very weird requirement, wherein I am required to have no option selected by default in drop down menu in HTML. However,
I cannot use this,
<select>
<option></option>
<option>Option 1</option>
<option>Option 2</option>
<option>Option 3</option>
</select>
Because, for this I will have to do validation to handle the first option. Can anyone help me in achieving this target without actually including the first option as part of the select tag?
Maybe this will be helpful
<select>
<option disabled selected value> -- select an option -- </option>
<option>Option 1</option>
<option>Option 2</option>
<option>Option 3</option>
</select>
-- select an option -- Will be displayed by default. But if you choose an option, you will not be able to select it back.
You can also hide it using by adding an empty option
<option style="display:none">
so it won't show up in the list anymore.
Option 2
If you don't want to write CSS and expect the same behaviour of the solution above, just use:
<option hidden disabled selected value> -- select an option -- </option>
You could use Javascript to achieve this. Try the following code:
HTML
<select id="myDropdown">
<option>Option 1</option>
<option>Option 2</option>
<option>Option 3</option>
</select>
JS
document.getElementById("myDropdown").selectedIndex = -1;
or JQuery
$("#myDropdown").prop("selectedIndex", -1);
Today (2015-02-25)
This is valid HTML5 and sends a blank (not a space) to the server:
<option label=" "></option>
Verified validity on http://validator.w3.org/check
Verified behavior with Win7(IE11 IE10 IE9 IE8 FF35 Safari5.1) Ubuntu14.10(Chrome40, FF35) OSX_Yosemite(Safari8, Chrome40) Android(Samsung-Galaxy-S5)
The following also passes validation today, but passes some sort of space character to the server from most browsers (probably not desirable) and a blank on others (Chrome40/Linux passes a blank):
<option> </option>
Previously (2013-08-02)
According to my notes, the non-breaking-space entity inside the option tags shown above produced the following error in 2013:
Error: W3C Markup Validaton Service (Public): The first child option
element of a select element with a required attribute and without a
multiple attribute, and whose size is 1, must have either an empty
value attribute, or must have no text content.
At that time, a regular space was valid XHTML4 and sent a blank (not a space) to the server from every browser:
<option> </option>
Future
It would make my heart glad if the spec was updated to explicitly allow a blank option. Preferably using the briefest syntax. Either of the following would be great:
<option />
<option></option>
Test File
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Test</title>
</head>
<body>
<form action="index.html" method="post">
<select name="sel">
<option label=" "></option>
</select>
</form>
</body>
</html>
<td><b>Field Label:</b><br>
<select style='align:left; width:100%;' id='some_id' name='some_name'>
<option hidden selected>Select one...</option>
<option value='Value1'>OptLabel1</option>
<option value='Value2'>OptLabel2</option>
<option value='Value3'>OptLabel3</option></select>
</td>
Just put "hidden" on option you want to hide on dropdown list.
Solution that works by only using CSS:
A: Inline CSS
<select>
<option style="display:none;"></option>
<option>Option 1</option>
<option>Option 2</option>
<option>Option 3</option>
</select>
B: CSS Style Sheet
If you have a CSS file at hand, you can target the first option using:
select.first-opt-hidden option:first-of-type {
display:none;
}
<select class="first-opt-hidden">
<option></option>
<option>Option 1</option>
<option>Option 2</option>
<option>Option 3</option>
</select>
This should help:
https://www.w3schools.com/tags/att_select_required.asp
<form>
<select required>
<option value="">None</option>
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="mercedes">Mercedes</option>
<option value="audi">Audi</option>
</select>
<button type="submit">Submit</button>
</form>
Just a small remark:
some Safari browsers do not seem to respect neither the "hidden" attribute nor the style setting "display:none" (tested with Safari 12.1 under MacOS 10.12.6). Without an explicit placeholder text, these browsers simply show an empty first line in the list of options. It may therefore be useful to always provide some explanatory text for this "dummy" entry:
<option hidden disabled selected value>(select an option)</option>
Thanks to the "disabled" attribute, it won't be actively selected anyway.
<select required>
<option value="" disabled selected>None</option>
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="mercedes">Mercedes</option>
<option value="audi">Audi</option>
</select>
You can avoid custom validation in this case.
I understand what you are trying to do.The best and the most successful way is :
<select name='department' required>
<option value="">None</option>
<option value="Teaching">Teaching department</option>
<option value="nonTeaching">Non-teaching department</option>
</select>
I found it really interesting because I just experienced the same thing not so long time ago.
However, I came across to an example on the Internet about the solution regarding this.
Without any further ado, see the code fragment below:
<select>
<option value data-isdefault="true">--Choose one Option--</option>
<option>Option 1</option>
<option>Option 2</option>
<option>Option 3</option>
</select>
With that, it will stay un-submittable but selectable, anytime. More convenience for User Interface and great for User Experience.
Well that's all, I hope it helps. Cheers!
There is no HTML solution. By the HTML 4.01 spec, browser behavior is undefined if none of the option elements has the selected attribute, and what browsers do in practice is that they make the first option pre-selected.
As a workaround, you could replace the select element by a set of input type=radio elements (with the same name attribute). This creates a control of the same kind though with different appearance and user interface. If none of the input type=radio elements has the checked attribute, none of them is initially selected in most modern browsers.
I'm using Laravel 5 framework and #Gambi `s answer worked for me as well but with some changes for my project.
I have the option values in a database table and I use them with a foreach statement. But before the statement I have added an option with #Gambit suggested settings and it worked.
Here my exemple:
#isset($keys)
<select>
<option disabled selected value></option>
#foreach($keys as $key)
<option>{{$key->value)</option>
#endforeach
</select>
#endisset
I hope this helps someone as well. Keep up the good work!
Try this:
<h2>Favorite color</h2>
<select name="color">
<option value=""></option>
<option>Pink</option>
<option>Red</option>
<option>Blue</option>
</select>
The first option in the drop down would be blank.
In order to show please select a value in drop down and hide it after some value is selected . please use the below code.
it will also support required validation.
<select class="form-control" required>
<option disabled selected value style="display:none;">--Please select a value</option>
<option >Data 1</option>
<option >Data 2</option>
<option >Data 3</option>
</select>
If you are using Angular (2+), (or any other framework), you could add some logic. The logic would be: only display an empty option if the user did not select any other yet.
So after the user selected an option, the empty option disappears.
For Angular (9) this would look something like this:
<select>
<option *ngIf="(hasOptionSelected$ | async) === false"></option>
<option *ngFor="let option of (options$ | async)[value]="option.id">{{ option.title }}</option>
</select>
For those who are using <select multiple> (combobox; no dropdown), this worked for me:
<select size=1 disabled multiple>
<option hidden selected></option>
<option>My Option</option>
</select>
If you don't need any empty option at first, try this first line:
<option style="display:none"></option>
just use "..option hidden selected.." as default option
I guess a good idea would be to use the radio buttons, set #1 as default and hide it, give it for example a
name="init" and a value="null" or whatever, up to you!
this way the radio buttons list has a value definitely, but default of null can be used logically!
I think it's not necessary to elaborate further, since the idea can easily be implemented with display: none; or visibility: hidden;
... whereas I think the first one display: none; is the better option:
In react, you can give a dummy value (say -1) with select tag as below and same value can be used with this disabled option of yours. (WORKED FOR ME)
const nonEmpty = selected[identifierField] || false;
<select
onChange={(e) => {
onSelect(
options.find((option) => option[identifierField] === e.target.value)
);
}}
value={nonEmpty || -1}
>
<option disabled value={-1}>Select Option</option>
{options.map((option) => (
<option key={option[identifierField]} value={option[identifierField]}>
{option[displayField]}
</option>
))}
</select>
option style="display:none"
Is bad solution for Tablet: iPad Pro / iOS 15 / Safari
An unnecessary row in the dropdown appears, only for real devices. Doesn`t reproduce on the emulator.
Try this:
<select>
<option value="">
<option>Option 1
<option>Option 2
<option>Option 3
</select>
Validates in HTML5. Works with required attribute in select element. Can be re-selected. Works in Google Chrome 45, Internet Explorer 11, Edge, Firefox 41.

Add space between two text inside an options tag

I need to display the following using select/options tag
January (1)
February (2)
March (3)
.
.
.
December (12)
How to give space between the month and its number? I tried usng nbsp , emsp, ensp , thinsp but the space differs when I open it in Firefox and chrome. The space needs to be equal in both browsers.
How can I achieve it?
Thanks.
Here are two ways to have the month names and the month numbers line up in two columns, as though they were table columns.
<select>
<option>--Choose Month--</option>
<option value="1">01 (January)</option>
<option value="2">02 (February)</option>
<option value="3">03 (March)</option>
<option value="4">04 (April)</option>
<option value="5">05 (May)</option>
<option value="6">06 (June)</option>
<option value="7">07 (July)</option>
<option value="8">08 (August)</option>
<option value="9">09 (September)</option>
<option value="10">10 (October)</option>
<option value="11">11 (November)</option>
<option value="12">12 (December)</option>
</select>
<select style="font-family: monospace">
<option>--Choose Month--</option>
<option value="1">January (1)</option>
<option value="2">February (2)</option>
<option value="3">March (3)</option>
<option value="4">April (4)</option>
<option value="5">May (5)</option>
<option value="6">June (6)</option>
<option value="7">July (7)</option>
<option value="8">August (8)</option>
<option value="9">September (9)</option>
<option value="10">October (10)</option>
<option value="11">November (11)</option>
<option value="12">December (12)</option>
</select>
The resulting dropdowns look like this, in Chrome, Firefox, IE, Safari & Opera:
If <option>January (1)</option>, containing a normal space, has different spacing in different situations, this is caused by the use of different fonts. The width of a space varies by font. So you would need to set the font of the option element; it is safest to set that font on the select element too, since some browsers don’t let you style option elements directly, only via select.
For maximal compatibility, you would need to use a downloadable font with #font-face.
Use & nbsp ; in the tag between month name and number. You can use as many spaces as you want. Remove space between & and nbsp; in the code.
Use & nbsp, use many of it, until satisfied with the result

use css to resize select tag in form to see all option tags

Is it possible to dynamicaly resize the visible options in select tag in forms? I have the example:
<select size="1">
<option value='1'>1
<option selected value='2'>2
<option value='3'>3
<option value='4'>4
</select>
I would like to have visible all options (to setup size dynamically with css) when design page for printing. And also to see selected option(s) in another design (color, bold ...). For resize I tried:
select{
size:4;
}
but it doesn't work. I need a working solution at least for FF, IE, Safari ...
Do have any idea?
Thanks in advance!
You can use this way:
<select size="1" size="4">
<option value='1'>1</option>
<option selected value='2'>2</option>
<option value='3'>3</option>
<option value='4'>4</option>
</select>​​​​​​​​​​​​
And don't forget to close the </option>

Are Multi-line Options in Html Select Tags Possible?

Is it possible (using HTML only) to display select with options that span multiple lines each?
It is not possible using html select control.
You can use a div that can act as a dropdown list using JavaScript and css.
not only is it not possible on standard html, but it would then (as an object) become incompatible with the way IOS devices handle the option tag, which is to display a scroll list so it is not even reasonable to want the option tag to behave that way as it would break cross-device compatibility badly and unexpectedly.
as others have answered (i dont have enough reputation to upvote or comment yet) have said, it must be done with css/div styling etc and in that way is a lot more extensible with full html functionality within each of the option tag's as well as (via css styling) being mobile device friendly.
If your case is around iOS truncating long option text, then the solution from How to fix truncated text on <select> element on iOS7 can help.
Add an empty optgroup at the end of the select list:
You can implement like this:
<select>
<option selected="" disabled="">option first</option>
<option>another option that is really long and will probably be truncated on a mobile device</option>
...
<optgroup label=""></optgroup>
</select>
As the presentation of a select element is up to the user agent, I'm afraid you can't have that, unless some UA actually implements it. But select as either a ListBox or ComboBox never really had much need for items spanning multiple lines. Furthermore it would greatly confuse users as they are used to one line = one item.
No.
You could use radio buttons instead though, their <label>s can word wrap.
It would be possible by using some JavaScript with CSS styling on HTML elements, easily done with a framework like Dojo Toolkit. Otherwise, use Radio or Checkbox controls.
If you have lots of options and for that reason looking for multi-line possibility, then there is another trick that can be helpful. Instead of using select and option tag, use datalist and option tag. By this, users can search for their option inside the select area.
<input list="stocks" name="stockArea" placeholder="hello">
<label for="stockArea">Select Your Stock</label>
<datalist id="stocks">
<option value="Microsoft" >
<option value="Lenovo">
<option value="Apple">
<option value="Twitter">
<option value="Amazon">
</datalist>
What about:
<!DOCTYPE html>
<html>
<body>
<select size="13" multiple>
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="opel">Opel</option>
<option value="audi">Audi</option>
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="opel">Opel</option>
<option value="audi">Audi</option>
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="opel">Opel</option>
<option value="audi">Audi</option>
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="opel">Opel</option>
<option value="audi">Audi</option>
</select>
</body>
</html>
I don't know if this is what you were looking for, but maybe it could help you.
If you want to select multiple options, you must press Ctrl + click to select more options.
If you want to disable multiselect, just erase the "multiple" parameter from the SELECT tag.

Rendering a hierarchy of "OPTION"s in a "SELECT" tag

My problem is HTML and CSS related. I have a hierarchy type structure that I want to display inside a list. The hierarchy contains Countries, States and Cities (it is three levels deep).
I want to display the list inside a select list, each item type (Country, State, City) must be selectable. The items should appear indented as:
United States
- Hawaii
-- Kauai
- Washington
-- Seattle
-- Chelan
The problem is with the indentation. I am trying to use either margin-left or padding-left to indent the tags, which appear correct in FireFox but not in IE7. This is an example of the generated select list:
<select name="Something">
<option style="padding-left: 0">United States</option>
<option style="padding-left: 20px">Hawaii</option>
<option style="padding-left: 40px">Kauai</option>
<option style="padding-left: 20px">Washington</option>
<option style="padding-left: 40px">Seattle</option>
<option style="padding-left: 40px">Chelan</option>
</select>
I want to achieve consistent indentation across browsers without using CSS hacks.
The rendering of SELECT elements is largely up to the browser, you have very little influence over their presentation. Some browsers obviously allow you more customization than others, IE happens to allow very little (gasp, who'd have thunk ;)). If you need very custom SELECT elements, you'll need to employ JavaScript or re-create something that behaves like a SELECT but is made of a bunch of DIVs and checkboxes or something to that extend.
Having said that, I think what you're looking for are OPTGROUPs:
<select>
<optgroup label="xxx">
<option value="xxxx">xxxx</option>
....
</optgroup>
<optgroup label="yyy">
...
</optgroup>
</select>
Every browser will display them differently, but they'll be displayed in a distinctive fashion in one way or another. Note though that officially in HTML4 you can't nest OPTGROUPs.
deceze way is much better and was my first idea. As an alternative if that doesn't work is that you can use non-breaking spaces in the tag value:
<select>
<option>select me</option>
<option> me indented</option>
<option> even more indentation</option>
</select>
It's far from pretty but it might work for you if the optgroup doesn't.
Just for the sake of visitors, I feel I should share this solution I devised: http://jsfiddle.net/n9qpN/
Decorate the options with the level class
<select name="hierarchiacal">
<option class="level_1">United States</option>
<option class="level_2">Hawaii</option>
<option class="level_3">Kauai</option>
<option class="level_2">Washington</option>
<option class="level_3">Seattle</option>
<option class="level_3">Chelan</option>
</select>
We can now use jQuery to reformat the content of the select element
$(document).ready(
function(){
$('.level_2').each(
function(){
$(this).text('----'+$(this).text());
}
);
$('.level_3').each(
function(){
$(this).text('---------'+$(this).text());
}
);
}
);
This can be extended to any level
Try using  
<select name="Something">
<option>United States</option>
<option> Hawaii</option>
<option>  Kauai</option>
<option> Washington</option>
<option>  Seattle</option>
<option>  Chelan</option>
</select>
Isn't this method of grouping creating more problems than it solves? As a user, which of those am I supposed to choose? Is there any benefit to choosing something more specific than country?
If the issue is that you only have one database field to store them in, why not have three separate select boxes (making 2 or 3 optional) and just store the most specific?:
<select name="country">
<option>Choose a country</option>
<option>United States</option>
</select>
<select name="state">
<option>Choose a state</option>
<option>Hawaii</option>
</select>
<select name="city">
<option>Choose a city</option>
<option>Kauai</option>
</select>
I was able to accomplish this using the NO-BREAK SPACE unicode character. http://www.fileformat.info/info/unicode/char/00a0/index.htm
Copy-paste the character from that page into code and voila:
https://jsfiddle.net/fwillerup/r9ch988h/
( didn't work for me because I was using a library for fancy select boxes that would inject them verbatim.)
Prepending Non breaking space (&nbsp) did not work for me.
I prepended the following:
String.fromCharCode(8194);