42

I have a inline element (a <span>) nested in a <h1> tag. I applied a transform property to the h1 ( skew so it looks like a parallelogram).
I need to transform the span tag to "unskew" it and its text. But this only seems to work in IE.

Here is an example of the HTML and CSS:

h1 {
  background: #f00;
  padding: .25em .5em;
  text-align: right;
  transform: skew(-15deg);
}
h1 span {
  color: #fff;
  transform: skew(15deg);
}
<h1><span>This is a Title</span></h1>
web-tiki
  • 92,319
  • 29
  • 210
  • 241
jenhan
  • 549
  • 2
  • 6
  • 14
  • Possible duplicate of [CSS transform doesn't work on inline elements](https://stackoverflow.com/questions/14883250/css-transform-doesnt-work-on-inline-elements) – TylerH Oct 13 '17 at 18:16

2 Answers2

93

Explanation:
A <span> or a link (<a>) are inline elements and the transform property doesn't apply to inline elements.
Here is the list of transformable elements from the CSS Transforms Module Level 1.

Solution:
Set the display property of the span to inline-block or block. This will let you apply transforms to the span element.
It also works for other inline elements like <a> <em> <strong>... (see the list of inline elements on MDN).

Here is a demo with the <span> and link <a> elements :

h1 {
  background: teal;
  padding: .25em .5em;
  margin: 1em;
  transform: skew(-15deg);
}
h1 span,
h1 a {
  color: #fff;
  display: inline-block;  /* <- ADD THIS */
  transform: skew(15deg);
}
<h1><span>This is a span in a title</span></h1>
<h1><a href="#">This is a link in a title</a></h1>
web-tiki
  • 92,319
  • 29
  • 210
  • 241
3

A little late here, but you can set

h1 span{
   position:absolute;
}

Then use the CSS3 transform properties normally.

Anurag Srivastava
  • 13,226
  • 4
  • 25
  • 40