The main idea is that the class .pop-out
transforms the code to change background, pop up off the screen a bit (scale & shadow), etc. The only problem is, there is no padding on my original element to the left and right, so it would look very weird when I pop it out without adding more padding as part of the transition.
When I do add padding, it shifts the text as there is now a different amount of horizontal space. I cannot add padding before hand, or the text will be misaligned. How can I fix this?
I currently have the following code:
* {
box-sizing: border-box;
}
.pop-out {
position: relative;
transition: transform 0.2s ease-in-out, box-shadow 0.2s ease-in-out, background-color 0.2s ease-in-out, border-radius 0.2s ease-in-out;
&:hover {
cursor: pointer;
background-color: white !important;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.2);
transform: scale(1.1);
border-radius: 10px;
padding-left: 1rem;
padding-right: 1rem;
}
}
#wrapper {
margin-left: 50px;
display: flex;
justify-content: center;
}
#my-element {
background-color: gray;
width: 200px;
flex: 0 0 auto;
}
<div id="wrapper">
<div class="pop-out" id="my-element">
My element. lipsumlipsum </div>
</div>
2
Answers
Please check this code we have using padding normal class remove for hover:
CSS code:
HTML Code:
To ensure the text doesn’t ‘move’ when scaling so that things stay aligned you could have the whole background color, ‘padding’, border radius and shadow on a before pseudo element which has absolute positioning.
Also, when doing the scale transform have the origin at top right of the element so the text doesn’t shift (if that is what is required) in terms of its left hand position so things are still aligned with any title.
I’ve put ‘padding in quotes here because this snippet creates the effect of padding by increasing the pseudo element’s width.