如何在搜索框中添加图标

5

需要回答的问题:
如何在搜索框/表单中添加图标
如何在输入框中添加图标

我遇到了以下问题,即无法使FontAwesome fa-search图标位于输入框/搜索框的右侧。

这是表单的图像:
网站页眉上的HTML搜索表单

表单链接(非常经典):
https://www.themirrorman.co

这是我的原始代码:

@media screen and (min-width: 800px) {
  #header-search-desktop-div {
      position: absolute;
      left: 180px;
      width: 450px;
      margin-top: 0;
      margin-bottom: 0;
      border: 1px solid #881d98;
      border-radius: 24px;
  }
  #header-search-desktop-div #header-search-fa {
      position: absolute;
      color: #7E7E7E;
      z-index: 100;
      font-size: 27px;
  }
  #header-search-desktop-div .fa-search {
      top: 5%;
      left: 1.5%;
      line-height: 0.8em;
  }
}
<div id="header-search-desktop-div">
    <form role="search" method="get" class="searchform" action="https://themirrorman.co/">
            <span id="header-search-fa" class="fa fa-search">
                <input id="header-search-desktop-input" name="product-search" type="text" value="" placeholder="Product Search…">
                <input type="hidden" name="post_type" value="product" />
            </span>
    </form>
</div>

我收到了以下代码并进行了修改,现在它的功能如上面的图片链接所示:

#header-search-submit {
position: absolute;
z-index: 1;
padding: 0;
right: 15px;
top: 6px;
font-size: 24px;
font-family: FontAwesome;
color: #7B7B7B;
cursor: pointer;
background: 0;
border: 0;
}
<div id="header-search-desktop-div">
<form id="header-search-form" role="search" action="https://themirrorman.co/" method="get">
<fieldset>
    <span class="text">
        <input id="header-search-desktop-span" style="border-radius: 24px;" type="text" value="" name="s" class="s" placeholder="I am looking for..." />
        <input id="header-search-submit" type="submit" value="&#xf002;" name="post_type" />
    </span>
</fieldset>
</form>
</div>


一旦您得到了问题的答案,您所链接的网站将会更改,这将使未来寻找答案的读者无法获得相关信息。请确保通过在问题本身中添加 [mcve] 以使您的问题保持相关性。 - tao
嗨,Andrei,感谢您的反馈。我已经按照您的要求完成了,请确认我所做的是否好? - VisualWizardry
1
将答案标记为已接受就足以告诉所有人您的问题已解决,您不需要在问题中再次说明。通常,只保留必要的内容以便理解和回答问题是优秀问题与其他问题之间的区别之一。因为未来的读者可以更快地理解它。感谢您调整您的问题并欢迎加入[so]。 - tao
1个回答

7

请查看下面的代码,它受到此链接的启发: 如何将Font Awesome图标添加到输入字段中?

