select not adding pseudo after selector - html

I want to add a font-awesome arrow to my select but the :after selector does not appear to be being applied:
The focus is not being applied either for some reason.
select {
border: 1px solid #727272;
-webkit-appearance: none;
border-radius: 0;
width: 50%;
padding: 10px;
}
.select:after {
content: "\F0DA";
font-family: FontAwesome;
font-size: 20px;
color: black;
}
select:focus: {
outline: 3px solid #ffbf47 !important;
outline-offset: 0;
}
<div>
<select class="select">
<option>One</option>
<option>Two</option>
</select>
</div>

As i said in comment you can not use pseudo on select instead wrap your select with a div then use pseudo on it.
select {
border: 1px solid #727272;
-webkit-appearance: none;
border-radius: 0;
width: 100%;
padding: 10px;
}
.select-option {
position: relative;
width: 50%;
}
.select-option::after {
content: "\F0DA";
font-family: FontAwesome;
font-size: 20px;
color: black;
position: absolute;
right: 10px;
top: 7px;
}
select:focus {
outline: 3px solid #ffbf47 !important;
outline-offset: 0;
}
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" integrity="sha384-wvfXpqpZZVQGK6TAh5PVlGOfQNHSoD2xbE+QkPxCAFlNEevoEH3Sl0sibVcOQVnN" crossorigin="anonymous">
<div class="select-option">
<select class="select">
<option>One</option>
<option>Two</option>
</select>
</div>
If you want to use this arrow instead of default select option arrow on your select you need set position: absolute for ::after element.
.select-option::after {
content: "\F0DA";
font-family: FontAwesome;
font-size: 20px;
color: black;
position: absolute;
right: 10px;
top: 7px;
}
Don't forget to set position: relative on parent .select-option and i change your code a little for move width: 50% to parent and set width: 100% on select.
And note that, correct is ::after not :after. CSS3 introduced the ::after notation (with two colons) to distinguish pseudo-classes from pseudo-elements. Browsers also accept :after, introduced in CSS2.
More info here.
And last word is, this is a common issue that you can't set ::after or ::before on select option possible there are many topics with this title, google, stackoverflow and etc.. anyway.

Related

How do i style <option> tag without JavaScript?

