Hide Empty Optgroups - css

I have some dynamically created select menus, and sometimes there were some optgroups without any options inside.
I hid them with a line of CSS code. It worked in Chrome at the time (2016), but revisiting the website I noticed it's no longer working.
optgroup:empty{display:none}
Is there a way I can tell if it was deprecated? How should I go about hiding optgroup labels if they do not have any options inside?

Both the <optgroup> tag and the :empty CSS pseudo-class are supported in all major browsers, and your code will indeed hide empty groups:
optgroup:empty {
display: none
}
<select>
<optgroup label="Filled">
<option value="one">One</option>
<option value="two">Two</option>
</optgroup>
<optgroup label="Empty"></optgroup>
</select>
However, note that when there's a space or newline inside of an element, it is not considered to be empty, as is reported by MDN:
The :empty CSS pseudo-class represents any element that has no children. Children can be either element nodes or text (including whitespace). Comments or processing instructions do not affect whether an element is considered empty.
As such, the :empty selector won't target it in the following example:
optgroup:empty {
display: none
}
<select>
<optgroup label="Filled">
<option value="one">One</option>
<option value="two">Two</option>
</optgroup>
<optgroup label="Empty">
</optgroup>
</select>
In short, to have your empty <optgroup> hidden, you need to make sure that there is absolutely no whitespace between your opening and closing <optgroup> tags.

Related

Hide an element inside a dropdown element of a form

I want to hide one of the dropdown options from a form I'm using. I want to keep the element in the form, as the form is doing some calculations based on user inputs and if I take out the option from the form it breaks the calculation, so I'd just like to hide it from users so it's still in the form but not visible to users.
I have inspected the area on the form and this is the code. I want to hide the word Liverpool.
<select class="iphorm-element-select iphorm_16_25 select2-hidden-accessible" name="iphorm_16_25" id="iphorm_16_25_5e32cecc9b3f6" tabindex="-1" aria-hidden="true">
<option value="Liverpool">Liverpool</option>
<option value="Shrewsbury">Shrewsbury</option>
</select>
If you are working in an environment that supports HTML5, you can use the hidden attribute on the option to hide it.
Note that in order for that to work, it can't be the first option (otherwise it will default to that one).
See this working snippet:
<select class="iphorm-element-select iphorm_16_25 select2-hidden-accessible" name="iphorm_16_25" id="iphorm_16_25_5e32cecc9b3f6" tabindex="-1" aria-hidden="true">
<option value="-">Choose an option</option>
<option value="Liverpool" hidden>Liverpool</option>
<option value="Shrewsbury">Shrewsbury</option>
</select>
Following OP Comment About Editing Existing HTML
OP has stated:
The issue I will probably face is that the html is generated by a
plugin that creates the form, so I can't add that attribute. I was
hoping to add a css rule to stop it displaying
Given this circumstance, the CSS property visibility could be used to control whether the option is visible, as per the following snippet:
select > option:nth-of-type(2) {
visibility: hidden;
}
<select class="iphorm-element-select iphorm_16_25 select2-hidden-accessible" name="iphorm_16_25" id="iphorm_16_25_5e32cecc9b3f6" tabindex="-1" aria-hidden="true">
<option value="-">Choose an option</option>
<option value="Liverpool">Liverpool</option>
<option value="Shrewsbury">Shrewsbury</option>
</select>
However, this has the unfortunate side-effect of leaving a hole in the select list as opposed to hiding it completely. It does, however, prevent that option from being displayed or chosen.

CSS - Placing content of a child element in the parent

I'm trying to create a print stylesheet for a web form that users fill out by hand and then copy to the web version. Unfortunately, I can't figure out how to print my select list options. It only prints whichever option is currently selected.
Is it possible with CSS (JavaScript isn't an option) to extract the content from the select options and place them in a parent element? I'm thinking something like this:
HTML:
<div id="parentDiv">
<select>
<option value="Option 1">Option 1</option>
<option value="Option 2">Option 2</option>
<option value="Option 3">Option 3</option>
</select>
</div>
and CSS:
#media print{
#parentDiv:before {
content: child.attr(value);
}
}
That's obviously not correct, but is there a way to actually do this? Or any other JavaScript-free way to print out the options?
NOTE: The page is dynamically rendered, and I am not able to render additional "hidden" <div> elements to unhide in the print css, so that solution won't work for me.
THANKS!

Assign height property to option tag

I need to set a bigger height for the option tags inside my select.
<select name="forms" id="forms">
<option value="1">Row 1</option>
<option value="1">Row 1</option>
<option value="1">Row 1</option>
</select>
I used the CSS code below for this purpose, but it did not work.
option {
padding-top:5px;
padding-bottom:5px;
}
Also I have tested this one, and again, anything happened.
option {
height:20px;
}
What is your suggestions ?
The implementation of select often uses built-in routines that are immune to CSS. This is why both methods fail on many browsers. On Firefox, they both work. The same seems to apply to any indirect methods one might try (line-height, font-size).
I could set a certain height for it using a style for the select tag:
<select name="" style="height:25px">

Why won't padding-left in select box options show in Chrome but works in FF?

I added a css class to options in a select dropdown box for indention,
<select id="users" name="users">
<option value="[1,2,3]">Devs team</option>
<option value="1" class="member">user1</option>
<option value="2" class="member">user2</option>
<option value="3" class="member">user3</option>
</select>
It shows correctly on Firefox but not on Chrome. :(
Hope you guys could help me with this little problem.
You cannot reliably style select and option elements. Browsers typically like to stick to the OS defaults for these elements.
If you are looking to pad .member because they are children of the Devs Team, you may want to look at the OPTGROUP tag: http://www.w3schools.com/tags/tag_optgroup.asp
Found the answer. Sadly chrome doesn't support giving styles to option.

ASP.NET: How to enter a Tab character in entry of listbox?

I have a dropdown list box which has a few itens as coded below in an aspx file:
<asp:ListItem Value="1"> Chairs</asp:ListItem>
As you can see, I tried to put a few blank characters before the word Chairs BUT they are removed when Chairs is displayed in the dropdown list box.
How do I force the indentation of Chairs with spaces or with a tab character in a aspx file? \t for TAB doesn't work. Thanks.
<asp:ListItem Value="1"> Chairs</asp:ListItem>
Try the for the spaces which is the html representation of a space.
How about adding CSS styles to indent the text? You can set the text-indent: 10px CSS style on the <option> tag. Mind you, it won't work in IE, which sucks pretty bad.
Also don't forget the <optgroup> tag, which you can use to group elements in a dropdown. It's a very overlooked tag, probably because the ASP.NET list and dropdown controls doesn't support it.
Try this in your page, and see what happens:
<select>
<optgroup label="Group 1">
<option value="1">Item 1</option>
<option value="2">Item 2</option>
</optgroup>
<optgroup label="Group X">
<option value="1">Item A</option>
<option value="2">Item B</option>
</optgroup>
</select>
:)

Resources