如何设置搜索框的样式

时间:2017-06-22 18:23:01

标签: html css flexbox styling search-box

我正在尝试设置搜索框的样式,但它不起作用。在.search-input下我试图将搜索框的背景颜色更改为绿色,我试图让搜索框更长。有人看到我的错误吗?按搜索图标时,搜索框会下拉。

jsfiddle - https://jsfiddle.net/n9uo8ek9/

document.getElementById("search-label").addEventListener("click", function(e) {
  if (e.target == this) {
    e.preventDefault();
    this.classList.toggle("clicked");
  }
});
/*--------------------------------------------------------------
## Search
--------------------------------------------------------------*/
#ht-masthead .search-field {
  padding: 0 10px 0 34px;
  display: flex;
  align-items: center;
  justify-content: flex-end;
}

#ht-masthead .search-field:focus {
  background-color: #fff;
  border: 2px solid #c3c0ab;
  cursor: text;
  outline: 0;
}

#ht-masthead .search-form {}

.search-toggle:hover #ht-masthead .search-form {
  display: block;
}

.search-form .search-submit {
  display: none;
}

.search-form {
  position: relative;
}

.search-form label {
  position: relative;
  background: url('https://cdn4.iconfinder.com/data/icons/ionicons/512/icon-ios7-search-strong-128.png') 0 0 no-repeat;
  background-size: cover;
    height: 70px;
}

.search-input {
  transform: translateY(-100%);
  opacity: 0;
  position: absolute;
  top: 100%;
  transition: opacity .25s, transform .25s;
  left: 0;
  z-index: -1;
  border: 0;
  outline: 0;
  background-color:#32CD32;
}

.search-label,
.search-input {
  background: #ccc;
  padding: .5em;
  display: inline-block;
}

.clicked + .search-input {
  opacity: 1;
  transform: translateY(0);
}
<div class="search-field">
</div>
<form role="search" method="get" class="search-form" action="<?php echo home_url( '/' ); ?>">
  <label for="search-input" id="search-label" class="search-label">
    Search for:   
    </label>
<input id="search-input" type="search" class="search-input" placeholder="Search …" value="" name="s" title="Search for:" />
  <input type="submit" class="search-submit" value="Search" />
</form>

3 个答案:

答案 0 :(得分:0)

这可能是因为它的特殊性。如果您使用下面的CSS,它将正常工作。

.clicked + .search-input {
  opacity: 1;
  transform: translateY(0);
  width: 200px; /* or whatever you want it to be */
  background: lightgreen;
}

&#13;
&#13;
document.getElementById("search-label").addEventListener("click", function(e) {
  if (e.target == this) {
    e.preventDefault();
    this.classList.toggle("clicked");
  }
});
&#13;
/*--------------------------------------------------------------
## Search
--------------------------------------------------------------*/

#ht-masthead .search-field {
  padding: 0 10px 0 34px;
  display: flex;
  align-items: center;
  justify-content: flex-end;
}

#ht-masthead .search-field:focus {
  background-color: #fff;
  border: 2px solid #c3c0ab;
  cursor: text;
  outline: 0;
}

#ht-masthead .search-form {}

.search-toggle:hover #ht-masthead .search-form {
  display: block;
}

.search-form .search-submit {
  display: none;
}

.search-form {
  position: relative;
}

.search-form label {
  position: relative;
  background: url('https://cdn4.iconfinder.com/data/icons/ionicons/512/icon-ios7-search-strong-128.png') 0 0 no-repeat;
  background-size: cover;
  height: 70px;
}

.search-input {
  transform: translateY(-100%);
  opacity: 0;
  position: absolute;
  top: 100%;
  transition: opacity .25s, transform .25s;
  left: 0;
  z-index: -1;
  border: 0;
  outline: 0;
  background-color: #32CD32;
}

.search-label,
.search-input {
  background: #ccc;
  padding: .5em;
  display: inline-block;
}

.clicked+.search-input {
  opacity: 1;
  transform: translateY(0);
  width: 200px;
  background-color: lightgreen;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="search-field">
</div>
<form role="search" method="get" class="search-form" action="<?php echo home_url( '/' ); ?>">
  <label for="search-input" id="search-label" class="search-label">
    Search for:   
    </label>
  <input id="search-input" type="search" class="search-input" placeholder="Search …" value="" name="s" title="Search for:" />
  <input type="submit" class="search-submit" value="Search" />
</form>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

这是因为在.search-label, .search-input陈述之后你有.search-input{ background-color:#32CD32; }。 CSS,层叠样式表,从上到下阅读,所以自.search-label, .search-input之后,它会读取,覆盖前一个语句并使背景变灰。 .search-label, .search-input之间的逗号意味着它将这些样式应用于每个样式。

如果您只是移动他们被调用的顺序,它将正常工作。

答案 2 :(得分:0)

问题是.search输入背景颜色是由.search-label, .search-input描述符编辑的,这会破坏你的工作!

.search-input {
      transform: translateY(-100%);
      opacity: 0;
      position: absolute;
      top: 100%;
      transition: opacity .25s, transform .25s;
      left: 0;
      z-index: -2;
      border: 1;
      outline: 0;
      background-color:#32CD32;
    }

.search-label, .search-input {
      //background: #ccc; //This can be deleted, as it is undoing your work
      padding: .5em;
      display: inline-block;
 }

.clicked + .search-input {
      opacity: 1;
      width: 200px;
      background-color:#32CD32;
      transform: translateY(0);
 }

JSFIDDLE