How to get font awesome icon into input type text [duplicate]

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-11 01:38:30

问题


I have trying for font awesome icon into text input type.

But can get idea and it's not working.

It will be like in below image.

.search_location{
    padding: 9px 10px 8px 10px;
    background: #fff;
    color: #777777;
    position: relative;
}
i.icon-map-marker 
{ 
  position: absolute; 
  top: 10px; 
  left: 50px;
}
<p>
 <input type="text" class="search_location" value="Choose Location">
<i class="icon-map-marker"></i>
</p>

回答1:


To place an element with position: absolute usually we need to add position: relative on parent element. As from Documentation:

The absolutely positioned element is positioned relative to nearest positioned ancestor (non-static). If a positioned ancestor doesn't exist, the initial container is used.

I would suggest you to use a bit modified HTML as shown below:

<div class="input-holder">
  <input type="text" class="search_location" value="Choose Location">
  <i class="fa fa-map-marker icon"></i>
</div>

With some following changes in css:

* {box-sizing: border-box;}

.input-holder {
  position: relative;
  width: 300px;
}

.search_location {
  display: block;
  width: 100%;
}

* {box-sizing: border-box;}
.input-holder {
  position: relative;
  width: 300px;
}
.search_location {
    padding: 9px 10px 8px 10px;
    background: #fff;
    display: block;
    color: #777;
    width: 100%;
}
.icon { 
  position: absolute;
  right: 10px;
  top: 10px;
}
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet"/>
<div class="input-holder">
  <input type="text" class="search_location" value="Choose Location">
  <i class="fa fa-map-marker icon"></i>
</div>



回答2:


You could try this :

    p{
      position: relative;
      font-family: 'FontAwesome';
    }
    p::after{
        position: relative;
        left: -20px;
        content: "\f002";
    }
    input{
      padding-right: 20px;
    }
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet"/>
<p>
     <input type="text" class="search_location" value="Choose Location">
    </p>

Where content is your icon unicode. You'll have to specify the font-family as well so it uses font awesome (unicodes are found on their icon page).

https://jsfiddle.net/h6877894/2/



来源:https://stackoverflow.com/questions/41566192/how-to-get-font-awesome-icon-into-input-type-text

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!