If you apply the border around an element on mouse hover it will move the surrounding elements from its original position, this is the default behavior. However using the negative CSS margin value and a little trick you can do it nicely without affecting the other elements or content.
Let's try out an example to understand how it basically works:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Add CSS Border on Mouse Hover without Pushing Content</title>
<style>
ul {
padding: 0;
list-style: none;
}
ul li {
float: left;
margin: 10px;
}
ul li a, ul li a img {
display: block;
}
ul li a:hover {
border: 5px solid #333333;
overflow: hidden;
}
ul li a:hover img{
margin: -5px;
}
</style>
</head>
<body>
<ul>
<li><a href="#"><img src="/examples/images/club.jpg" alt="Club Card"></a></li>
<li><a href="#"><img src="/examples/images/diamond.jpg" alt="Diamond Card"></a></li>
<li><a href="#"><img src="/examples/images/spade.jpg" alt="Spade Card"></a></li>
<li><a href="#"><img src="/examples/images/heart.jpg" alt="Heart Card"></a></li>
</ul>
</body>
</html>
Use the negative CSS margin
If you apply the
borderaround an element on mouse hover it will move the surrounding elements from its original position, this is the default behavior. However using the negative CSSmarginvalue and a little trick you can do it nicely without affecting the other elements or content.Let's try out an example to understand how it basically works:
need an explanation for this answer? contact us directly to get an explanation for this answer