Grow your CSS skills. Land your dream job.

Last updated on:

Highlight Related Label when Input in Focus

$("form :input").focus(function() {
  $("label[for='" + + "']").addClass("labelfocus");
}).blur(function() {


  1. asheep
    Permalink to comment#

    pleasee give me this example :(

  2. This will come in handy, will use a subtle rgba colour background on selection – cheers :-)

  3. JohnMotyJr
    Permalink to comment#

    I ran a test case with this because i was doing this the long way. Come to find out, if you happen to already know the labels for="" and input’s id, then a switch case is quicker.

    $('form :input').bind({
        focus: function () {
            var id = $(this).attr('id').toString();
            var add = $("label[for='" + + "']").addClass('labelfocus');
            switch (id) {
                case 'title':add;break;
                case 'description':add;break;
                case 'resource':add;break;
                case 'tag':add;break;
                case 'theme':add;break;
                case 'language':add;break;
        blur: function () {

    jsPerf Benchmark Test Case (Slightly modified with triggering events). I do however prefer this method due to the less code and easy to use, especially if you have dynamic forms.

  4. You can style it with just CSS if you put the label after the input

    <input id="something-cool" type="text" />
    <label for="something-cool">Something Cool</label>
    input:focus + label { position: relative; top: -20px; }
  5. Here is very good example It realised like small jQuery plugin and can be customized as you wish.

Leave a Comment

Posting Code

Markdown is supported in the comment area, so you can write inline code in backticks like `this` or multiline blocks of code in in triple backtick fences like ```this```. You don't need to escape code in backticks, Markdown does that for you.

Sadly, it's kind of broken. WordPress only accepts a subset of HTML in comments, which makes sense, because certainly some HTML can't be allowed, like <script> tags. But this stripping happens before the comment is processed by Markdown (via Jetpack). It seems to me that would be reversed, because after Markdown processes code in backticks, it's escaped, thus safe. If you think you can fix this issue, get in touch!

If you need to make sure the code (typically HTML) you post absolutely posts correctly, escape it and put it within <pre><code> tags.

Current ye@r *

*May or may not contain any actual "CSS" or "Tricks".