如何防止文本显示在搜索图标后面

时间:2019-01-19 02:40:53

标签: html css

为了进行自学,我试图建立一个搜索字段,但是我正在尝试两个问题。

1。 如果文字太长,它将显示在搜索图标的后面。 我该如何预防?

2。 即使我添加了right:0;

,为什么图标和右边框之间仍然有空格

相对代码:

.search i{
position: absolute;    
top:50%;
right:0;
transform: translateY(-50%);
font-size: 35px;
color: dimgray;
background-color:green;
}

.search-container{
    display:flex;
    width:70%;
}
.search{
    width:100%;
    position:relative;
}
.search input{
    height:50px;
    padding:4px 0 4px 30px;
    font-size: 20px;
}
.search i{
    position: absolute;
    top:50%;
    right:0;
    transform: translateY(-50%);
    font-size: 35px;
    color: dimgray;
}
<!DOCTYPE html>
<html lang="en" dir="ltr">
    <head>
        <meta charset="utf-8">
        <title></title>
        <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.6.3/css/all.css"
        integrity="sha384-UHRtZLI+pbxtHCWp1t77Bi1L4ZtiqrqD80Kn4Z8NTSRyMA2Fd33n5dQ8lWUE00s/" crossorigin="anonymous">
    </head>
    <body>
        <div class="search-container">
            <select class="name">
                <option value="Name-1">Name-1</option>
                <option value="Name-2">Name-2</option>
                <option value="Name-3">Name-3</option>
                <option value="Name-4">Name-4</option>
                <option value="Name-5">Name-5</option>
                <option value="Name-6">Name-6</option>
                <option value="Name-7">Name-7</option>
            </select>
            <div class="search">
                <input class="search-bar search" type="text" name="search" autofocus placeholder="You can search for a name.">
                <i class="fas fa-search"></i>
            </div>
        </div>
    </body>
</html>

3 个答案:

答案 0 :(得分:1)

  1. 您可以在输入中添加一个权限padding,并将box-sizing设置为border-box

  2. 之所以有空格,是因为该图标相对于输入没有放置在右侧,但是相对于<div class="search"> 却是,因为这是它的第一个定位(非静态)祖先元素。而且,如果您使用浏览器对其进行检查,则图标div的结尾位置。

enter image description here

这是固定的代码段:

.search-container{
    display:flex;
    width:70%;
}
.search{
    width:100%;
    position:relative;
}
.search input{
    height:50px;
    padding:4px 50px 4px 30px;
    font-size: 20px;
    box-sizing: border-box;
}
.search i{
    position: absolute;
    top:50%;
    right:10px;
    transform: translateY(-50%);
    font-size: 35px;
    color: dimgray;
}
<!DOCTYPE html>
<html lang="en" dir="ltr">
    <head>
        <meta charset="utf-8">
        <title></title>
        <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.6.3/css/all.css"
        integrity="sha384-UHRtZLI+pbxtHCWp1t77Bi1L4ZtiqrqD80Kn4Z8NTSRyMA2Fd33n5dQ8lWUE00s/" crossorigin="anonymous">
    </head>
    <body>
        <div class="search-container">
            <select class="name">
                <option value="Name-1">Name-1</option>
                <option value="Name-2">Name-2</option>
                <option value="Name-3">Name-3</option>
                <option value="Name-4">Name-4</option>
                <option value="Name-5">Name-5</option>
                <option value="Name-6">Name-6</option>
                <option value="Name-7">Name-7</option>
            </select>
            <div class="search">
                <input class="search-bar search" type="text" name="search" autofocus placeholder="You can search for a name.">
                <i class="fas fa-search"></i>
            </div>
        </div>
    </body>
</html>

答案 1 :(得分:1)

这是您认为可以满足您要求的修改后的代码。 我已从您的搜索输入中删除了user@example.com false : 405 类,因为父节点具有相同的类。

search
.search-container{
    display:flex;
    width:70%;
}
.search{
    width:100%;
    position:relative;
    display: flex;
    flex-direction: row;
    border: 1px solid black;
}

.search:focus-within {
  outline-width: 1px;
  outline-color: rgb(166,200,255);
  outline-style: solid;
}

.search input{
    height:50px;
    width: 90%;
    padding:4px 0 4px 30px;
    font-size: 20px;
    border: none;
    outline: none;
}

.search i{
    display: flex;
    width: 10%;
    justify-content: center;
    align-items: center;
    font-size: 35px;
    padding: 0 .5em 0 .5em;
    color: dimgray;
}

答案 2 :(得分:0)

来自http://jsfiddle.net/Sadhana/2LHh9/

.search_box {
    background:url("http://png-5.findicons.com/files/icons/980/yuuminco/256/search.png");
    background-size:20px 20px;
    background-repeat:no-repeat;
    background-position: right;
    padding-right: 20px !important;
    padding-top: 2px !important;
    padding-bottom: 2px !important;
    padding-left: 5px !important;
    float: left;
}
<input type="text" class="textbox search_box" name="keywords" placeholder="Search..." />