为了账号安全,请及时绑定邮箱和手机立即绑定

使用CSS 3在div内输入时扩展搜索输入字段

使用CSS 3在div内输入时扩展搜索输入字段

POPMUISE 2024-01-22 17:01:01
我创建了一个自定义“搜索框”。“搜索框”实际上是由 div 和在其中输入文本构建的。稍后我将在左侧添加“搜索”图标,在右侧添加“清除”按钮。问题是,div 内的输入已经根据其容器调整了大小,当我聚焦输入文本时,我找不到扩展父 div(而不是输入本身)的方法。重要的是 div 将展开,以便我稍后添加的图标和按钮将随输入展开。找不到仅使用 CSS(不使用 JS)的方法。将感谢您的帮助!<!DOCTYPE html><html><head><style> * { box-sizing: border-box;}.App {  width: 100%;  height: 200px;  background-color: tan;}.fieldcontainer {  display: flex;  padding-left: 8px;  background-color: #CCCCCC;  border-color: grey;  width: 300px;  min-height: 35px;  align-items: center;  justify-content: space-between;  border-radius: 8px;  cursor: default;  transition: 'background-color' 0.4s;}.fieldcontainer:hover {  background-color: #C3C3C3;}.searchfield {  background-color: inherit;  font-size: 1em;  border: 0;  flex-grow: 8;    transition: all 0.4s linear;}.searchfield:focus {  outline: none;  width: 100%;}</style></head><body><div class="App">  <div class="fieldcontainer">    <input class="searchfield" type="text" placeholder="search" />  </div></div></body></html>
查看完整描述

1 回答

?
海绵宝宝撒

TA贡献1809条经验 获得超8个赞

使用focus-withinhttps://developer.mozilla.org/fr/docs/Web/CSS/:focus-within

* {

  box-sizing: border-box;

}


.App {

  height: 200px;

  background-color: tan;

}


.fieldcontainer {

  display: flex;

  padding-left: 8px;

  background-color: #CCCCCC;

  border-color: grey;

  width: 300px;

  min-height: 35px;

  align-items: center;

  justify-content: space-between;

  border-radius: 8px;

  cursor: default;

  transition: 0.4s;

}


.fieldcontainer:hover {

  background-color: #C3C3C3;

}


.searchfield {

  background-color: inherit;

  font-size: 1em;

  border: 0;

  flex-grow: 8;

  transition: all 0.4s linear;

  outline:none;

}


.fieldcontainer:focus-within {

  width: 100%;

}

<div class="App">

  <div class="fieldcontainer">

    <input class="searchfield" type="text" placeholder="search" />

  </div>

</div>



查看完整回答
反对 回复 2024-01-22
  • 1 回答
  • 0 关注
  • 28 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信