css - 输入内的响应式 fa-icon

这是来自 Treehouse 作者的一些代码,展示了如何在输入框内放置一个图标。

HTML:

<div class="search">
  <span class="fa fa-search"></span>
  <input placeholder="Search term">
</div>

CSS:

@import url("//maxcdn.bootstrapcdn.com/font-awesome/4.1.0/css/font-awesome.min.css");
body {
  margin: 30px;
}

.search {
  position: relative;
  color: #aaa;
  font-size: 16px;
}

.search input {
  width: 250px;
  height: 32px;
  background: #fcfcfc;
  border: 1px solid #aaa;
  border-radius: 5px;
  box-shadow: 0 0 3px #ccc, 0 10px 15px #ebebeb inset;
}

.search input {
  text-indent: 32px;
}

.search .fa-search {
  position: absolute;
  top: 10px;
  left: 10px;
}

证明这一点的笔: http://codepen.io/jamesbarnett/pen/aBnbL#0

如何将图标放在输入的末尾并让它响应屏幕的变化?

我尝试使用百分比定位图标,但它根本不起作用。 这是我尝试的 fiddle :

http://jsfiddle.net/x7j869g7/2/

最佳答案

您可以将display:inline-block;position:relative 赋给.search 然后将图标对齐到右侧(右:10px).

Jsfiddle

@import url("//maxcdn.bootstrapcdn.com/font-awesome/4.1.0/css/font-awesome.min.css");
 body {
  margin: 30px;
}
.search {
  position: relative;
  color: #aaa;
  font-size: 16px;
  display: inline-block;
}
.search input {
  width: 250px;
  height: 32px;
  background: #fcfcfc;
  border: 1px solid #aaa;
  border-radius: 5px;
  box-shadow: 0 0 3px #ccc, 0 10px 15px #ebebeb inset;
}
.search input {
  text-indent: 5px;
  padding-right: 30px;
  box-sizing: border-box;
}
.search .fa-search {
  position: absolute;
  top: 8px;
  right: 10px;
}
<div class="search">
  <span class="fa fa-search"></span>
  <input type="text">
</div>

https://stackoverflow.com/questions/34081889/

相关文章:

c# - wpf - Expander 的标题适合内容宽度?

solr - 使用身份验证在控制台中创建 Solr 核心

scala - 收集和计数操作之间有什么区别?

haskell - 在 Haskell 中创建一个简单的数组

php - 使用 Prestashop 1.6 在模块中获取产品类别名称

git - "git clone"是否创建一个完全相对的目录?

c# - 还有其他更好的方法可以将参数传递给 backgroundworker runasync 吗

php - 编译PHP7错误

gcc - 32 位 GCC C 程序中的内联 64 位汇编

c++11 - 向量的分段平方,C++ Eigen 中两个向量的分段乘积