【发布时间】:2015-09-02 19:47:59
【问题描述】:
我正在尝试为我的“搜索”输入创建一个自定义占位符。它应该看起来像一个搜索图标(为此使用 Bootstrap glyphicon glyphicon-search 类),然后是输入元素内的“搜索”一词,就像占位符一样,并且居中。
我正在尝试将包含这些的 div 定位到输入中的元素,但我无法正确处理。
这是jsfiddle中的代码。
HTML:
<div class="search-wrapper">
<form class="post_search" id="post_search" action="/posts/explore" accept-charset="UTF-8" method="get"><input name="utf8" type="hidden" value="✓">
<input autocomplete="off" class="search-input" type="search" name="q[caption_or_user_user_name_cont]" id="q_caption_or_user_user_name_cont">
<div class="placeholder">
<div>
<span class="glyphicon glyphicon-search"></span>
<span>Search</span>
</div>
</div>
</form>
</div>
CSS:
.search-wrapper {
max-width: 340px;
margin: 0 auto;
text-align: center;
margin-top: 20px;
display: inline;
}
.search-wrapper .search-input {
border: 1px solid #ddd;
border-radius: 15px;
background-color: #eee;
width: 220px;
height: 31px;
padding: 10px 15px;
transition: 0.25s all;
}
.search-wrapper .search-input:focus {
outline: 0 none;
background-color: #fff;
transition: 0.25s all;
}
.search-wrapper .placeholder {
display: inline;
position: relative;
top: 30%;
width: 40px;
margin: 0 auto;
text-align: center;
font-size: 10px;
}
那么当专注于输入时,占位符应该消失了,我想这对一些js来说应该不难。
但回到问题,我做错了什么?如何按预期显示占位符?
【问题讨论】:
-
我已经想提一下,由于您的元素是如何定位的,您甚至不需要 javascript,只需使用
input:focus + .placeholder定位即可 -
抱歉,我对 sn-ps 不熟悉...您在 jsfiddle 中有代码,这与我的工作示例非常接近。你是对的,我可以这样定位它并添加
display: none属性。无论如何,这不是我要问的。 -
想试试这样的:jsfiddle.net/83x8tfwp/3
标签: css