How do I add negative margin equal to half the width of an image in css?
1 answer
If the negative left position depends on (half) the width of the element, you can do this:
transform: translateX(-50%);
quick demo:
*{margin:0;}
img {
vertical-align: top;
height: 100vh;
transform: translateX(-50%);
}
<img src="//placehold.it/800x600/0bf">
and hover animation
*{margin:0;}
.halfThere {
vertical-align: top;
height: 100vh;
transition: 0.4s;
transform: translateX(-50%);
}
.halfThere:hover {
transform: translateX(0%);
}
<img class="halfThere" src="//placehold.it/800x600/0bf">
+1
source to share