Ways to combine an icon with an input field

I am attempting to achieve the following:

  • Creating 3 input elements in a row
  • Each should have an icon to the left of it, perfectly centered.
  • Each should also have a border-bottom that extends to include the icon.

Similar to the image shown below:

https://i.sstatic.net/GPWdk.png

However, with my current code, I am facing issues centering the icons and extending the border properly. Here is my code snippet:

input {
  border: none;
  width: 250px;
  background-color: #393d49;
  border-bottom: 1px solid #767D93;
  padding: 10px;
}

form img {
  width: 24px;
  height: 24px;
}
<form>
      <img src="assets/images/envelope.png" alt="Envelope icon indicating user's E-Mail.">
      <input type="email" placeholder="E-Mail"><br>

      <img src="assets/images/locked.png" alt="Lock icon indicating user's Password.">
      <input type="password" placeholder="Password"><br>
      
      <img src="assets/images/avatar.png" alt="Avatar icon indicating user's Name.">      
      <input type="text" placeholder="Username"><br>
    </form>

Answer №1

Instead of relying on external libraries, I would recommend creating custom CSS styles. If you're not comfortable with that idea, here is a basic example of how you can style your form without any external resources.

form, .form-row, input {
    background-color: #051024;
}

.input-icon, label, input {
  display: inline-block;
}

form {
  padding: 0.8em 1.2em;
}

.form-row {
  padding: 0.8em 0;
  padding-bottom: 0.2em;
}

.form-row:not(:last-child) {
  border-bottom: solid #18273a 1px; /* Only the last row has a border */
}

.input-icon {
  width: 15px;
  height: 15px;
  margin: 0 10px;
}

label {
  max-width:4em; /* Or the maximum width you want your lebel to be */
  min-width:4em; /* Same */
  color:white;
  font-weight: 100;
}

input {
   border:none;
   padding: 0.8em 0.5em;
   color: #6691c9;
   font-size: 15px;
   outline: none; /* No glowing borders on chrome */
}
<form>
    <div class="form-row">
        <img class="input-icon" src="http://1.bp.blogspot.com/-QTgDeozeWws/VLztRSNkMEI/AAAAAAAAKkQ/mrxdCfxWfvU/s1600/1f499.png" alt="oops"/>
        <label for="form-email">Email</label>
        <input id="form-email" type="email">
    </div>

    <div class="form-row">
        <img class="input-icon" src="http://1.bp.blogspot.com/-QTgDeozeWws/VLztRSNkMEI/AAAAAAAAKkQ/mrxdCfxWfvU/s1600/1f499.png" alt="oops"/>
        <label for="form-password">Password</label>
        <input id="form-password"type="password" placeholder="(8 characters min)">
    </div>
      
    <div class="form-row">
        <img class="input-icon" src="http://1.bp.blogspot.com/-QTgDeozeWws/VLztRSNkMEI/AAAAAAAAKkQ/mrxdCfxWfvU/s1600/1f499.png" alt="oops"/>
        <label for="form-user">User</label>   
        <input id="form-user" type="text"><br>
    </div>
</form>

If you're feeling adventurous

Take a leap and experiment with bootstrap. It offers a wide range of tools to enhance your web design (including the font-awesome library).

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Groupings of CSS style declarations

I am currently learning CSS and I have a question that may seem basic or silly, but I can't seem to find the answer anywhere. Here is the CSS code I am working with: #table-of-contents ol { list-style-type: none; counter-reset: item; margin: 0; ...

Using CSS to position an element relative/absolute within text inline

Need help aligning caret icons next to dynamically populated text in a navbar menu with dropdown tabs at any viewport size. Referring to positioning similar to the green carets shown here: https://i.stack.imgur.com/4XM7x.png Check out the code snippet bel ...

What steps do I need to take to successfully get this JavaScript Timer working?

I'm currently working on developing a timer using JavaScript. However, I've encountered an issue where the timer does not stop once it reaches zero. I've attempted to use return and if statements without success. Is my approach correct, or i ...

How can I transfer a JavaScript value to PHP for storage in an SQL database?

