在焦点上展开输入字段

时间:2013-11-04 11:50:55

标签: html css

我想在焦点上展开搜索框到左侧。以下代码将扩展,但它将向右扩展。无论如何将它扩展到左边?

HTML:

<input type="text" placeholder="search">

CSS:

 input[type="text"] {
    background: #444;
    border: 0 none;
    color: #d7d7d7;
    width:50px;
    padding: 6px 15px 6px 35px;
    -webkit-border-radius: 20px;
    -moz-border-radius: 20px;
    border-radius: 20px;
     margin:3px 12px;
}

input[type="text"]:focus {
    background:#ccc;
    color: #6a6f75;
    width: 120px;    
    margin:3px 12px;
    outline: none;
}

input[type="text"] {
    -webkit-transition: all 0.7s ease 0s;
    -moz-transition: all 0.7s ease 0s;
    -o-transition: all 0.7s ease 0s;
    transition: all 0.7s ease 0s;
}

演示: http://jsfiddle.net/7ppaS/

4 个答案:

答案 0 :(得分:8)

如何使用padding-left将其展开到左侧,同时更改margin-left以便输入框保持在同一位置:

input[type="text"] {
    background: #444;
    border: 0 none;
    color: #d7d7d7;
    width:50px;
    padding: 6px 15px 6px 35px;
    -webkit-border-radius: 20px;
    -moz-border-radius: 20px;
    border-radius: 20px;
     margin:3px 80px;
}

input[type="text"]:focus {
    background:#ccc;
    color: #6a6f75;
    padding-left:80px;  
    margin-left:35px;
    outline: none;
}

DEMO http://jsfiddle.net/7ppaS/4/

答案 1 :(得分:1)

在代码中使用Direction:rtl:

 input[type="text"] {
    background: #444;
    border: 0 none;
    color: #d7d7d7;
    width:50px;
    padding: 6px 15px 6px 35px;
    -webkit-border-radius: 20px;
    -moz-border-radius: 20px;
    border-radius: 20px;
     margin:3px 12px;
    direction:rtl
}

http://jsfiddle.net/7ppaS/8/

答案 2 :(得分:0)

您可以在焦点上添加padding-left:70px;。这将把它扩展到左边。

答案 3 :(得分:0)

使用padding只会创建一个假宽度,不允许在明显宽度内显示长文本。请使用width属性,因为它的目的是:

input {
  width: 10rem;
  -webkit-transition: all 0.7s ease 0s;
  -moz-transition: all 0.7s ease 0s;
  -o-transition: all 0.7s ease 0s;
  transition: all 0.7s ease 0s;
  direction: rtl
}

input[type=text]:focus {
  width: 15rem;
}
<input type="text" placeholder="search">