动画搜索表单向左扩展

时间:2017-02-02 15:09:07

标签: html css

我的框正在向右扩展,如何将其扩展到左侧?

 input[type=text] {
        width: 130px;
        box-sizing: border-box;
        border: 2px solid #ccc;
        border-radius: 4px;
        font-size: 16px;
        background-color: white;
        background-image: url('searchicon.png');
        background-position: 10px 10px; 
        background-repeat: no-repeat;
        padding: 12px 20px 12px 40px;
        -webkit-transition: width 0.4s ease-in-out;
        transition: width 0.4s ease-in-out;
    }
    
    input[type=text]:focus {
        width: 100%;
    }
    
<form>
      <input type="text" name="search" placeholder="Search..">
    </form>

1 个答案:

答案 0 :(得分:9)

只需将其对齐到其容器元素的右侧即可。将display属性更改为block并将左右边距设置为0auto,或将其保留为内联元素并应用text-align: right容器块。

&#13;
&#13;
input[type=text] {
  display:block;
  margin: 0 0 0 auto;
  width: 130px;
  box-sizing: border-box;
  border: 2px solid #ccc;
  border-radius: 4px;
  font-size: 16px;
  background-color: white;
  background-image: url('searchicon.png');
  background-position: 10px 10px;
  background-repeat: no-repeat;
  padding: 12px 20px 12px 40px;
  -webkit-transition: width 0.4s ease-in-out;
  transition: width 0.4s ease-in-out;
}
input[type=text]:focus {
  width: 100%;
}
&#13;
<form>
  <input type="text" name="search" placeholder="Search..">
</form>
&#13;
&#13;
&#13;