this is my HTML code:
.search-bar{
width: 40px;
height: 45px;
transition: 0.7s;
}
.search-icon {
top: .4%;
right: 5%;
width: 35px;
margin-left: 300px;
cursor: pointer;
box-sizing: border-box;
}
.search-bar:focus{
cursor: pointer;
width: 200px;
outline: none;
}
.search-btn{
background-color: transparent;
outline: none;
border: none;
position: absolute;
top: .4%;
right: -10%;
width: 35px;
height: 35px;
margin-left: 300px;
cursor: pointer;
}
.search-icon:focus ~ .search-bar{
border-width: 0px;
width: 200px;
}
<div class="mid-header-div">
<div search-bar-div>
<input class="search-bar" type="text">
<button class="search-btn">
<img class="search-icon" src="../Frst/intermediate/Icon/search.svg" />
</button>
</div>
Typically, it’s a search bar that when I click on the input box or button, it should increase the width to 200px.
The problem is I have a search icon in this input that when I click on the search icon the width doesn’t increase.
3
Answers
The selector
.search-icon:focus ~ .search-bar
does not match anything since there is no element with the classsearch-bar
that is a subsequent sibling of an element with the classsearch-icon
when focused.Instead, you could consider using JavaScript to focus the
<input>
element when the.search-icon
button is pushed:There is no way to select parent element by its child but
:has
pseudo-class is like indirectly selecting parent by child.You should aware of
:has
because recently it has all browser supportAdd the following rule to increase the width when either the input or icon is focused