Style <select> inside <form> - html

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>

Related

Why is it that if you target a "Form" element in CSS, the properties apply only to the first child-element, and not all?

Here's what I mean.
<div class="drop-down">
<form action="/action-page.php">
<label for="cars">Choose a Japanese car:</label>
<select id="cars" name="cars">
<option value="Toyota">Toyota</option>
<option value="Honda">Honda</option>
<option value="Mitsubishi">Mitsubishi</option>
<option value="Suzuki">Suzuki</option>
<option value="Nissan">Nissan</option>
<option value="Subaru">Subaru</option>
<option value="Mazda">Mazda</option>
</select>
<input type="submit">
</form>
</div>
and CSS:
.drop-down {
background-color:white;
padding:20px;
display:inline-block;
padding-right:70px;
padding-left:70px;
font-size:20px;
}
Now, the font-size applies to only the first child-element, which is the label element. Why is it that it doesn't apply to the rest of the child-elements, i.e. the select and input?
I have to separately target them to change their font-size. I thought that, if I target the parent element, the changes are going to cascade down to all the texts within the parent element, yet it only cascades down to the first child element.
Why is that?
Browser has it's own stylesheet (called user agent stylesheet).
If you inspect your input and select elements, you would see that browser overrides your style, since it has higher specificity:
Simply adding
input, select{
font-size: inherit;
}
solves the problem.
It would be good idea to add css reboot like this or write your own in separate file. That would solve common problems and make styles less browser-opinionated.
You can do it by defining a separate CSS for select.
select {
font-size:20px;
}
if you want the same style for both you can add it in style like .drop-down, select {} but it will make it worse as there is padding in your style for the label. it will be applied to the options also this will make your dropdown larger.
.drop-down {
background-color:white;
padding:20px;
display:inline-block;
padding-right:70px;
padding-left:70px;
font-size:20px;
}
select {
font-size:20px;
}
<div class="drop-down">
<form action="/action-page.php">
<label for="cars">Choose a Japanese car:</label>
<select id="cars" name="cars">
<option value="Toyota">Toyota</option>
<option value="Honda">Honda</option>
<option value="Mitsubishi">Mitsubishi</option>
<option value="Suzuki">Suzuki</option>
<option value="Nissan">Nissan</option>
<option value="Subaru">Subaru</option>
<option value="Mazda">Mazda</option>
</select>
<input type="submit">
</form>
</div>

How do i get rid of the dropdown effect of the select tag

I'm trying to use css to style a select/option html element so that they display as a small list of button with the selected option being highlighted instead of the traditional dropdown menu.
I would've simply rewritten the entire thing, but i am editing a shopify theme, and there are too many implications of such change, some of them are above my "pay-grade" let's say, so i am trying to use the easy way out of just restyling it using css to get the desired result
I have done this before like this, but it doesn't work on mobile devices such as iOS.
#foo {
overflow: auto;
height: 2.4em;
border: 0;
}
#foo option {
display: inline-block;
margin: 0 .3em;
}
#foo option:checked {
font-weight: bold;
}
<select multiple="multiple" name="foo[]" id="foo" size="5">
<option value="1" selected="selected">Foo 1</option>
<option value="2">Foo 2</option>
<option value="3">Foo 3</option>
<option value="4">Foo 4</option>
<option value="5">Foo 5</option>
<option value="6">Foo 6</option>
<option value="7" selected="selected">Foo 7</option>
</select>
Sounds like <select multiple> might be closer to what you're looking for. Learn more here: https://www.w3schools.com/tags/att_select_multiple.asp

CSS & HTML: Styling the selected option (as placeholder + don't show in the dropdown)

EDIT:
I don't know why I got 2 downvotes, because still no one fixed the problem.
Problem:
I can't style the color of the selected option (that got functionality as the placeholder).
I tried this and some other silimar solutions, but couldn't find a HTML/CSS based solution.
option {
color:#999;
}
/* NOT working */
option:selected {
color:blue;
}
<select class="form-control" id="frmMaand" name="offer_request[month]">
<option value="" class="disabled-option" hidden selected>
<span style="color:#999 !important; background:#333 !important;">Select an option</span>
</option>
<option value="value1">Option 1</option>
<option value="value2">Option 2</option>
<option value="value3">Option 3</option>
<option value="value4">Option 4</option>
</select>
Note:
I am looking for a non-JavaScript/jQuery solution.
I want attributes attached to the option tag to keep the user experience optimal:
hidden selected
Edit:
I want to use the first option like a placeholder. I don't want the placeholder visible in the dropdown and the color need to be lighter than the other option elements.
Try this code, jsfiddle
option {
color:#999;
}
/* NOT working */
option:selected {
color:blue;
}
.styled-offer select.offer-select {
color:blue;
}
.styled-offer select.offer-select option {
color: #999;
}
<div class="styled-offer">
<select class="form-control offer-select" id="frmMaand" name="type">
<option value="1" class="disabled-option selected" hidden selected >
<span>Select an option</span>
</option>
<option value="value1" >Option 1</option>
<option value="value2">Option 2</option>
<option value="value3">Option 3</option>
<option value="value4">Option 4</option>
</select>
</div>
This option element of a select control cannot be styled, due to being rendered as HTML, except for the background-color and color
properties.
You can change style of select and option like following :-
if you want to style each one of the option tags.. use the css attribute selector:
select option {
margin:40px;
background: rgba(0,0,0,0.3);
color:#fff;
text-shadow:0 1px 0 rgba(0,0,0,0.4);
}
select option[val="1"]{
background: rgba(100,100,100,0.3);
}
select option[val="2"]{
background: rgba(200,200,200,0.3);
}
It may help you.
You don't need the :selected selector:
Just do tht :
#frmMaand option {
color:#999;
}

how to give width of ListBox in HTML5

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).

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