Code
.topnav {
background-color: black;
overflow: hidden;
}
.topnav a {
box-sizing: border-box;
color: white;
display: inline-block;
padding: 14px 16px;
text-decoration: none;
font-size: 17px;
margin-top: 6px;
margin-bottom: 6px;
margin-right: 6px;
}
.topnav a:hover {
background-color: blue;
color: white;
}
.topnav a.active {
background-color: blue;
color: white;
}
.topnav input[type=text] {
display: inline-block;
float: right;
width: 20%;
padding: 14px;
border-radius: 1px;
background-color: white;
margin: 6px;
box-sizing: border-box;
outline: none;
transition: 0.1s;
}
.topnav input[type=text]:hover {
border: 3px solid blue;
}
<div id='menu' class='topnav'>
<a href='#1'>abcdefg</a>
<a href='#2'>abcdefg</a>
<a href='#3'>abcdefg</a>
<a href='#4'>abcdefg</a>
<input type='text' placeholder='Search here'>
</div>
Goal
As you can see, when <input>
is hovered, its border expands by 3 pixels.
However, it makes <div>
expand as well, which is not what I want. In other words, I want <div>
not to expand when input
is being hovered.
What I've tried
To accomplish the above, I've set the width of <input>
and <a>
to auto
As long as the value of width is auto, the element can have horizontal margin, padding and border without becoming wider than its container (unless of course the sum of margin-left + border-left-width + padding-left + padding-right + border-right-width + margin-right is larger than the container). The width of its content box will be whatever is left when the margin, padding and border have been subtracted from the container’s width.
Recovered from The difference between width:auto and width:100%
Unfortunately, It didn't work. I also don't want to use fixed widths, for example, set <input>
width to 20 pixels. Although it works, that is, it doesn't make <div>
expand, it's too small.