Reputation: 3302
I need to higlight a div 5 pixels before its current position on mouse over. That is to say if the user passes the pointer over it the div would be highlighted 5 pixels before, but without changing its content position.
.container {
width: 100%;
&:hover {
background-color: rgba(0, 0, 0, 0.1);
position: relative;
padding-left: 5px;
}
}
<div class='container'>
<a href='#'>Click me!</a>
</div>
On mouse over it highlights but the "Click me!" message moves 5 pixels to the right. How can I achieve this?
Upvotes: 0
Views: 86
Reputation: 27227
Use the outline
property. This is literally the use case it is meant for. It improves accessibility. For more info read http://www.outlinenone.com
Upvotes: 0
Reputation: 3749
Try this SCSS
.container {
width: 100%;
&:hover {
background-color: rgba(0, 0, 0, 0.1);
position: relative;
padding-left: 5px;
a{
margin-left:-5px;
}
}
}
hope this helps..
Upvotes: 1
Reputation: 2975
I've had a go at this as it looked fun.
Moving the element 5px left whilst preserving the contents with padding (and adding 5px to the width to offset the shift)
You should have something that looks like this:
.container:hover {
position:relative;
width:calc(100% + 5px);
left:-5px;
box-sizing:border-box;
padding-left:5px;
}
Upvotes: 0
Reputation: 7696
add margin-left:-5px; to your hover class
.container {
width: 100%;
}
.container:hover {
background-color: rgba(0, 0, 0, 0.1);
position: relative;
margin-left:-5px;
}
<div class='container'>
<a href='#'>Click me!</a>
</div>
Upvotes: 1