html - 如何在搜索框内对齐搜索图标

标签 html css

<分区>

我想知道如何在文本框中对齐搜索图标,就像 Google 的搜索栏一样

像这样:

enter image description here

这就是我得到的,但是我不知道怎么做:

enter image description here

我试图避免使用 Bootstrap 或任何库。我想尽可能地保持原汁原味。

#search-bar {
  width: 32%;
  color: white;
  border-radius: 24px;
  border-style: solid;
  border-width: 1px;
  border-color: white;
  background-color: #2d2d2d;
  height: 44px;
}
<form class="search-bar-form">
  <input id="search-bar" type="text" href="#" />
  <img src="https://via.placeholder.com/24" />
</form>

最佳答案

您可以将图片作为文本框的背景:

#search-bar {
  width: 32%;
  color: white;
  border-radius: 24px;
  border-style: solid;
  border-width: 1px;
  border-color: white;
  background-color: #2d2d2d;
  height: 44px;
  
  background-image: url(https://via.placeholder.com/24);
  background-position: 10px center;
  background-repeat: no-repeat;
  padding-left: 40px;
}
<form class="search-bar-form">
  <input id="search-bar" type="text" value="text goes after the padding" />
</form>

关于html - 如何在搜索框内对齐搜索图标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69224446/

相关文章:

html - 同一页上的两个无序列表,一个内联和一个初始

javascript - 变换后 chrome 中的模糊图像

javascript - 在页面的一部分上规范化-css

c# - Css 规则在 .net 站点中不起作用

html - 有没有办法让父 div 在 CSS 中调整其子内容的大小?

html - 如何制作可点击的父链接 Accordion 菜单

css - 适用于 Mac 和 Windows 的 Chrome——忽略使用系统对话框打印@page 属性

javascript - 在 iframe 中玩游戏时禁用滚动父页面

javascript - 是否可以使用 PHP 表单传递 html 元素属性?

html - 显示内联 CSS 属性行为。有人可以解释吗?