CSS transform doesn't work on inline elements

后端 未结 2 1144
Happy的楠姐
Happy的楠姐 2020-11-21 07:19

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

相关标签:
2条回答
  • 2020-11-21 08:01

    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

    what is a non-replaced inline element?

    0 讨论(0)
  • 2020-11-21 08:07

    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]

    0 讨论(0)
提交回复
热议问题