1

For example if I have lots of this:

<div class="form-group">
  <div class="col-xs-2"><label for="USER_FORM___unique_id" class="control-label">Employee ID</label>  </div>  
  <div class="col-xs-5"><input disabled="disabled" type="text" class="form-control" id="USER_FORM___unique_id__orig" value="20141100497">  </div>  
  <div class="col-xs-5"><input type="text" class="form-control" id="USER_FORM___unique_id" value="20141100497">  </div> 
</div>
<div class="form-group">
  <div class="col-xs-2"><label for="USER_FORM___name" class="control-label">Name</label>  </div>  
  <div class="col-xs-5"><input disabled="disabled" type="text" class="form-control" id="USER_FORM___name__orig" value="20141100497">  </div>  
  <div class="col-xs-5"><input type="text" class="form-control" id="USER_FORM___name" value="Me me me me">  </div> 
</div>
<!-- and so on -->
  • I want to add border on the parent div.form-group, only when the last inner input.col-xs-5 focused/hovered

  • or highlighting the label.control-label only when the for attribute it refer to focused/hoevered

Is it possible to use CSS only to do this?

I know that this is possible using jQuery, by adding onfocus="highlight(this)", onblur="highlight(this)", onmouseenter="highlight(this)", and onmouseleave="highlight(this)" on the last input:

function highlight(el) {
  el = $(el);
  var focused = el.is(":focus");
  var style1 = focused ? '1px solid blue' : '';
  $(el).parent().css('border',style1); // 1
  var style2 = focused ? 'underline' : '';
  $(el).prev().prev().css('text-decoration',style2); // 2
}
Kokizzu
  • 24,974
  • 37
  • 137
  • 233
  • 3
    _Is it possible to use CSS only to do this?_ - No, CSS does not have a parent/previous selector. – Weafs.py Jan 22 '15 at 09:23
  • 1
    Just about to say the same thing, not a bad question but there are a ton of these around the site. Surprised you didn't find that answer before writing this question. – Ruddy Jan 22 '15 at 09:24
  • 1
    Quite a shame that we can't do this in 2015 though... – Brewal Jan 22 '15 at 09:27

1 Answers1

2

[This is a hack, but is works]

If you can change your markup, then you can add a div after the input.form-control and simulate the parent styling to that div instead of the parent div.form-group. Example:

.form-group {
    position: relative;
    border:1px solid blue;
    display:inline-block;
    padding:20px;
}
.form-control:focus + div {
    position: absolute;
    top:0;
    bottom:0;
    left:0;
    right:0;
    border:1px solid red;
}
<div class="form-group">
    <div class="col-xs-2"><label for="USER_FORM___name" class="control-label">Name</label></div>
    <div class="col-xs-5"><input disabled="disabled" type="text" class="form-control" id="USER_FORM___name__orig" value="20141100497" /></div>
    <div class="col-xs-5">
        <input type="text" class="form-control" id="USER_FORM___name" value="Me me me me" />
        <div></div> <!-- additional markup -->
    </div>
</div>
Vucko
  • 20,555
  • 10
  • 56
  • 107