【问题标题】:How to add icon inside the input field with the placeholder text如何使用占位符文本在输入字段内添加图标
【发布时间】:2021-03-17 17:18:40
【问题描述】:

如何在输入字段中添加图标作为占位符文本? 该图标位于输入字段之外。我尝试使用填充和边距移动它,但我似乎无法将它准确定位在我想要的位置。

【问题讨论】:

标签: html css


【解决方案1】:

您最好的选择是使用 CSS Grid 或 Flexbox 将图标置于包装元素的中心。

您可以在此处阅读有关 Grid 的更多信息:
https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Grid_Layout

您可以在此处阅读有关 Flexbox 的更多信息:
https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Flexible_Box_Layout

这是一个使用 Grid 的示例:

body {
  background: #F5F5F6;
}

label {
  display: grid;
  grid-template: 1fr / auto 1fr;
  gap: 12px;
  
  
  border: 1px solid #CFD5DB;
  border-radius: 5px;
  background: #fafafa;
  padding: 12px;
  color: #6C757D;
  cursor: text;
}

label:focus-within {
  border: 1px solid #000;
}

label>input {
  outline: none;
  border: none;
  background: transparent;
}
<label>
  <svg viewBox="0 0 24 24" width="24" height="24" stroke="currentColor" stroke-width="2" fill="none" stroke-linecap="round" stroke-linejoin="round" class="css-i6dzq1">
    <circle cx="11" cy="11" r="8"></circle>
    <line x1="21" y1="21" x2="16.65" y2="16.65"></line>
  </svg>
  <input placeholder="Enter Hotel name" type="search" />
</label>

关于此示例需要注意的几个重要事项:

  • 我使用了label 元素来包装input。这为我们提供了单击图标以聚焦输入字段的行为,而无需使用 javascript。
  • 我使用了input[type=search],因为它在语义上更适合您的用法,并且有助于屏幕阅读器用户理解文本字段的用途。
  • label 的子 input 获得焦点时,我使用了 :focus-within 伪选择器来定位和设置其样式。
  • 我已选择使用 svg 图标,但基于 img 的图标也可以使用。
  • 我本可以轻松地使用 Flexbox,但我想在包装 label 上使用 gap 属性,而不是在图标或输入上设置边距。 gap 也适用于 Flexbox,其方式与适用于 Grid 的方式相同,只是 Safari 仅在 Grid 布局上支持 gap。如果您选择 Flexbox 方法,请在其中一个子元素上使用边距,而不是 gap

【讨论】:

    【解决方案2】:

    如果您使用的是 bootstrap4/bootstrap5,您可以轻松完成

    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" rel="stylesheet"/>
     <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
      <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
      <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
     <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.7.0/css/all.css" integrity="sha384-lZN37f5QGtY3VHgisS14W3ExzMWZxybE1SJSEsQp9S+oqd12jhcu+A56Ebc1zFSJ" crossorigin="anonymous">
        
    <div class="container">  
      <div class="input-group">
        <div class="input-group-prepend">
            <div class="input-group-text">
               <i class="fas fa-search"></i>
             </div>
        </div>
        <input type="text" placeholder="search here" class="form-control" name="search" />
      </div>
    </div>

    检查一下

    【讨论】:

    • 单击图标不会聚焦输入字段。这会让用户感到沮丧。
    • 这取决于您如何使用它 可能有很多情况假设您希望某些功能应在单击图标上运行,那么您的答案不是正确的选择,因为在这种情况下图标也应用作按钮在大多数情况下,它仅用于指示显示该输入字段的真正含义@JustinTaddei
    • 为了解决您关于交互性的第一点,如果单击图标应该执行一些操作,如果输入为空,最好让图标聚焦输入,并在当用户输入了一些东西。至于您的第二点,如果图标纯粹是为了指示,那么如果单击它,它应该绝对聚焦输入。许多人都期待这种行为(尤其是根据我的经验,年龄较大的观众),缺乏这种行为会导致认知摩擦。
    • 是的,只需在输入字段内使用焦点类即可轻松实现,但这里的问题是在输入字段内显示图标,以便我们可以根据需要添加功能@JustinTaddei
    【解决方案3】:

    将两个内联控件(imageinput)放入容器(例如 Div)中。

    容器的样式必须是position:relative

    将内联元素的样式设置为position:absolute; top:0; left:0,这会将它们放在另一个之上。然后使用z-index 来决定哪个元素在上面,哪个在下面。

    然后进行一点填充,以便将输入文本推到右侧,为图像腾出空间……这样就可以了。

    border-radius 不是必需的 - 它只会使图像的角变圆。

    .iwi{
      position:relative;
    }
    
    img,input{position:absolute;top:0;left:0;}
    
    input{
      z-index:1;
      font-size:1.3rem;
      padding: 3px 5px;
      padding-left:25px;
    }
    img{
      z-index:2;
      margin-top:7px;
      margin-left:3px;
      border-radius: 20px;
    }
    ::placeholder{
      color: #ddd;
    }
    <div class="iwi">
      <img src="https://placekitten.com/20/20" />
      <input type="text" placeholder="Name the kitten" />
    </div>

    【讨论】:

    • 单击图标不会聚焦输入字段。
    【解决方案4】:
    <!DOCTYPE html>
    <html>
    <head>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- Add icon library -->
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
    <style>
    body {font-family: Arial, Helvetica, sans-serif;}
    * {box-sizing: border-box;}
    
    .input-container {
      display: -ms-flexbox; /* IE10 */
      display: flex;
      width: 100%;
      margin-bottom: 15px;
    }
    
    .icon {
      padding: 10px;
      background: dodgerblue;
      color: white;
      min-width: 50px;
      text-align: center;
    }
    
    .input-field {
      width: 100%;
      padding: 10px;
      outline: none;
    }
    
    .input-field:focus {
      border: 2px solid dodgerblue;
    }
    
    /* Set a style for the submit button */
    .btn {
      background-color: dodgerblue;
      color: white;
      padding: 15px 20px;
      border: none;
      cursor: pointer;
      width: 100%;
      opacity: 0.9;
    }
    
    .btn:hover {
      opacity: 1;
    }
    </style>
    </head>
    <body>
    
    <form action="/action_page.php" style="max-width:500px;margin:auto">
      <h2>Register Form</h2>
      <div class="input-container">
        <i class="fa fa-user icon"></i>
        <input class="input-field" type="text" placeholder="Username" name="usrnm">
      </div>
    
      <div class="input-container">
        <i class="fa fa-envelope icon"></i>
        <input class="input-field" type="text" placeholder="Email" name="email">
      </div>
      
      <div class="input-container">
        <i class="fa fa-key icon"></i>
        <input class="input-field" type="password" placeholder="Password" name="psw">
      </div>
    
      <button type="submit" class="btn">Register</button>
    </form>
    
    </body>
    </html>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-07-17
      • 1970-01-01
      • 2015-07-01
      • 1970-01-01
      • 2016-05-30
      • 1970-01-01
      • 2020-11-11
      • 2019-03-14
      相关资源
      最近更新 更多