Is there any easy way to style <option> tag after <select> without javascript? i tried to add class or another tag in it.
<option><span> </span><option> OR
<option><lable class="padding"> </lable><option>
it doesn't show any tags. i'm trying to put some padding for option text. Please suggest.
Without javascript if not be impossible else is very hard , i create a custom select element, I hope help you.
$(document).ready(function(){
$('.sel>span').click(function(){
$('.sub').toggle();
})
$('.sub div').click(function(){
$('.sel .content').text($(this).text());
$('.sub').hide();
})
})
.sel {
display: inline-block;
width: 200px;
height: 20px;
border: 1px solid #ccc;
position: relative;
padding-left: 40px;
box-sizing: border-box;
}
.sel > .arrow{
position: absolute;
right: 0;
cursor: pointer;
outline: none;
}
.sub {
position: absolute;
border: 1px solid #ccc;
display: inline-block;
width: 200px;
left: -1px;
top: 19px;
box-sizing: border-box;
display: none;
cursor: pointer;
}
.content {
color: orange;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<div class="sel">
<span class="arrow">▾</span>
<span class="content"></span>
<div class="sub">
<div> </div>
<div>Orange</div>
<div>Blue</div>
<div>Red</div>
</div>
</div>
Building your own dropdown is easier than trying to style the existing one.
Take a look at Bootstrap or these links:
https://semantic-ui.com/modules/dropdown.html#/examples
https://tympanus.net/Tutorials/CustomDropDownListStyling/index3.html
Or use Google for that matter

CSS - Triangle border not rendering correctly IE8

I have a horizontal nav that has triangle borders inserted :before and :after a list item to make it appear as though there is a white bordered point on the right side of the list item. It's rendering in FF and Chrome but not IE8, and I'm using the html5 doctype and have included the html5.js.
It appears as though the list item background-color is stacked on top of the arrow border so it can't be seen because if I turn the background-color off I can see the arrow. I thought it might be a z-index problem but that doesn't help either. I also notice in IE that all of the :after styles are crossed out, any ideas?
<html>
<head>
<script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<style type="text/css">
ul.registration-nav {
float: left;
margin-bottom: 45px;
width: 100%;
}
ul.registration-nav li {
background-color: #afafaf;
color: #FFFFFF;
display: block;
float: left;
font-weight: 600;
padding: 10px 0;
position: relative;
text-align: center;
text-decoration: none;
width: 33%;
}
ul.registration-nav li.arrow:after {
content: " ";
display: block;
width: 0;
height: 0;
border-top: 20px solid transparent;
border-bottom: 20px solid transparent;
border-left: 15px solid #afafaf;
position: absolute;
top: 50%;
margin-top: -20px;
left: 100%;
z-index: 2;
}
ul.registration-nav li.arrow:before {
content: " ";
display: block;
width: 0;
height: 0;
border-top: 24px solid transparent;
border-bottom: 24px solid transparent;
border-left: 18px solid white;
position: absolute;
top: 50%;
margin-top: -24px;
margin-left: 1px;
left: 100%;
z-index: 1;
}
ul.registration-nav li.checkout {
background-color: #cecece;
}
ul.registration-nav li.active {
background-color: #067673;
}
ul.registration-nav li.active:after {
border-left: 15px solid #067673;
}
</style>
</head>
<body>
<ul class="nav inline registration-nav capitalize">
<li class="register arrow active">Register</li>
<li class="books arrow">Books</li>
<li class="checkout">Checkout</li>
</ul>
</body>
</html>
FYI - Chris Coyier has an article on the after pseudo-selectors here
Browser Support
Little issues:
Firefox 3.5- wouldn't allow absolute positioning of pseudo elements.
In Opera 9.2, whitespace is always displayed within this
pseudo-element as if it’s pre text.
IE 8 doesn't support z-index on them
So this very well could have been your problem.

Pseudo element stopping event click

I am customizing select box by using css :after Attribute.
Html:
<div class="styled">
<select>
<option>India, Rupees (Rs.)</option>
<option>USA, Dollars ($) </option>
<option>United Kingdom, Pounds (£)</option>
</select>
</div>
css:
div.styled{
display: inline-block;
float: left;
position: relative;
border-right: 1px solid #CCC;
border-radius: 3px;
box-shadow: 1px 1px 1px #ddd;
}
.styled select option{
padding: 5px;
}
div.styled:after{
content: "";
background: #fff url("dn_arw.png") no-repeat 2px 4px;
display: inline-block;
float: left;
height: 18px;
position: absolute;
right: 8px;
top: 6px;
width: 15px;
}
The below is the image and js fiddle
http://jsfiddle.net/CtYnt/
The Problem is the area which is overlapped with "down arrow image" is not clickable
To make it work in Chrome, Firefox and Safari, you can use one line of CSS in the :after block:
pointer-events: none;
IE takes more work, the easiest way is to use conditional statement for IE on the :after block and set:
display:none;
Alternatively if your overlayed DIV MUST work in IE, there is a hack detailed here.

Cannot Get Hover to Work over a Wrapper Div

I need your help.
I can't seem to be able to get the hover working properly over a wrapper/container DIV
CSS:
#myhover:hover {
border: 1px solid red;
}
HTML:
<div id="myhover" style="background: #ffffff;
width: 177px; height: 20px; border: 1px solid #808080;">
<div style="float: left;">
<input id="refdocs" style="padding-left: 4px; padding-right: 3px;
height: 15px; width: 158px; border: none;" type="text">
</div>
<div style="line-height:18px; font-size: 11pt; color: #779297;">+</div>
</div>
Because inline styles are the most specific in the cascade, they can over-ride things you didn't intend them to. Move your #myhover styles into a style sheet then it should work fine.
for example:
#myhover {
background: #ffffff;
width: 177px;
height: 20px;
border: 1px solid #808080;
}
jsfiddle: http://jsfiddle.net/va8Bz/
Your style attribute is overriding your stylesheet selectors. It's more specific.
You have three options here:
Move your styles out of the style attribute.
Move your styles out of the style attribute.
Add !important to the style declarations that should override the ones in your style attribute.
I suggest you move your styles out of the style attribute into a stylesheet.
Example: http://jsfiddle.net/Y7NW9/
You need to stick all your CSS in a stylesheet instead of using inline styles.
What you're trying to accomplish can be done with less markup too:
<div class="container">
<input class="refdocs" type="text">
<div class="icon">+</div>
</div>
Then in your CSS stylesheet:
.container {
display: inline-block;
position: relative;
}
.input-wrapper {
float: left;
}
.refdocs {
border: 1px solid #808080;
padding: 2px;
padding-right: 14px;
margin: 0;
}
.refdocs:hover {
border: 1px solid red;
}
.icon {
font-size: 11pt;
position: absolute;
top: 3px;
right: 5px;
}
Here's a working demo
I solved this for you in your previous question:
#add {
float: right;
width: 20px;
height: 20px;
cursor: pointer;
text-align: center;
}
#add:hover {
color: #f00;
}
http://jsfiddle.net/kUSBM/
Please respond to the questions or accept answers.

problem with <select> and :after with CSS in WebKit

