4

I want to have a tooltip on input:focus so what I have:

Code:

.containerTooltipXxx{
    padding: 20px; 
    position: relative;
  float: left;
    display: inline-block;
    border: 2px solid lime;
    margin: 50px;
}

.hovTol1{display: none;}

.containerTooltipXxx:hover > .hovTol1{
    display: block;
    position: absolute;
    top:-5px;
    left: 50px;
    background: red;
}
<div class="containerTooltipXxx">
    <p class="hovTol1">Tooltip description is here</p>
    <div class="blocks">
      <label>Field</label>  <input></input>
    </div>
</div>

This works great when hover over containerTooltipXxx div but I need to show that tooltip when input is focused so I've tried something like:

.containerTooltipXxx input:focus> .hovTol1{
    display: block;
    position: absolute;
    top:-5px;
    left: 50px;
    background: red;
}

but is not working so if someone can help me with this, Thank you.

fiddle

4
  • any harm in using js ? Commented Jul 2, 2013 at 7:39
  • @Let's Code No need for js :) is very simple but I don't find the perfect combination :) Commented Jul 2, 2013 at 7:42
  • jquerytools.org/demos/tooltip/form.html Commented Jul 2, 2013 at 7:43
  • @falguni that tooltip js is buggy because he create a div with that tooltip but not delete it, however I don't need to use a js for that :) Commented Jul 2, 2013 at 7:49

1 Answer 1

4

I would place the tooltip after the input field (in the html) and then use the adjacent selector (+):

.containerTooltipXxx input:focus + .hovTol1 {}

.containerTooltipXxx{
    padding: 20px; 
    position: relative;
    float: left;
    display: inline-block;
    border: 2px solid lime;
    margin: 50px;
}

.hovTol1{display: none;}

.containerTooltipXxx input:focus + .hovTol1{
    display: block;
    position: absolute;
    top:-5px;
    left: 50px;
    background: red;
}
<div class="containerTooltipXxx">
  <div class="blocks">
    <label>Field</label>  <input></input>
    <p class="hovTol1">Tooltip description is here</p>
  </div>
</div>

<!-- hover section with green border to see the tooltip-->

Sign up to request clarification or add additional context in comments.

2 Comments

it is possible to do that without changing html hierarchy ? in my local code I cannot insert that paragraph inside div :(
unfortunately, you need to insert the paragraph somewhere after the input field, so that they are siblings. If you would use an editable div instead of an input element, you could also put the tooltip into this div or use an :after or :before pseudo-object for the tootip, but before and after again unfortunately can not be used with input objects. So unless you can adjust the html a little, you will not be able to style the tooltip according to the focused input field only with CSS.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.