CSS如何在输入框的末尾有一个图像

Posted

技术标签:

【中文标题】CSS如何在输入框的末尾有一个图像【英文标题】:CSS how to have an image at the end of the input box 【发布时间】:2021-01-15 21:35:07 【问题描述】:

我有一个输入框,用于搜索。 在盒子的末端,而不是外面,在右边的盒子末端我想要一个图像,它也应该像一个按钮

如何使用 css 实现这一点。在此先感谢,我对 css 很陌生。 以下是我的代码:


<div class="group1">
<input type="text" class="form1" id="Search" placeholder="PO Quick Search..."> -- **SearchBox**
<button class="btn" type="button" id="btnSearch" onclick=""> -- **Button next to search box, which is wanted inside**
</button>
</div>

图片和按钮应该是这样的,最后的图片应该是可点击的

【问题讨论】:

这能回答你的问题吗? html button with search icon? 【参考方案1】:

您可以使用background-imagebackground-position 来执行此操作,检查我的 sn-p 以了解我到底做了什么。 我认为你也可以对位置::after::before 做同样的事情。

input 
  display:block;
  margin: 5px 5px 5px 5px;


#login-form 
  margin: 10px 0px 0px 20px;


#password-input 
  background: url(data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wCEAAkGBxQTEhQUExQVFhQWFBQYGBgYFBQWFxsXFxQWFxQXFxQYHCggHCUlHBQUITEiJSksLi4uFx8zODMsNygtLisBCgoKBQUFDgUFDisZExkrKysrKysrKysrKysrKysrKysrKysrKysrKysrKysrKysrKysrKysrKysrKysrKysrK//AABEIAOEA4QMBIgACEQEDEQH/xAAcAAEAAwADAQEAAAAAAAAAAAAAAQcIAgQGAwX/xABNEAABAgMDCAcFAwkFBwUAAAABAAIRITEDBGEFIjJBUXGx8AYHEkKBweEIE3KRoRQjcxUkM2KCkqOz8TRSdJPRFyVEg6Ky01NUY2TS/8QAFAEBAAAAAAAAAAAAAAAAAAAAAP/EABQRAQAAAAAAAAAAAAAAAAAAAAD/2gAMAwEAAhEDEQA/ALuJJOCgviYDxPkj4mQ8T/ooJ7rf6eqCXPnAV14I5+oTPMyoMs1teZlNGQm48xKCXPhKpKOdATmSo0Zmbj9UpN1eZBBPagImvMggdARdJQBDO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/ovO9MOt652Fi8XS0F4vLmwaWh3u2E0c5xECBXsiJJhGEYrN9dyYBBL3EkzJJMSTUnWSVGATAIgJTelN6U3oFN6ImJQMSmJTEpVBZvs9uP5UfAf8La7tOyWj9HFx+qzh7Pj/wDej5f8LawH/Mslo/RmZuPMAgaMzNx5gEa2E3V4YBAIZzq8MAjR3neA2IPpFEiiDg90JCp5iVGji4/VcnSxJXFrYTMzzIIGjMzJ5gFU/tC5Cfa3OxvIETYWhDwNVnawEfBzWD9pWw1vedXhuXyvV0basey2aHMe1zXMMwWuECCMQUGKMAlFY/WD1VXi5vda3Zj7a6kkjsjtWlmI6L2iZH6w2Tgq4igU3pTeglvQICYlBtKb0DEpXclUruQK7kruSPyQnUEDAJgEJ1BIoCU3pTelN6BTeiBBtKBiUxKb0QKpXckYr2nQfq3veUHNd2HWN2qbZzYRGyzaZvJ2iQ1naHtfZ0yMQ+8XxzSGhosLP9ZxIfaQ3dmz/eV5gQznV4YBdHIOR7K6WFnY2TeyyzbBoriSTrJJJJ1krvNb3neA2IAHed4DZ6o0RmZDUPMoGxMTQUHmVIBJw44lBziiRRAREQChREErNPXr/bB48VKIKxREQEREBERBKhEQEREBERAREQEREHsuqn+3sWrxqREElCiIBQoiCEREH//Z) no-repeat right top;
  background-size: 15px 15px;
  padding-right: 15px;
<form id="login-form">
  <input id="password-input" type="password"> 
</form>

【讨论】:

谢谢,这就是我要找的东西!!!如果单击图像时它应该像按钮一样怎么办?在这种情况下如何实现? @user2294434 欢迎您,先生,如果您只需将图标放在按钮标签中,这是可能的。查看此代码以获取更多信息:jsfiddle.net/fs39bxvm/2 如果有用,请接受我作为答案:)

以上是关于CSS如何在输入框的末尾有一个图像的主要内容,如果未能解决你的问题,请参考以下文章

如何在 Access 中快速滚动并到达组合框的末尾

css如何选中那个按钮,并把它放到输入框的右下角

在文本框的任何文本的末尾设置光标[重复]

如何设置输入文件文本框的样式? [复制]

将图像作为输入框的背景

jquery如何判断多个输入框是不是有值,在线等啊~~~~~~~~~~~~~~~