如何为一个文本框设置.input [type = text]

时间:2017-02-22 14:34:00

标签: html css html5

所以我正在开发一个项目,我为搜索文本框设置了动画,如下所示:

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;
  height: 30px;
}

input[type=text]:focus {
  width: 700px;
}
<input type="text" name="search" placeholder="Search..">

一切正常,但现在我遇到了问题,我在同一页面上有更多的文本框,我不希望它们像这一样动画,因为输入[type = text]正在改变它们。任何帮助将不胜感激。

4 个答案:

答案 0 :(得分:2)

有几种方法:

  • input
  • 中使用班级或ID

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;
  height: 30px;
}

input[type="text"]:focus {
  width: 700px;
}

#animated {
  -webkit-transition: width 0.4s ease-in-out;
  transition: width 0.4s ease-in-out;
}
<input id="animated" type="text" name="search" placeholder="Search..">
<hr />
<input type="text" name="search" placeholder="Search..">

  • 使用其他属性,在本例中为[name="search"]

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;
  height: 30px;
}

input[type="text"]:focus {
  width: 700px;
}

input[name="search"] {
  -webkit-transition: width 0.4s ease-in-out;
  transition: width 0.4s ease-in-out;
}
<input type="text" name="search" placeholder="Search..">
<hr />
<input type="text" name="whatever" placeholder="Search..">

答案 1 :(得分:1)

假设您不想添加ID或类,请将您的选择器更改为此...

input[name="search"]

答案 2 :(得分:1)

在输入文本中添加一个类,例如animated

<input type="text" class="animated" name="search" placeholder="Search..">

然后在你的CSS中,修改它:

input.animated:focus {
    width: 700px;
}

答案 3 :(得分:1)

您需要将css选择器更改为更具体,而不仅仅是input[type=text]可能input[name=search]