1

i am a beginner at CSS. i am trying to change the width of the input to 120px when i hover over the search text. how do i do it? i have try few code, but it doesn't seem to work.

html

<body>
    <div class = "search-box">
    <input class="search-text" type="text" placeholder="type something">
    <a class="search-button" href="#">search</a>

    </div>
</body>

CSS

a:hover + .search-text{`
width:125px;
}
Abhinav K
  • 57
  • 8

5 Answers5

1

Css

There isn't a nice CSS selector that allows us to target a element when an other element is behing hovered.

There is a way, described in this question, but that expects the element to exist after the hover element seen in the DOM. Your current HTML is reversed, so that wont work.

The easiest way, in my opinion, is to use flex-direction to reverse the DOM element so we can use the 'hack' described in the linked post.

Example:

a:hover ~ .search-text {
  width:200px;
}

.search-box {
  display: flex;
  justify-content: flex-end;
  flex-direction: row-reverse;
}

.search-button {
  margin-left: 10px;
}
<div class = "search-box">
  <a class="search-button" href="#">search</a>
  <input class="search-text" type="text" placeholder="type something">
</div>

NB: Since the <input> is getting larger, the mouse isn't hovering on the element anymore, therefor it will shrink again, letting the mouse back over the element, this causes the weird jumping effect.


jQuery

Of course we can create a jQuery based solution using the mouseover and mouseleave events. This way we

  • Don't need to change the DOM order
  • Prevent the <input> jumping by checking on mouseleave on the <input> instead off the <a>

Example:

let i = $('.search-text'),
    a = $('.search-button');
i.width(150);

a.on('mouseover', function() {
    i.width(300);
});

i.on('mouseleave', function() {
    i.width(150);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<div class = "search-box">
  <input class="search-text" type="text" placeholder="type something">
  <a class="search-button" href="#">search</a>
</div>
0stone0
  • 34,288
  • 4
  • 39
  • 64
0

Try this: CSS

.search-text:hover{
width: 120px;
}
<body>
    <div class = "search-box">
    <input class="search-text" type="text" placeholder="type something">
    <a class="search-button" href="#">search</a>

    </div>
</body>

NB : you are trying to decrease the width.

Sajid
  • 9
  • 1
0

To do this without Javascript you will have to wrap your input element inside of a(anchor) tag something like this--

try out the code below

a:hover .search-text{
  width: 120px;
}
<div class = "search-box">
      
      <a class="search-button" href="#"><input class="search-text" type="text" placeholder="type something">search</a>
  
      </div>
Gulshan Aggarwal
  • 954
  • 1
  • 7
  • 13
0

From 0stone0 answer :)

.search-box:hover > .search-text {
  width:125px;
}
<body>
    <div class = "search-box">
    <input class="search-text" type="text" placeholder="type something">
    <a class="search-button" href="#">search</a>
    </div>
</body>
Nikola Pavicevic
  • 21,952
  • 9
  • 25
  • 46
-1

.searchbox > input[type='text']{
  border : 1px solid rebeccapurple;
  outline: none;
  transition : 0.7s;
}


.searchbox > input[type='text']:hover{
  padding-right: 300px; 
}
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width">
    <title>repl.it</title>
    <link href="style.css" rel="stylesheet" type="text/css" />

  </head>
  <body>
    <div class="searchbox">
      <input type="text" placeholder="search">
    </div>

  </body>
</html>
  • This does not answers OP question, he want the input to enlarge when hovering on the search text~ – 0stone0 Jul 14 '21 at 14:04