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

和我

我创建了一个自定义的“搜索框”。实际上由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>

陪同Afif

使用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>

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

无法使用 Puppeteer 在输入字段中输入内容

使用输入内容以HTML搜索本地网页

如何使用自定义参数在搜索栏中填充预输入内容?

当输入内容时,使用jQuery执行某些操作

用户开始在输入框中输入内容时,如何使用AngularJS加载新页面

在输入字段中输入内容时如何过滤列表?

使用Python 3在输入中搜索模式时出错

当有人仅使用 PHP 和 HTML 在表单中输入内容时,如何创建新的表格行?

使用e.preventDefault()时无法在输入框中键入内容;

使用Interactive Ruby程序,当我输入内容时关闭

如何使用CSS在表单输入字段中单击可单击的搜索图标

如何在Java中使用Selenium WebDriver(Selenium 2)在文本框中输入内容?

无法使用 int 80h 在 nasm 中输入内容?

使用子流程在python脚本中输入内容来调用python脚本

如果在输入中插入内容,则必须使用复选框

在div而不是输入字段上使用CSS切换

无法使用css使用“:”隐藏输入字段

使用.append()在div中插入内容

jQuery仅在输入字段中输入内容时才添加新字段

当输入字段为焦点时,使用CSS隐藏提交按钮

当输入或div未使用纯CSS聚焦时隐藏div

当我添加输入和div时,使用CSS3 + HTML5的菜单中的意外对齐

使用AWK提取HTML表单/输入内容

使用Selenium和BeautifulSoup输入内容来抓取网站?

无法使用Selenium在搜索字段中输入文本

仅使用CSS扩展输入框

使用CSS在输入字段中更改字体大小

使用CSS更改输入字段的文本颜色

使用CSS根据内容扩展div?