将图像放在文本字段中

时间:2013-12-23 09:26:37

标签: html css image

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

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

图片中是文本字段电子邮件地址内的图片,这就是我想要的。你是怎么做到的?

10 个答案:

答案 0 :(得分:15)

<强> 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/

答案 1 :(得分:11)

试试这个:

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

答案 2 :(得分:4)

你可以尝试这样的事情

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

然后将此样式应用于文本框:

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

答案 3 :(得分:3)

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

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;

}

答案 4 :(得分:1)

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

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

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

答案 5 :(得分:0)

您可以在输入中添加课程

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

然后将背景图像添加到css中的类

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

答案 6 :(得分:0)

你需要添加这样的类:

<强> CSS:

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

<强> HTML:

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

答案 7 :(得分:0)

一个小答案:您可以通过设置为输入字段的背景图像来完成此操作。

答案 8 :(得分:0)

尝试:

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

答案 9 :(得分:0)

上述答案并未复制提问者图片中显示的格式,或提供任何解释。这是使用背景图像的解决方案。

背景图像语法说明。

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

&#13;
&#13;
.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;
}
&#13;
<input name="exampleEmail" class="bg-email-icon" placeholder="Email" type="email">
<input name="examplePassword" class="bg-lock-icon" placeholder="Password" type="password">
&#13;
&#13;
&#13;