【问题标题】:Place an image inside a text field在文本字段中放置图像
【发布时间】:2014-01-11 11:51:15
【问题描述】:

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

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

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

【问题讨论】:

标签: html css image


【解决方案1】:

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-color:#fff; display:block; width: 100%; box-sizing: border-box }
.fake-input img { position: absolute; top: 2px; right: 5px }

工作演示

http://jsfiddle.net/HnJZa/

【讨论】:

  • 谢谢,这很有帮助
【解决方案2】:

上面的答案没有复制提问者图片中显示的格式,也没有提供任何解释。这是使用背景图片的解决方案。

背景图片语法说明。

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">

【讨论】:

    【解决方案3】:

    .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">

    【讨论】:

      【解决方案4】:

      试试这个:

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

      【讨论】:

        【解决方案5】:

        使用 background-image 和 background-position 属性

        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;
        
        }
        

        【讨论】:

          【解决方案6】:

          试试:

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

          【讨论】:

            【解决方案7】:

            一个小答案:您可以通过设置为输入字段的背景图像来做到这一点。

            【讨论】:

              【解决方案8】:

              你需要像这样添加类:

              CSS:

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

              HTML:

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

              【讨论】:

                【解决方案9】:

                您可以在输入中添加类

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

                然后将背景图像添加到 CSS 中

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

                【讨论】:

                  【解决方案10】:

                  你可以试试这样的

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

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

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

                  【讨论】:

                    猜你喜欢
                    • 1970-01-01
                    • 2014-09-24
                    • 2018-02-04
                    • 2020-05-03
                    • 1970-01-01
                    • 1970-01-01
                    • 2021-01-01
                    • 1970-01-01
                    相关资源
                    最近更新 更多