In the snippet below I have a div that's rotated 45 degrees. How can we shrink this div along the X axis without modifying the HTML at all?
html,
body {
height: 100%;
margin: 0;
}
html {
overflow: hidden;
}
body {
display: flex;
}
div {
width: 10rem;
height: 10rem;
margin: auto;
background-image: linear-gradient( #eee, #888 );
border-radius: 2rem;
border-width: 0.05rem;
border-color: #666;
border-style: solid;
transform: rotate( 45deg );
}
<div></div>
Example achieved using a wrapper element
html,
body {
height: 100%;
margin: 0;
}
html {
overflow: hidden;
}
body {
display: flex;
}
div {
margin: auto;
transform: scaleX( 0.75 );
}
div > div {
width: 10rem;
height: 10rem;
background-image: linear-gradient( #eee, #888 );
border-radius: 2rem;
border-width: 0.05rem;
border-color: #666;
border-style: solid;
transform: rotate( 45deg );
}
<div>
<div></div>
</div>
How can we achieve the above without adding any markup to the original example consisting of only 1 div? Of course we can do all things with JavaScript but the interest here is specifically if this is possible with only CSS. Perhaps through pseudo elements?
EDIT To explain why this is not a duplicate of How to apply multiple transforms in CSS? and simply placing the transforms on the same line a specific order does not work see the snippet below:
html,
body {
height: 100%;
margin: 0;
}
html {
overflow: hidden;
}
body {
display: flex;
}
div {
width: 10rem;
height: 10rem;
margin: auto;
background-image: linear-gradient( #eee, #888 );
border-radius: 2rem;
border-width: 0.05rem;
border-color: #666;
border-style: solid;
transform: rotate( 45deg ) scaleX( 0.75 );
}
<div></div>
The transforms from before aren't 'remembered'. The scaling is done from a perspective as if the div was still in its original position before it was rotated.