<script> var latitudeVal = document.getElementById("latitude"); var longitudeVal = document.getElementById("longitude"); function getLocation() { if (navigator.geolocation) { navigator.geolocation.getCurrentPosition(showPosition); ...

Guide to creating a fixed accordion header in Bootstrap 4

Looking for a way to make the header of a BS4 accordion sticky when it is opened, so that it stays at the top of the screen even when users scroll down Current approach is not achieving the desired result: #accordion .card-header BUTTON:not(.collapsed) { ...

What is the best way to align the navbar elements horizontally and at an equal height?

I am currently working on creating a navigation bar for my webpage using bootstrap. I have utilized the <li> tags, but I am facing an issue where the items are not lining up properly. Below is the code for reference: <nav class="navbar navbar ...

Setting the margin to 0 auto to create a CSS Grid layout featuring an Image taking up the entire right side

I’ve been grappling with finding a consistent method to ensure uniform white space for all containers like this: https://i.stack.imgur.com/wtk0G.png Important: The grey rectangle signifies an image. My goal is for it to extend to the viewport's en ...

How to align the navbar toggle button and list items to the right in Bootstrap 5

I'm struggling with a simple html page that has a fixed navbar at the top. Everything looks great when viewed in full-screen mode, with centered links. However, when the page size is reduced, it collapses to a toggle button on the left side. What I re ...

What is preventing BODY from respecting min-height: 100% ?

Struggling to create a basic webpage with a container that extends to the bottom? Here's what I'm aiming for: #Main should be as tall as the viewport, but able to stretch further with additional content. body should match the viewport height, y ...

Incorporating Angular 2 Templates: Exploring how to bind keydown.arrow event to the entire div rather than just specific

I am currently working on a simple navigator component that includes buttons and a date-picker subcomponent. My goal is to be able to bind keydown.arrowleft etc. for the entire div, which has a CSS style of 100% height and width. Below is the template stru ...

Retrieve a PDF file from an Asp.net MVC controller

What would be the most efficient method to create an HTML page from data within a view? I already have an HTML template containing all necessary tables and elements. My preference is not to use any templating options such as JqueryTemplate. ...

Can the CSS be used to conceal the PNG image while still preserving the drop shadow effect?

I recently applied the "Filter: drop-shadow" effect to my png photo and it worked perfectly. However, I am now interested in hiding the actual png image while retaining the shadow effect. My initial attempt involved adjusting the translate and drop-shadow ...

Generate real-time dynamic line charts using data pulled directly from a MySQL database

I am in search of guidance on developing a real-time line chart that can dynamically update based on data fetched from MySQL. I need an example or reference on how to achieve this functionality without having to refresh the webpage every time new data is ...

Relative fluid icon within the parent element

Looking at my JSFiddle example, I have two buttons with icons that currently have fixed width and height set. How can I make these icon sizes relative to the parent element using percentages, such as 80%? Current fixed width and height of icons: .gc_foot ...

Unable to shorten the visible content in the dropdown

When a user selects text from the dropdown menu, I want to remove any extra spaces. However, I am having trouble implementing this feature. /* $("#input_13").find("option").each(function (index, option) { $(option).html($(option).html().replace ...

CSS hover effect: altering background colors of both parent and child elements simultaneously

HTML: <div class="container"> <div class="parent">one <div class="child">two</div> </div> </div> CSS: .parent { background: red; height: 200px; width: 200px; } .child { back ...

Tips for displaying content within a directive in different locations

I am looking for a way to create a component directive that will render the content placed inside its selector at specific sections within its HTML structure. The sections I want the content to be rendered in are header, footer, main. This is the example ...

Adjust the height of videos automatically on columns

I need help figuring out how to display two videos with different dimensions side by side without any weird gaps. I've already tried using flex display with no luck. My current code looks like this: <div class="row"> <d ...

Creating a Transparent Scrollbar in React for Google Chrome

::-webkit-scrollbar { width: 0.5vw; } ::-webkit-scrollbar-track { background-color: transparent; } ::-webkit-scrollbar-thumb { background-color: rgb(99, 99, 99); border-radius: 10px; } I wrote this code to style a scrollbar in CSS. The scrollba ...

The sup tag does not function properly when used within the title tag

I added a sup tag inside a title tag but it doesn't seem to work. Surprisingly, the same sup tag works perfectly fine within li tags. Can anyone explain why this is happening? I tried changing the title tags to H class tags but I'm still curious ...