CSS to put icon inside an input element in a form - GeeksforGeeks

10 May, 2022
To add icon inside the input element the <i> tag and <span> tag are used widely to add icons on the webpages. To add any icons on the webpages or in some specific area, it needs the fontawesome link inside the head tag. The fontawesome icon can be placed by using the fa prefix before the icon’s name. 
fontawesome link: https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css 
Note: No downloading or installation is required. 
Example-1: 
html
<!DOCTYPE html><html> <head>    <link rel="stylesheet"          href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">    <style>        .input-icons i {            position: absolute;        }                 .input-icons {            width: 100%;            margin-bottom: 10px;        }                 .icon {            padding: 10px;            min-width: 40px;        }                 .input-field {            width: 100%;            padding: 10px;            text-align: center;        }    </style></head> <body>    <h3>      Icons inside the input element  </h3>    <div style="max-width:400px;margin:auto">        <div class="input-icons">            <i class="fa fa-user icon"></i>            <input class="input-field" type="text">            <i class="fa fa-instagram icon"></i>            <input class="input-field" type="text">            <i class="fa fa-envelope icon"></i>            <input class="input-field" type="text">            <i class="fa fa-youtube icon"></i>            <input class="input-field" type="text">            <i class="fa fa-facebook icon"></i>            <input class="input-field" type="text">        </div>  </div></body> </html>
Output:
                                            
Example-2: 
html
<!DOCTYPE html><html> <head>    <link rel="stylesheet"          href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">    <style>        .input-icons i {            position: absolute;        }                 .input-icons {            width: 100%;            margin-bottom: 10px;        }                 .icon {            padding: 10px;            color: green;            min-width: 50px;            text-align: center;        }                 .input-field {            width: 100%;            padding: 10px;            text-align: center;        }                 h2 {            color: green;        }    </style></head> <body>    <center>        <form style="max-width:450px;margin:auto">            <h2>GeeksforGeeks</h2>            <div class="input-icons">                <i class="fa fa-user icon">              </i>                <input class="input-field"                       type="text"                       placeholder="Username">            </div>             <div class="input-icons">                <i class="fa fa-envelope icon">              </i>                <input class="input-field"                       type="text"                       placeholder="Email">            </div>             <div class="input-icons">                <i class="fa fa-key icon">              </i>                <input class="input-field"                       type="password"                       placeholder="Password">            </div>        </form>    </center></body> </html>
Output:
                                                           
CSS is the foundation of webpages, is used for webpage development by styling websites and web apps. You can learn CSS from the ground up by following this CSS Tutorial and CSS Examples.
Attention reader! Don’t stop learning now. Get hold of all the important HTML concepts with the Web Design for Beginners | HTML  course.
hardikkoriintern
Picked
CSS
HTML
Web Technologies
HTML
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to apply style to parent if it has child with CSS?
How to set space between the flexbox ?
Design a web page using HTML and CSS
Create a Responsive Navbar using ReactJS
Form validation using jQuery
How to set the default value for an HTML <select> element ?
Hide or show elements in HTML using display property
How to set input type date in dd-mm-yyyy format using HTML ?
REST API (Introduction)
How to Insert Form Data into Database using PHP ?