CSS transform doesn't work on inline elements

CssCss Transforms

Css Problem Overview


I wanted to mirror letter E in the word WEBLOG so I used CSS transform property but it doesn't work if I wrap the text inside a span but it works if I assing display: inline-block; or display: block;

So transforms doesn't apply to inline elements?

Example 1 (Fails To Transform)

<h1>W<span>E</span>BLOG</h1>

h1 span {
    transform:rotate(7deg);
    -ms-transform:rotate(7deg); /* IE 9 */
    -moz-transform:rotate(7deg); /* Firefox */
    -webkit-transform:rotate(7deg); /* Safari and Chrome */
    -o-transform:rotate(7deg); /* Opera */
}

Example 2 (Works, If Used display: block OR display: inline-block)

Css Solutions


Solution 1 - Css

Answered here in the official W3 specifications under transformable element:

> an element whose layout is governed by the CSS box model which is > either a block-level or atomic inline-level element, or whose > ‘display’ property computes to ‘table-row’, ‘table-row-group’, > ‘table-header-group’, ‘table-footer-group’, ‘table-cell’, or > ‘table-caption’ [CSS21]

Solution 2 - Css

The updated version of the specification says:

>A transformable element is an element in one of these categories: > >- all elements whose layout is governed by the CSS box model except for non-replaced inline boxes, table-column boxes, and table-column-group boxes [CSS2], > >- all SVG paint server elements, the clipPath element and SVG renderable elements with the exception of any descendant element of text content elements [SVG2].

We should note that not all the inline elements cannot be transformed but only the non-replaced inline elements thus the replaced inline elements can be transformed.

So basically we can apply transformation to img, canvas, etc without the need of making them inline-block or block

var all = document.querySelectorAll('.replaced');

for(var i=0;i<all.length;i++) {
 console.log(window.getComputedStyle(all[i],null).getPropertyValue("display"));
}

canvas {
  background:red;
}

.replaced {
  transform:rotate(20deg);
}

<img src="https://picsum.photos/200/200?image=1069" class="replaced">
<canvas class="replaced"></canvas>

More details about replaced elements:

https://html.spec.whatwg.org/multipage/rendering.html#replaced-elements

https://developer.mozilla.org/en-US/docs/Web/CSS/Replaced_element

https://stackoverflow.com/q/12468176/8620333

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMr. AlienView Question on Stackoverflow
Solution 1 - CssGiacomo1968View Answer on Stackoverflow
Solution 2 - CssTemani AfifView Answer on Stackoverflow