This question already has an answer here:
这个问题在这里已有答案:
- How do I add a Font Awesome icon to input field? 6 answers
- 如何在输入字段中添加Font Awesome图标? 6个答案
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>
2 个解决方案
#1
0
To place an element with position: absolute
usually we need to add position: relative
on parent element. As from Documentation:
要放置一个具有position:absolute的元素,通常我们需要在父元素上添加position:relative。来自文档:
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:
我建议你使用一点修改后的HTML,如下所示:
<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:
随着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
1
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).
内容是您的图标unicode。您还必须指定font-family,因此它使用了很棒的字体(在其图标页面上可以找到unicodes)。
https://jsfiddle.net/h6877894/2/
https://jsfiddle.net/h6877894/2/
#1
0
To place an element with position: absolute
usually we need to add position: relative
on parent element. As from Documentation:
要放置一个具有position:absolute的元素,通常我们需要在父元素上添加position:relative。来自文档:
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:
我建议你使用一点修改后的HTML,如下所示:
<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:
随着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
1
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).
内容是您的图标unicode。您还必须指定font-family,因此它使用了很棒的字体(在其图标页面上可以找到unicodes)。
https://jsfiddle.net/h6877894/2/
https://jsfiddle.net/h6877894/2/