Q:

How to apply border to an element on mouse hover without affecting the layout in CSS

0

How to apply border to an element on mouse hover without affecting the layout in CSS

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Use the negative CSS margin

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>

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

HTML / CSS Frequently Asked Questions

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
How to apply CSS opacity in IE7 and IE8 browsers... >>
<< How to center align absolute positioned div using ...