Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to combine input:after:focus pure css?

Tags:

css

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!

like image 901
user3310932 Avatar asked Feb 14 '14 16:02

user3310932


People also ask

How to use the focus selector in HTML?

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:

How many input focus effects are there in HTML?

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>

What does the CSS pseudo selector focus-within mean?

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; }.

How do you use focus within a form?

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.


Video Answer


3 Answers

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;
}
like image 155
JPawelHeaven Avatar answered Oct 13 '22 21:10

JPawelHeaven


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';
    }
like image 44
Bel Avatar answered Oct 13 '22 22:10

Bel


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:

HTML:

<textarea>Text</textarea><span></span>

CSS

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

like image 13
user2428118 Avatar answered Oct 13 '22 21:10

user2428118