【问题标题】:Stop placeholder / input text from overlapping background image used as icon?从用作图标的重叠背景图像中停止占位符/输入文本?
【发布时间】:2014-03-27 00:20:01
【问题描述】:

我正在创建一个搜索输入,并使用背景图像 css 作为搜索图标。

但是这会导致占位符和内容与左侧的背景图像重叠。

有没有办法让 div 中的文本从背景图像图标之后开始?如果您进行较长时间的搜索,然后取消聚焦输入,您将看到它重叠。

jsfiddle:

http://jsfiddle.net/ET4e7/

代码:

<form class="searchform" action="search.php?" method="get">
  <input type="text" id="filtersearch" name="st" placeholder="search..." />
</form>

#filtersearch {
 width: 100px;
 padding-right: 5px;
 border-radius: 0px;
 border:none;
 background-color: #555;
 color:#fff;
 font-size: 15px;
 float:left;
 text-align: center;
 Display: table;
 margin-left: 0%;
 height: 100%;
 margin-top: 0px;
 -webkit-appearance: none;
 background-image: url('http://buildsanctuary.com/images/icons/searchicon.png');
 background-repeat: no-repeat;
}

#filtersearch:focus {
 text-align: center;
 width: 100%;
 outline: none;
 background-color: #555;
 -webkit-transition: width 200ms;
 -moz-transition: width 200ms;
 -o-transition: width 200ms;
 -ms-transition: width 200ms;
 transition: width 0.25;
 display: block;
}

.searchform {
 width: 200px; 
 float:left; 
 height: 35px;
}

*注意,尚未确定最大字符长度。

【问题讨论】:

    标签: html css forms search input


    【解决方案1】:

    您只需将padding-left: 20px; 添加到输入中(或者图标实际上有多大):

    #filtersearch {
        width: 100px;
        padding-right: 5px;
        border-radius: 0px;
        border:none;
        background-color: #555;
        color:#fff;
        font-size: 15px;
        float:left;
        text-align: center;
        Display: table;
        margin-left: 0%;
        height: 100%;
        margin-top: 0px;
        -webkit-appearance: none;
        background-image: url('http://placehold.it/20x37');
        background-repeat: no-repeat;
        padding-left: 20px; /* <-------------------------------------------------------- */
    }
    

    小提琴Fiddle

    【讨论】:

    • 就这么简单!我的印象是填充会影响背景图像的位置。但现在一切都说得通了。谢谢:)
    猜你喜欢
    • 1970-01-01
    • 2011-07-05
    • 2014-08-24
    • 2012-11-25
    • 1970-01-01
    • 1970-01-01
    • 2013-06-04
    • 1970-01-01
    • 2013-11-13
    相关资源
    最近更新 更多