I found this: width/height after transform
and several others, but nothing is not quite what I\'m looking for. What I want is to scale something to 50% of it\'s siz
I finally figured out a simple solution and it is much similar than where I started from.
I will admit, it took many attempts to figure this out (over the last year or so) to finally figure it out. I was actually looking for an answer to this question. I have had problems with this before. Anyways, I started working this out again and I did find a simple solution.
Anyways, the trick was to use the margin bottom together with the CSS calc script.
Also, you must have to have absolute height declared for the the object (div). I dont think this will work with if the div can expand, otherwise, after many attempts and hours on this I finally have a very simple, clean, working solution.
So, for example, height = 300px (not 30%, or no height). I would even set overflow-y = hidden if you can. If you want the div to grow (not have an absolute height), I believe you will need JavaScript. I did not try it. Otherwise this is working correctly in all tests.
I am sure this will work nice with SASS or LESS since you could declare some of the repeated things using variables in the CSS. I have not tried that yet.
Some important comments on my solution:
I have also have set up a Codepen to test this with and for sharing.
https://codepen.io/cyansmiles/details/yvGaVP
.box-wrap {
display: block;
position: relative;
box-sizing: border-box;
width: 100%;
margin: auto;
text-align: center;
margin: 30px 10px;
padding: 40px;
background: yellow;
}
.box-wrap:before {
content: "";
display: block;
box-sizing: border-box;
position: absolute;
background: rgba(0, 0, 0, 0.5);
width: calc(100% - 80px);
height: calc(100% - 80px);
top: 40px;
left: 40px;
}
.box {
display: inline-block;
box-sizing: border-box;
position: relative;
transform-origin: 50% 0;
width: 300px;
height: 150px;
background: red;
}
.box.size-80 {
transform: scale(0.8);
margin: 0 auto calc(-150px * (1 - 0.8));
}
.box.size-70 {
transform: scale(0.7);
margin: 0 auto calc(-150px * (1 - 0.7));
}
.box.size-60 {
transform: scale(0.6);
margin: 0 auto calc(-150px * (1 - 0.6));
}
.box.size-50 {
transform: scale(0.5);
margin: 0 auto calc(-150px * (1 - 0.5));
}
//etc
Box at 100%
Box at 80%
Box at 70%
Box at 60%
Box at 50%