I would like to add some style on a select box with the pseudo :after (to style my select box with 2 parts and without images). Here's the HTML:
<select name="">
<option value="">Test</option>
</select>
And it doesn't work. I don't know why and I didn't find the answer in the W3C specs. Here's the CSS:
select {
-webkit-appearance: none;
background: black;
border: none;
border-radius: 0;
color: white;
}
select:after {
content: " ";
display: inline-block;
width: 24px; height: 24px;
background: blue;
}
So is it normal or is there a trick?
I haven't checked this extensively, but I'm under the impression that this isn't (yet?) possible, due to the way in which select elements are generated by the OS on which the browser runs, rather than the browser itself.
I was looking for the same thing since the background of my select is the same as the arrow color. As previously mentioned, it is impossible yet to add anything using :before or :after on a select element. My solution was to create a wrapper element on which I added the following :before code.
.select-wrapper {
position: relative;
}
.select-wrapper:before {
content: '\f0d7';
font-family: FontAwesome;
color: #fff;
display: inline-block;
position: absolute;
right: 20px;
top: 15px;
pointer-events: none;
}
And this my select
select {
box-sizing: border-box;
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
width: 100%;
padding: 10px 20px;
background: #000;
color: #fff;
border: none;
-webkit-appearance: none;
-moz-appearance: none;
appearance: none;
}
select::-ms-expand {
display: none;
}
I have used FontAwesome.io for my new arrow, but you can use whatever else you want. Obviously this is not a perfect solution, but depending on your needs it might be enough.
To my experience it simply does not work, unless you are willing to wrap your <select> in some wrapper. But what you can do instead is to use background image SVG. E.g.
.archive .options select.opt {
-moz-appearance: none;
-webkit-appearance: none;
padding-right: 1.25EM;
appearance: none;
position: relative;
background-color: transparent;
background-image: url("data:image/svg+xml;charset=utf8,%3Csvg xmlns='http://www.w3.org/2000/svg' version='1.1' height='10px' width='15px'%3E%3Ctext x='0' y='10' fill='gray'%3E%E2%96%BE%3C/text%3E%3C/svg%3E");
background-repeat: no-repeat;
background-size: 1.5EM 1EM;
background-position: right center;
background-clip: border-box;
-moz-background-clip: border-box;
-webkit-background-clip: border-box;
}
.archive .options select.opt::-ms-expand {
display: none;
}
Just be careful with proper URL-encoding because of IE. You must use charset=utf8 (not just utf8), don't use double-quotes (") to delimit SVG attribute values, use apostrophes (') instead to simplify your life. URL-encode s (%3E). In case you havee to print any non-ASCII characters you have to obtain their UTF-8 representation (e.g. BabelMap can help you with that) and then provide that representation in URL-encoded form - e.g. for ▾ (U+25BE BLACK DOWN-POINTING SMALL TRIANGLE) UTF-8 representation is \xE2\x96\xBE which is %E2%96%BE when URL-encoded.
What if modifying the markup isn't an option?
Here's a solution that has no requirements for a wrapper: it uses an SVG in a background-image.
You may need to use an HTML entity decoder to understand how to change the fill colour.
-moz-appearance: none;
-webkit-appearance: none;
appearance: none;
background-image: url('data:image/svg+xml;charset=US-ASCII,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20width%3D%22292.4%22%20height%3D%22292.4%22%3E%3Cpath%20fill%3D%22%23000000%22%20d%3D%22M287%2069.4a17.6%2017.6%200%200%200-13-5.4H18.4c-5%200-9.3%201.8-12.9%205.4A17.6%2017.6%200%200%200%200%2082.2c0%205%201.8%209.3%205.4%2012.9l128%20127.9c3.6%203.6%207.8%205.4%2012.8%205.4s9.2-1.8%2012.8-5.4L287%2095c3.5-3.5%205.4-7.8%205.4-12.8%200-5-1.9-9.2-5.5-12.8z%22%2F%3E%3C%2Fsvg%3E');
background-repeat: no-repeat;
background-position: right .7em top 50%;
background-size: .65em auto;
Pinched from CSS-Tricks.
Faced the same problem. Probably it could be a solution:
<select id="select-1">
<option>One</option>
<option>Two</option>
<option>Three</option>
</select>
<label for="select-1"></label>
#select-1 {
...
}
#select-1 + label:after {
...
}
This post may help http://bavotasan.com/2011/style-select-box-using-only-css/
He is using a outside div with a class for resolving this issue.
<div class="styled-select">
<select>
<option>Here is the first option</option>
<option>The second option</option>
</select>
</div>
This solution is similar to the one from sroy, but with css triangle instead of web font:
.select-wrapper {
position: relative;
width: 200px;
}
.select-wrapper:after {
content: "";
width: 0;
height: 0;
border-left: 5px solid transparent;
border-right: 5px solid transparent;
border-top: 6px solid #666;
position: absolute;
right: 8px;
top: 8px;
pointer-events: none;
}
select {
background: #eee;
border: 0 !important;
border-radius: 0;
-webkit-appearance:none;
-moz-appearance:none;
appearance:none;
text-indent: 0.01px;
text-overflow: "";
font-size: inherit;
line-height: inherit;
width: 100%;
}
select::-ms-expand {
display: none;
}
<div class="select-wrapper">
<select>
<option value="1">option 1</option>
<option value="2">option 2</option>
<option value="3">option 3</option>
</select>
</div>
This is a modern solution I cooked up using font-awesome. Vendor extensions have been omitted for brevity.
HTML
<fieldset>
<label for="color">Select Color</label>
<div class="select-wrapper">
<select id="color">
<option>Red</option>
<option>Blue</option>
<option>Yellow</option>
</select>
<i class="fa fa-chevron-down"></i>
</div>
</fieldset>
SCSS
fieldset {
.select-wrapper {
position: relative;
select {
appearance: none;
position: relative;
z-index: 1;
background: transparent;
+ i {
position: absolute;
top: 40%;
right: 15px;
}
}
}
If your select element has a defined background color, then this won't work as this snippet essentially places the Chevron icon behind the select element (to allow clicking on top of the icon to still initiate the select action).
However, you can style the select-wrapper to the same size as the select element and style its background to achieve the same effect.
Check out my CodePen for a working demo that shows this bit of code on both a dark and light themed select box using a regular label and a "placeholder" label and other cleaned up styles such as borders and widths.
P.S. This is an answer I had posted to another, duplicate question earlier this year.
<div class="select">
<select name="you_are" id="dropdown" class="selection">
<option value="0" disabled selected>Select</option>
<option value="1">Student</option>
<option value="2">Full-time Job</option>
<option value="2">Part-time Job</option>
<option value="3">Job-Seeker</option>
<option value="4">Nothing Yet</option>
</select>
</div>
Insted of styling the select why dont you add a div out-side the select.
and style then in CSS
.select{
width: 100%;
height: 45px;
position: relative;
}
.select::after{
content: '\f0d7';
position: absolute;
top: 0px;
right: 10px;
font-family: 'Font Awesome 5 Free';
font-weight: 900;
color: #0b660b;
font-size: 45px;
z-index: 2;
}
#dropdown{
-webkit-appearance: button;
-moz-appearance: button;
appearance: button;
height: 45px;
width: 100%;
outline: none;
border: none;
border-bottom: 2px solid #0b660b;
font-size: 20px;
background-color: #0b660b23;
box-sizing: border-box;
padding-left: 10px;
padding-right: 10px;
}
Instead of a wrapper element you can use CSS grid and place an icon (or whatever) in the same cell as the select:
.select-field {
display: grid;
grid-template:
"label"
"select"
/ max-content;
}
.label {
grid-area: label;
}
.select {
appearance: none;
background: white;
border: 1px solid var(--border-color);
grid-area: select;
padding-block: 0.5ex;
padding-inline: 1ch calc(1ch + 1em);
}
.after {
align-self: center;
border-block-start: 0.5em solid var(--border-color);
border-inline: 0.5em solid transparent;
block-size: 0;
grid-area: select;
inline-size: 0;
justify-self: end;
margin-inline-end: 1ch;
pointer-events: none;
}
.select,
.select + .after {
--border-color: silver;
}
.select:hover,
.select:hover + .after {
--border-color: grey;
}
.select:focus,
.select:focus + .after {
--border-color: rebeccapurple;
}
<div class="select-field">
<label for="my-select" class="label">Select One</label>
<select id="my-select" class="select">
<option value="foo">Foo</option>
<option value="bar">Bar</option>
<option value="baz">Baz</option>
</select>
<div class="after"></div>
</div>
Here I used an empty div to and styled it to be a CSS triangle which has the same color as the border which changes during hover/focus.
The most important bits here are the following:
The <select> and the <div class="after"> go into the same grid-area (which I named select). This will put the empty div over the select.
Give the <select> an appearance of none. This will remove any browser default style.
Give the <select> and extra padding at the end of the inline direction to make room for the empty style.
Justify the empty div to the end.
Give the empty div an extra margin at the end of the inline direction which matches your desired padding of the <select>
Give the empty div a pointer-events of none so the click will go through it to the <select> element.
Other then that you can do whatever with the empty div. It doesn’t even have to be empty. E.g. you can put an svg icon in there if you want.
If you chose to use the select::after method, remember that:
.select::after{
...
pointer-events: none;
...
for clickable..