2013-12-23 55 views
6

我有一个像这样的HTML输入字段。我想在右侧的文本框内放置一个图像。有人可以帮我用它的CSS吗?在文本字段中放置图像

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

在图片中是作为文本字段的电子邮件地址,这是我想要的东西里面的图像。你怎么做到这一点?

+0

http://stackoverflow.com/questions/14839771/how-to-put-an-icon-inside-textbox-in-jquery-mobile –

+0

http://jsfiddle.net/oscarj24/zy7YP/ 1/ – anand4tech

回答

9

HTML

<div class="fake-input"> 
    <input type="text" /> 
    <img src="http://www.zermatt-fun.ch/images/mastercard.jpg" width=25 /> 
</div> 

CSS

.fake-input { position: relative; width:240px; } 
.fake-input input { border:none: background:#fff; display:block; width: 100%; box-sizing: border-box } 
.fake-input img { position: absolute; top: 2px; right: 5px } 

工作演示

http://jsfiddle.net/HnJZa/

+0

谢谢,这是有益的 – user3128920

+0

我假设图像被取下。这里是更新:http://jsfiddle.net/1ve4s64b/ – crh225

3

,你可以尝试这样的事情

.textBox{ 
background-image:url(iconimage.jpg); 
background-position:right; 
background-repeat:no-repeat; 
padding-left:17px; 
} 

那么这种风格应用到您的文本框:

<input type="text" class="textBox" /> 
0

您可以添加类的输入

<input type="text" id="name" class="inputImage" /> 

,然后背景image to class in css

.inputImage 
{ 
background:#FFFFFF url('img.jpg') no-repeat top right; 
} 
0

您需要添加类是这样的:

CSS:

.bgInput { 
    background: url(path of your image.jpg) top right no-repeat; 
} 

HTML:

<input type="text" class="bgInput" id="name"/> 
0

小的回答:你可以通过设置为输入域的背景图像做到这一点。

0

尝试:

#name { 
     background: url('path/image.png') right no-repeat; 
} 
6

试试这个:

input { 
    background-image: url("icon.png"); 
    background-position: right top; 
    background-repeat: no-repeat; 
    background-size: contain; 
} 
2

使用背景图片和背景位置属性

DEMO

CSS:

input { 
    height: 70px; 
    width: 200px; 
    background-image:url("http://cloud.ohloh.net/attachments/25869/plone-icon-64_med.png"); 
    background-repeat:no-repeat; 
    background-position: 133px 3px; 

} 
1

.text3 { 
 
\t width: 300px; 
 
\t height: 30px; 
 
} 
 

 
#text3 { 
 
\t background-image: url(https://cdn3.iconfinder.com/data/icons/interface-8/128/InterfaceExpendet-09-128.png); 
 
\t background-size: 30px; 
 
\t background-repeat: no-repeat; 
 
} 
 

 
input { 
 
\t text-align: center; 
 
}
<p class="email"> 
 
\t \t \t Email 
 
</p> 
 
<input type="text" placeholder="Email_ID" class="text3" id="text3">

0

答案以上不复制在提问图像中示出的格式,或提供任何解释。这是一个使用背景图像的解决方案。

背景图像语法的解释。图像的

background: url('https://d1ululg65bfe3q.cloudfront.net/images/troy/email-icon.png') no-repeat 97.25% 10px white; 

.bg-email-icon { 
 
    background: url('https://d1ululg65bfe3q.cloudfront.net/images/troy/email-icon.png') no-repeat 97.25% 10px white; 
 
    padding-left: 5px; 
 
    padding-right: 50px; 
 
    width: 255px; 
 
    height: 30px; 
 
    border: 1px gray solid; 
 
    border-radius: 5px 5px 0px 0px; 
 
} 
 

 
.bg-lock-icon { 
 
    background: url('https://d1ululg65bfe3q.cloudfront.net/images/troy/lock-icon.png') no-repeat 96.75% 10px white; 
 
    padding-left: 5px; 
 
    padding-right: 50px; 
 
    width: 255px; 
 
    height: 30px; 
 
    border: 1px gray solid; 
 
    border-top-width: 0px; 
 
    border-radius: 0px 0px 5px 5px; 
 
}
<input name="exampleEmail" class="bg-email-icon" placeholder="Email" type="email"> 
 
<input name="examplePassword" class="bg-lock-icon" placeholder="Password" type="password">