亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用CSS 3在div內輸入時擴展搜索輸入字段

使用CSS 3在div內輸入時擴展搜索輸入字段

POPMUISE 2024-01-22 17:01:01
我創建了一個自定義“搜索框”?!八阉骺颉睂嶋H上是由 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 關注
  • 136 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號