Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to show a text field upon hovering a button with just pure CSS?

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. :/

like image 660
user12109321358 Avatar asked Sep 01 '16 05:09

user12109321358


People also ask

How do I show hover messages in HTML?

HTML: Use a container element (like <div>) and add the "tooltip" class to it. When the user mouse over this <div>, it will show the tooltip text. The tooltip text is placed inside an inline element (like <span>) with class="tooltiptext" .

Which adds hover effect on a button?

So, such Hover Button animation effect can be easily generated by using HTML and CSS. By using HTML we will design the basic structure of the button and then by using the properties of CSS, we can create the hover animation effect.


1 Answers

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

like image 109
Hitesh Misro Avatar answered Oct 05 '22 16:10

Hitesh Misro