9

Is there a way to only use pure CSS code to show a text field when you hover a button?

<form id="demo">
    <input type="text" id="search-field" name="s" placeholder="Search">
    <input type="submit" id="search-icon">
    <i class="fa fa-search"></i>
</form>

The text field should be hidden and only shows up when you hover the button. I'm not sure it can be done through CSS only. :/

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
user12109321358
  • 421
  • 3
  • 9
  • 18
  • which field would you like to show on hover? – Roysh Sep 01 '16 at 05:22
  • 3
    This looks like a usability nightmare. In the provided answers, you can't even click on the text field (though you can tab to it). Where are all these upvotes coming from? – thirtydot Sep 01 '16 at 09:35

2 Answers2

10

Try this code:

#search-field {
    display: none;
}
#btn:hover + #search-field {
    display: inline-block;
}
<form id="demo">
    <input type="button" value="Hover" id="btn" />
    <input type="text" id="search-field" name="s" placeholder="Search" />
    <input type="submit" id="search-icon" />
    <i class="fa fa-search"></i>
</form>

Updated

Swap your inputs in the HTML and rearrange your elements using the order property and apply display:flex; to your container in CSS. That would solve it.

#search-field {
    visibility: hidden;
}
#search-icon:hover + #search-field {
    visibility: visible;
}
#demo {
    display: flex;
}
input:nth-child(2) {
    order: -1;
}
<form id="demo">
    <input type="submit" id="search-icon" />
    <input type="text" id="search-field" name="s" placeholder="Search" />
    <i class="fa fa-search"></i>
</form>

Check: https://stackoverflow.com/a/36118012/5336321

Community
  • 1
  • 1
Hitesh Misro
  • 3,397
  • 1
  • 21
  • 50
2

You can swap the order of input[type="text"] and input[type="submit"] and then you can style input[type="text"] on hover of button as shown below.

#demo {
  position: relative;
  padding-top: 30px;
}
#search-field {
  transition: opacity 0.25s linear;
  position: absolute;
  opacity: 0;
  left: 0;
  top: 0;
}

#search-icon:hover + #search-field {
  opacity: 1;
}
<form id="demo">
  <input type="submit" id="search-icon" />
  <input type="text" id="search-field" name="s" placeholder="Search" />
  <i class="fa fa-search"></i>
</form>
Mohammad Usman
  • 37,952
  • 20
  • 92
  • 95