I have an input field which requires specific gradient borders on focus. So, I've created this kind of border as background color on the block and smaller background on :after element. It works at the simple block, but doesn't work on :focus.
.test:focus:after
and .test:after:focus
are not working.
Is there any solution?
Here is the sample to see what am I talking about http://codepen.io/anon/pen/Ajiwk
Thank you!
The :focus selector is used to select the element that has focus. Tip: The :focus selector is allowed on elements that accept keyboard events or other user inputs. The numbers in the table specifies the first browser version that fully supports the selector. When an <input type="text"> gets focus, gradually change the width from 100px to 250px:
From all 24 input focus effectI am going to share one example with you to acknowlage you a quick view or idea about it. HTML Code: <div class="col-3 input-effect"> <input class="effect-1" type="text" placeholder="" /> <label>First Name</label> </div>
The :focus-within pseudo selector in CSS is a bit unusual, although well-named and rather intuitive. It selects an element if that element contains any children that have :focus. form:focus-within { background: lightyellow; }.
Note that example uses :focus-within on the entire form and on interior input-wrapping <div> s. Any way that a child element can become focused will trigger :focus-within. For example, if an element has a tab-index or contenteditable attribute, then it is a focusable element, and will work.
It worked for me when focus is present in input field.
.date-of-birth:focus-within::after {
position: absolute;
color: #b5d0ee;
font-weight: 600;
left: 40%;
top: 14px;
content: attr(placeholder);
pointer-events: none;
opacity: 0.5;
z-index: 1;
}
I realize this is old, but I found this question when looking for my own and the solution listed didn't work for me, though it did put me on track for what did work.
The solution I used is similar, but a bit different.
HTML
<span class="after-holder"><input type="text" class="edit long" /></span>
CSS
.after-holder:focus-within::after {
content: 'content';
}
You can't do this for elements that can't have HTML content. See this example.
One way to do this is by adding an element that can have HTML content, like span
, and applying the ::after
pseudo-element (or in CSS 2.1, the :after
pseudo-selector) to that.
An example using your code is here.
A generic example:
<textarea>Text</textarea><span></span>
textarea + span::after{
content:"Hello world!";
}
textarea:focus + span::after{
content:"Goodbye, world!";
}
↪ See this example on JSFiddle
↪ More information about the +
selector in CSS
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With