how to give width of ListBox in HTML5 - html

I want to give ListBox width in HTML5.
Please give me some idea.

You can style it as a regular input - all CSS properties applicable to it
DEMO
CSS
input[list] {
width: 300px;
}
HTML
<input type="text" list="browsers" />
<datalist id="browsers">
<option> Chrome </option>
<option> Firefox </option>
<option> IE9 </option>
</datalist>

You can try with
style="width:150px;"

CSS
#example {
width: 300px;
}
HTML
<select size="4" id="example" name="example">
<option selected>Fries</option>
<option>Drink</option>
<option>Pie</option>
<option>Salad</option>
<option>Shake</option>
</select>
For completeness and to show an actual HTML5 Listbox (no JS needed).

Related

A select element that is not visible

I use bootstrap and try to display a select box. But the select box is not visible.
HTML:
<div class="textAlignCenter">
<select class="selectpicker" placeholder="Situation maritale" formControlName="maritalSituation">
<option value="" selected></option>
<option value="MARIE">Marié</option>
<option value="DIVORCE">Divorcé</option>
<option value="CELIBATAIRE">Célibataire</option>
<option value="VEUF">Veuf</option>
</select>
</div>
CSS:
.textAlignCenter {
text-align: center;
border: 1px solid red;
}
Here is the fiddle: https://jsfiddle.net/flamant/awrb4Lxz/3/
Select elements don't work like that. To display a "placeholder", you can replace your first <option> by something like this:
<option value="" selected disabled>Situation maritale</option>
It was my browser that didn't work. I upgraded firefox with the latest version and restart and it worked. Could you please remove the negative mark please ? Thank you. The post is closed

Style <select> inside <form>

I have some code like this:
<form>
<select name="slctr__lvl1">
<option selected>aaa</option>
<option value="1">1</option>
</select>
</form>
I tried doing the CSS in these ways:
[name="slctr__lvl1"] {
width: 150px;
}
and:
select {
width: 150px;
}
and:
font select {
width: 150px;
}
but none worked. I want it to look like I wrote:
<form>
<select name="slctr__lvl1" style="width: 150px">
<option selected>aaa</option>
<option value="1">1</option>
</select>
</form>
but the CSS seems to have no effect.
You can use CSS Attribute Selectors:
The [attribute="value"] selector is used to select elements with a specified attribute and value.
Try as the following:
form select[name="slctr__lvl1"] {
width: 150px;
}
<form>
<select name="slctr__lvl1">
<option selected>aaa</option>
<option value="1">1</option>
</select>
</form>
You can also read further on MDN's Attribute selectors documentation.
CSS attribute selector needs HTML tag before attribute
CSS Attribute selector
https://www.w3schools.com/css/css_attribute_selectors.asp
CSS all selector types
https://www.w3schools.com/cssref/css_selectors.asp
have a look to the following example, I tried to make it work as you did, and if you compare my example with yours you will have the difference that why your code is not working
select[name=slctr__lvl1] {
background-color: yellow;
}
select{
color: red;
}
select {
width: 200px;
}
<select name="slctr__lvl1">
<option selected>aaa</option>
<option value="1">1</option>
</select>
PS: I didn't understand what you want to achieve with this.
font select {
width: 150px;
}
I think this is just a typo, You want to add form, not the font I think
Give the select box a class:
.selectBox{
width: 400px;
}
<form>
<select class="selectBox" name="slctr__lvl1">
<option selected>aaa</option>
<option value="1">1</option>
</select>
</form>

Mysterious extra left padding on input[type="search"]

