如何通过CSS在输入获得焦点时更改外部元素的样式?

huangapple go评论69阅读模式
英文:

How to change the style of the outer element when input focus through CSS?

问题

我想询问一下,我希望在输入框获得焦点时,最外层的search_bar边框可以加粗并改变颜色,但因为设计草图需要我的HTML结构作为示例,所以有其他方法可以在获得焦点时更改search_bar的边框颜色和线条粗细吗?

.search_bar {
  display: flex;
  align-items: center;
  width: 90%;
  padding: 10px 8px;
  border-radius: 4px;
  background-color: #fff;
  border: 1px solid #222;
}
.search_bar .search_icon {
  width: 24px;
  height: 24px;
  background-image: url("https://png.pngtree.com/element_our/20200702/ourmid/pngtree-magnifying-glass-icon-image_2292648.jpg");
  background-repeat: no-repeat;
  background-size: cover;
}
.search_bar .search_input {
  width: 100%;
  border: 0px;
  margin-left: 6px;
  font-size: 16px;
}
.search_bar .search_input:focus .search_bar {
  border: 1px solid red;
}
.search_bar .enter {
  white-space: nowrap;
}
<div class="search_bar">
  <label for="search_input" class="search_icon"></label>
  <input id="keyWord" class="search_input" type="text" placeholder="輸入問題關鍵字">
  <button class="enter" id="js-search">搜尋</button>
</div>
英文:

I would like to ask, I hope that when the input box foucs, the border of the outermost search_bar can be thickened and changed color, but because the design draft needs my HTML structure as the example, so there are other ways to focus When changing the frame color and line thickness of search_bar?

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

.search_bar {
  display: flex;
  align-items: center;
  width: 90%;
  padding: 10px 8px;
  border-radius: 4px;
  background-color: #fff;
  border: 1px solid #222;
}
.search_bar .search_icon {
  width: 24px;
  height: 24px;
  background-image: url(&quot;https://png.pngtree.com/element_our/20200702/ourmid/pngtree-magnifying-glass-icon-image_2292648.jpg&quot;);
  background-repeat: no-repeat;
  background-size: cover;
}
.search_bar .search_input {
  width: 100%;
  border: 0px;
  margin-left: 6px;
  font-size: 16px;
}
.search_bar .search_input:focus .search_bar {
  border: 1px solid red;
}
.search_bar .enter {
  white-space: nowrap;
}

<!-- language: lang-html -->

&lt;div class=&quot;search_bar&quot;&gt;
  &lt;label for=&quot;search_input&quot; class=&quot;search_icon&quot;&gt;&lt;/label&gt;
  &lt;input id=&quot;keyWord&quot; class=&quot;search_input&quot; type=&quot;text&quot; placeholder=&quot;輸入問題關鍵字&quot;&gt;
  &lt;button class=&quot;enter&quot; id=&quot;js-search&quot;&gt;搜尋&lt;/button&gt;
&lt;/div&gt;

<!-- end snippet -->

答案1

得分: 1

在这种情况下,您可以使用一个名为 :focus-within 的CSS伪类,它匹配一个元素,如果该元素或其任何后代元素都处于焦点状态。

.search_bar:focus-within {
  border: 1px solid red;
}
.search_bar {
  display: flex;
  align-items: center;
  width: 90%;
  padding: 10px 8px;
  border-radius: 4px;
  background-color: #fff;
  border: 1px solid #222;
}
.search_bar .search_icon {
  width: 24px;
  height: 24px;
  background-image: url("https://png.pngtree.com/element_our/20200702/ourmid/pngtree-magnifying-glass-icon-image_2292648.jpg");
  background-repeat: no-repeat;
  background-size: cover;
}
.search_bar .search_input {
  width: 100%;
  border: 0px;
  margin-left: 6px;
  font-size: 16px;
}
.search_input:focus {
  outline: none;
}
.search_bar:focus-within {
  border: 1px solid red;
}
.search_bar .enter {
  white-space: nowrap;
}
<div class="search_bar">
  <label for="search_input" class="search_icon"></label>
  <input id="keyWord" class="search_input" type="text" placeholder="輸入問題關鍵字">
  <button class="enter" id="js-search">搜尋</button>
</div>
英文:

In this case you could use a CSS pseudo-class named :focus-within, which

> matches an element if the element or any of its descendants are focused

.search_bar:focus-within {
  border: 1px solid red;
}

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

.search_bar {
  display: flex;
  align-items: center;
  width: 90%;
  padding: 10px 8px;
  border-radius: 4px;
  background-color: #fff;
  border: 1px solid #222;
}
.search_bar .search_icon {
  width: 24px;
  height: 24px;
  background-image: url(&quot;https://png.pngtree.com/element_our/20200702/ourmid/pngtree-magnifying-glass-icon-image_2292648.jpg&quot;);
  background-repeat: no-repeat;
  background-size: cover;
}
.search_bar .search_input {
  width: 100%;
  border: 0px;
  margin-left: 6px;
  font-size: 16px;
}
.search_input:focus {
  outline: none;
}
.search_bar:focus-within {
  border: 1px solid red;
}
.search_bar .enter {
  white-space: nowrap;
}

<!-- language: lang-html -->

&lt;div class=&quot;search_bar&quot;&gt;
  &lt;label for=&quot;search_input&quot; class=&quot;search_icon&quot;&gt;&lt;/label&gt;
  &lt;input id=&quot;keyWord&quot; class=&quot;search_input&quot; type=&quot;text&quot; placeholder=&quot;輸入問題關鍵字&quot;&gt;
  &lt;button class=&quot;enter&quot; id=&quot;js-search&quot;&gt;搜尋&lt;/button&gt;
&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年1月9日 14:03:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/75053666.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定