Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS: Change parent on focus of child

Let's say you have something like:

<div class="parent">     <input class="childInput" type="text" />     <div class="sibling"></div> </div> 

I want to change the appearance of the parent/siblings when the child receives focus. Are there any CSS tricks for doing stuff like this?

Edit:

The reason for my question is as follows:

I'm creating an Angular app which needs editable text fields. It should look like a label until it is clicked, at which point it should look like a normal text input. I styled the text field based on :focus to achieve this effect, but the text is cut off by text input's boundaries. I also used ng-show, ng-hide, ng-blur, ng-keypress and ng-click to switch between the label and the text input based on blurs, key presses and clicks. This worked fine except for one thing: After the label's ng-click="setEdit(this, $event)" changes the edit boolean used by ng-show and ng-hide to true, it uses a jQuery call to .select() the text input. However, it isn't until after the completion of the ng-click that everything is $digest'd, so the text input loses focus again. Since the text input never actually receives focus, using ng-blur to revert back to showing the label is buggy: The user has to click in the text input and then click out of it again to revert back to showing the label.

Edit:

Here's an example plunk of the issue: http://plnkr.co/edit/synSIP?p=preview

like image 276
AaronF Avatar asked Jun 18 '14 13:06

AaronF


People also ask

How do you style the parent element when hovering a child element?

The trick is to give the sibling the same size and position as the parent and to style the sibling instead of the parent. This will look like the parent is styled!

How do I apply CSS from parent to child?

It's easy to apply style to a child element, but if you want to apply style to a parent class that already has child elements, you can use the CSS selector child combinators (>), which are placed between two CSS selectors. For example, div > p selects all <p> elements where the parent is a <div> element.

Can I use CSS focus-within?

The :focus-within CSS pseudo-class matches an element if the element or any of its descendants are focused. In other words, it represents an element that is itself matched by the :focus pseudo-class or has a descendant that is matched by :focus .


1 Answers

You can now do this in pure CSS, so no JavaScript needed 😁

The new CSS pseudo-class :focus-within would help for cases like this and will help with accessibility when people use tabbing for navigating, common when using screen readers.

.parent:focus-within {   border: 1px solid #000; } 

The :focus-within pseudo-class matches elements that either themselves match :focus or that have descendants which match :focus.


Can I use...

You can check which browsers support this by visiting http://caniuse.com/#search=focus-within


Demo

fieldset {   padding: 0 24px 24px !important; }  fieldset legend {   opacity: 0;   padding: 0 8px;   width: auto; }  fieldset:focus-within {   border: 1px solid #000; }  fieldset:focus-within legend {   opacity: 1; }
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet" />  <div class="container">   <form>     <fieldset>       <legend>Parent Element</legend>       <div class="form-group">         <label for="name">Name:</label>         <input class="form-control" id="name" placeholder="Enter name">       </div>       <div class="form-group">         <label for="email">Email:</label>         <input type="email" class="form-control" id="email" placeholder="Enter email">       </div>     </fieldset>   </form> </div>
like image 200
J J B Avatar answered Oct 19 '22 23:10

J J B