Anyone know what's causing this extra left 'padding' on the input? The inspector isn't reporting that anything is actually causing it. Can't say I've run into this issue before…
The top field is a text input (search), the bottom field is a select
Apparently the HTML code is the key to solving the problem, here you go:
<input type="search" placeholder="Search" />
<select>
<option value="ir35">IR35</option>
</select>
Edit to add:
I was a bit of a doofus, but the problem still remains. In experimenting with different input types I'd created two templates. The isse is actually on an input[type="search"] - NOT text. This is an issue with the search type. However I am already applying -webkit-appearance: textfield is there something else I'm missing? There's no actualy 'thing' causing the padding, text indent, padding, etc. It's just 'there'.
Edit:
My solution will be dropping search in favour of text - it's a workaround but it'll do.
select elements and input elements operate, I believe, under different box-models.
box-sizing:border-box is the default on select elements.
You can check this by looking in the "Computed Styles" when using Developer Tools
Setting box-sizing:border-box on both should fix it.
DEMO Without
select,
input {
display: block;
width: 200px;
}
<select name='options'>
<option value='option-1'>Option 1</option>
<option value='option-2'>Option 2</option>
<option value='option-3'>Option 3</option>
</select>
<input type="text" />
DEMO With
select, input {
display: block;
width: 200px;
box-sizing:border-box;
}
<select name='options'>
<option value='option-1'>Option 1</option>
<option value='option-2'>Option 2</option>
<option value='option-3'>Option 3</option>
</select>
<input type="text" />
The styling of certain form elements like input and select is not specified in the CSS specification and is left up to the discretion of the browser developer, so there will be slight style variations across browsers.
In Firefox, you can make some hard-coded style adjustments to fix the padding issue that you are seeing.
For example, if you add padding-left: 3px to the input field, then the text value will line up with the option value displayed in the select box.
However, since padding cannot be negative, you can eliminate the left padding in the select element, though you can in the option element.
select,
input {
display: block;
width: 200px;
box-sizing: border-box;
}
select {
padding-left: 0px;
}
input {
padding-left: 3px;
}
<input type="text" value="Option 1" />
<select name='options'>
<option value='option-1'>Option 1</option>
<option value='option-2'>Option 2</option>
<option value='option-3'>Option 3</option>
</select>

How can I make one <option> tag display in a different colour when the <select> is both open and closed?

I have the following HTML:
<select name="selectedEventTypeId">
<option value="1">meeting</option>
<option value="2" selected="" class="ololool">write contract</option>
</select>
And this CSS:
<style type="text/css">
.ololool {color:green}
</style>
However, the option doesn't display in green when the <select> is closed:
It only does when it's open:
I want the second <option> to be green in both views, open and closed. (But I don't want the first option to be displayed in green)
How can I make so?
Try this code
<style type="text/css">
.ololool {color:green}
.blacktext{color:black; }
</style>
<select name="selectedEventTypeId" onchange="this.className=this.options[this.selectedIndex].className" class="blacktext">
<option value="1" class="blacktext">meeting</option>
<option value="2" class="ololool">write contract</option>
</select>
I cant understand very well your question.
But i think that you need to add that class .ololool also to to make it all with the same style.
<select class="ololool">
<option value="2" selected class="ololool" >write contract</option>
</select>

Style Select And Multiple Select?

How can I style both the <select> HTML tag, and the <select multiple="multiple"> HTML tag while both of the tags reside under a <form> tag? Of course styling within CSS. If someone knows how to accomplish this, may someone give me an example?
Thank you!
Aaron
You can use classes fairly easily:
.singleSelect {
width: 200px;
}
.singleMultiple {
width: 300px;
}
<form>
<select class="singleSelect">
<option>Test</option>
</select>'
<br/>
<select class="singleMultiple" multiple="multiple">
<option>Test 1</option>
<option>Test 2</option>
<option>Test 3</option>
</select>
</form>
http://jsfiddle.net/GyxL4/
A more advanced selector may be used on the select[multiple], but beware, legacy browsers (EDIT: apparently only IE6) may not always support the attribute selector, and in the first example below, you're styling every SELECT element on the page (this is called an element selector):
select {
width: 200px;
}
select[multiple] {
width: 300px;
}
See: http://www.w3.org/TR/CSS2/selector.html