Pedro Vieira
Pedro Vieira

Reputation: 2316

CSS focus on child element change a parent element

I want to style a form that has the label and input inside the form field and when I'll write something inside the input (probably with focus), I want the borders to light up with some blue. Now I have something like this:

HTML

<div class="login-form-field">
  <label for="email" class="login-form-label">Email:</label>
  <input class="login-form-input" autofocus="autofocus" type="email" value="" name="user[email]" id="user_email">
</div>

CSS

.login-form-input{
  margin-left: 20px;
  width: 90%;
  outline: none;
  border: none;
  -webkit-box-shadow: none;
  -moz-box-shadow: none;
  box-shadow: 0 0 0px 1000px white inset;
}
.login-form-label {
  font-size: 13px;
  font-weight: 300;
  padding-left: 20px;
}
.login-form-field{
  width: 100%;
  border-radius: 0px;
  height: 6rem;
  border: 0.5px solid grey;
  box-shadow: 0px 0px 0px;
}

I already tried to select the parent to made some change and other stuff I found on google. The closest I got was to highlight with blue when the mouser was over it with :hover, but i need the color to stay as I'm with the input selected.

.login-form-field:hover {
  border-color: blue !important;
}

Here is the JSFiddle, if anyone could help I would be grateful!

Upvotes: 5

Views: 11335

Answers (6)

Mir Adnan
Mir Adnan

Reputation: 884

Although this is an old answer. I am answering this so anyone who lands here can use just CSS to achieve this.

Use CSS3 pseudo element: focus-within

You could do:

form:focus-within {
  border-color: blue !important;
}

Upvotes: 0

J J B
J J B

Reputation: 9230

You can now do this in pure CSS so no JavaScript is 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.

.login-form-field:focus-within {
  border-color: blue !important;
}

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

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

Upvotes: 16

user1817927
user1817927

Reputation:

React with jquery:

$( document ).ready(function() {
    console.log( "ready!" );

    $('.login-form-input').focus(function() {
         $(this).parent().css( "border", "#99f 2px solid" );
    });

    $('.login-form-input').focusout(function() {
         $(this).parent().css( "border", "" );
    });

});

Upvotes: 0

Asons
Asons

Reputation: 87191

You can do like this, where you add an extra div, absolute positioned, which acts as the border, ... and no script is required.

.login-form-input {
  margin-left: 20px;
  width: 90%;
  outline: none;
}

.login-form-label {
  font-size: 13px;
  font-weight: 300;
  padding-left: 20px;
}

.login-form-field {
  position: relative;
  width: 100%;
  border-radius: 0px;
  height: 6rem;
  box-shadow: 0px 0px 0px;
}
.login-form-field input ~ .login-form-field-border {
  position: absolute;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  box-sizing: border-box;  
  border: 0.5px solid grey;
  z-index: -1
}
.login-form-field input:focus ~ .login-form-field-border {
  border: 2px solid blue;  
}
<div class="login-form-field">
  <label for="email" class="login-form-label">Email:</label>
  <input class="login-form-input" autofocus="autofocus" type="email" value="" name="user[email]" id="user_email">
  <div class="login-form-field-border"></div>
</div>

Upvotes: 2

Robert
Robert

Reputation: 6967

CSS does not have native support for parent selecting. If your goal is to have .login-form-field have a blue border on focus you're going to have to rely on JavaScript to add the respective CSS.

The following CSS:

.login-form-field.highlight {
  border-color: blue;
}

With the following jQuery

$('.login-form-field').hover(function() {
    $(this).toggleClass('highlight');
});

Would achieve that goal. I should note that jQuery is certainly not necessary here; it's just what I prefer to use.

Upvotes: 1

Dyana Putry
Dyana Putry

Reputation: 107

if you want to give the border color when the input is active you can add like this:

.login-form-input:focus {
  border:1px solid blue;
}

Upvotes: -3

Related Questions