input[type="search"] {
    font-family: FontAwesome;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.css" rel="stylesheet"/>


<!-- Where fOO2 is the Unicode value for the fa-search icon -->

<input type="search"  placeholder="&#xf002 Search" >

编辑:2020年7月23日 最近我重新访问了这个问题,并想添加一种替代方案。我基本上复制了Google搜索栏的代码,并对其进行了一些调整。从左到右,搜索栏由标签、输入框和按钮组成。

body {
  height: 100%;
  background-color: rgb(230, 230, 230);
}


/* -----search-container styles----- */

.search-container {
  --searchbox-height: 44px;
  margin: 0 auto 0 auto;
  min-width: 410px;
  width: var(--content-width);
}


/* -----searchbar styles----- */

.searchbar {
  background: white;
  cursor: text;
  margin: 0 auto;
  max-width: 584px;
  box-shadow: 0 1px 6px 0 rgba(32, 33, 36, .28);
  border-radius: 22px;
  box-sizing: border-box;
  font-size: 16px;
  height: var(--searchbox-height);
  position: relative;
}


/* -----search-input styles----- */

.search-input {
  box-sizing: border-box;
  margin: 0;
  height: 100%;
  width: calc(100% - 80px);
  border-color: transparent;
  color: rgb(117, 117, 117);
  font-size: 16px;
  line-height: 44px;
  margin: auto 0;
  overflow: hidden;
  text-align: initial;
  text-overflow: ellipsis;
  user-select: none;
  vertical-align: middle;
  white-space: nowrap;
}

.search-input:focus {
  outline: transparent;
}

.search-input:focus::placeholder {
  color: transparent;
}


/* -----search-icon styles---- */

.search-icon-wrapper {
  padding: 0 24px;
  /* margin-right: 48px; */
}

.search-icon {
  -webkit-mask-image: url(data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHdpZHRoPSIyNCIgaGVpZ2h0PSIyNCIgZmlsbD0iIzVGNjM2OCI+PHBhdGggZD0iTTE1LjUgMTRoLS43OWwtLjI4LS4yN0E2LjQ3MSA2LjQ3MSAwIDAgMCAxNiA5LjUgNi41IDYuNSAwIDEgMCA5LjUgMTZjMS42MSAwIDMuMDktLjU5IDQuMjMtMS41N2wuMjcuMjh2Ljc5bDUgNC45OUwyMC40OSAxOWwtNC45OS01em0tNiAwQzcuMDEgMTQgNSAxMS45OSA1IDkuNVM3LjAxIDUgOS41IDUgMTQgNy4wMSAxNCA5LjUgMTEuOTkgMTQgOS41IDE0eiIvPjwvc3ZnPg==);
  -webkit-mask-position: center;
  -webkit-mask-repeat: no-repeat;
  -webkit-mask-size: 20px;
  background-color: rgb(117, 117, 117);
  bottom: 0;
  left: 16px;
  margin: auto;
  position: absolute;
  top: 0;
  width: 24px;
}


/* -----search-microphone styles----- */

.search-microphone {
  background: url(data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHdpZHRoPSIyNCIgaGVpZ2h0PSIyNCI+PHBhdGggZmlsbD0ibm9uZSIgZD0iTTAgMGgyNHYyNEgweiIvPjxwYXRoIGZpbGw9IiM0Mjg1RjQiIGQ9Ik0xMiAxNWMxLjY2IDAgMi45OS0xLjM0IDIuOTktM0wxNSA1YzAtMS42Ni0xLjM0LTMtMy0zUzkgMy4zNCA5IDV2N2MwIDEuNjYgMS4zNCAzIDMgM3oiLz48cGF0aCBmaWxsPSIjMzRBODUzIiBkPSJNMTEgMTguOTJoMlYyMmgtMnoiLz48cGF0aCBmaWxsPSIjRjRCNDAwIiBkPSJNNyAxMkg1YzAgMS45My43OCAzLjY4IDIuMDUgNC45NWwxLjQxLTEuNDFDNy41NiAxNC42MyA3IDEzLjM4IDcgMTJ6Ii8+PHBhdGggZmlsbD0iI0VBNDMzNSIgZD0iTTEyIDE3Yy0xLjM4IDAtMi42My0uNTYtMy41NC0xLjQ3bC0xLjQxIDEuNDFBNi45OSA2Ljk5IDAgMCAwIDEyLjAxIDE5YzMuODcgMCA2Ljk4LTMuMTQgNi45OC03aC0yYzAgMi43Ni0yLjIzIDUtNC45OSA1eiIvPjwvc3ZnPg==) no-repeat center;
  background-size: 21px 21px;
  bottom: 0;
  cursor: pointer;
  margin-inline-end: 16px;
  padding: 22px 12px 0;
  position: absolute;
  right: 0;
  top: 0;
  width: 21px;
  border-color: transparent;
}
<div class="search-container">
  <div class="searchbar">
    <label for="search-input" class="search-icon-wrapper">
    <div class="search-icon"></div>
    </label>
    <input type="text" class="search-input" id="search-input" placeholder="Search Google or type a URL">
    <button class="search-microphone"></button>
  </div>
</div>


非常感谢您的建议!我已经修改了这篇文章,并将您的回答标记为最佳答案。 - VisualWizardry

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接