将图标放在表单中的input元素内

问题描述 投票:232回答:13

如何在表单的输入元素中放置一个图标?

现场版:Tidal Force theme

css forms input icons element
13个回答
356
投票

您链接的网站使用CSS技巧组合来解决此问题。首先,它使用背景图像作为<input>元素。然后,为了将光标推过,它使用padding-left

换句话说,他们有这两个CSS规则:

background: url(images/comment-author.gif) no-repeat scroll 7px 7px;
padding-left:30px;

4
投票

我有这样的情况。由于background: #ebebeb;,它没有用。我想在输入字段上添加背景,并且该属性不断显示在背景图像的顶部,我无法看到图像!所以,我把background财产移到了background-image财产之上并且它运作了。

input[type='text'] {
    border: 0;
    background-image: url('../img/search.png');
    background-position: 9px 20px;
    background-repeat: no-repeat;
    text-align: center;
    padding: 14px;
    background: #ebebeb;
}

我的案例的解决方案是:

input[type='text'] {
    border: 0;
    background: #ebebeb;
    background-image: url('../img/search.png');
    background-position: 9px 20px;
    background-repeat: no-repeat;
    text-align: center;
    padding: 14px;
}

请注意,borderpaddingtext-align属性对解决方案并不重要。我只是复制了原始代码。


4
投票

你可以试试这个:Bootstrap-4 Beta https://www.codeply.com/go/W25zyByhec

<div class="container">
            <form>
                <div class="row">
                    <div class="input-group mb-3 col-sm-6">
                      <input type="text" class="form-control border-right-0" placeholder="Username" aria-label="Username" aria-describedby="basic-addon1">
                        <div class="input-group-prepend bg-white">
                            <span class="input-group-text border-left-0 rounded-right bg-white" id="basic-addon1"><i class="fas fa-search"></i></span>
                        </div>
                    </div>
                </div>
            </form>
        </div>


2
投票

在输入中定位Icon的简单方法是使用位置CSS属性,如下面的代码所示。注意:为了清晰起见,我简化了代码。

  1. 创建围绕输入和图标的容器。
  2. 将容器位置设置为相对位置
  3. 将图标设置为绝对位置。这将相对于周围的容器定位图标。
  4. 使用顶部,左侧,底部,右侧将图标放置在容器中。
  5. 在输入内设置填充,以使文本不与图标重叠。

#input-container {
  position: relative;
}

#input-container > img {
  position: absolute;
  top: 12px;
  left: 15px;
}

#input-container > input {
  padding-left: 40px;
}
<div id="input-container">
  <img/>
  <input/>
</div>

1
投票
 <label for="fileEdit">
    <i class="fa fa-cloud-upload">
    </i>
    <input id="fileEdit" class="hidden" type="file" name="addImg" ng-file-change="onImageChange( $files )" ng-multiple="false" accept="{{ contentType }}"/>
  </label>

例如,您可以使用:带有隐藏输入的标签(图标存在)。


0
投票

在开始时使用此css类作为输入,然后相应地进行自定义:

    
 .inp-icon{
background: url(https://i.imgur.com/kSROoEB.png)no-repeat 100%;
background-size: 16px;
 }
<input class="inp-icon" type="text">

46
投票

其他人发布的CSS解决方案是实现这一目标的最佳方式。

如果这应该给你任何问题(阅读IE6),你也可以在div内使用无边框输入。

<div style="border: 1px solid #DDD;">
    <img src="icon.png"/>
    <input style="border: none;"/>
</div>

不是“干净”,但应该适用于旧浏览器。


31
投票

你可以试试这个:

input[type='text'] {
    background-image: url(images/comment-author.gif);
    background-position: 7px 7px;
    background-repeat: no-repeat;
}

26
投票

没有背景图像的解决方案:

#input_container {
    position:relative;
    padding:0 0 0 20px;
    margin:0 20px;
    background:#ddd;
    direction: rtl;
    width: 200px;
}
#input {
    height:20px;
    margin:0;
    padding-right: 30px;
    width: 100%;
}
#input_img {
    position:absolute;
    bottom:2px;
    right:5px;
    width:24px;
    height:24px;
}
<div id="input_container">
    <input type="text" id="input" value>
    <img src="https://cdn4.iconfinder.com/data/icons/36-slim-icons/87/calender.png" id="input_img">
</div>

25
投票

我发现这是最好,最干净的解决方案。在input元素上使用text-indent

CSS:

#icon{
background-image:url(../images/icons/dollar.png); 
background-repeat: no-repeat; 
background-position: 2px 3px;
}

HTML:

<input id="icon" style="text-indent:17px;" type="text" placeholder="Username" />

8
投票
.icon{
background: url(1.jpg) no-repeat;
padding-left:25px;
}

将以上标记添加到CSS文件中并使用指定的类。


5
投票

这对我有用:

input.valid {
   border-color: #28a745;
   padding-right: 30px;
   background-image: url('https://www.stephenwadechryslerdodgejeep.com/wp-content/plugins/pm-motors-plugin/modules/vehicle_save/images/check.png');
   background-repeat: no-repeat;
   background-size: 20px 20px;
   background-position: right center;
}
<form>
<label for="name">Name</label>
<input class="valid" type="text" name="name" />
</form>

4
投票

只需在CSS中使用background属性即可。

<input id="foo" type="text" />

#foo
{
    background: url(/img/foo.png);
}

4
投票

使用font-icon

<input name="foo" type="text" placeholder="&#61447;">

要么

<input id="foo" type="text" />

#foo::before
{
  font-family: 'FontAwesome';
  color:red;
  position: relative;
  left: -5px;
  content: "\f007";    
}
© www.soinside.com 2019 - 2024. All rights reserved.