Skip to main

CSS attempts at text with inline skewed background

I've been looking for a reliable way to add a styalised background behind strings of text. It would allow more stylalised designs for emphasis tags such as <em> and <strong>.

Notes:

  • Using skew is only rendered if the item is display block or inline-block.
  • Some of these look the same on a wide viewport but fail differently as the viewport width is decreased.

Attempt A
— 1 element

  • Text will be skewed the same as the background. Don't know how to straighten the text, if straight text is desired.
  • If multiline, preceding and following text is ejected to the next line even if there is space for it inline.
  • Text always stays within background.

Test with a very short string of text.

Test with a much longer string of text that would wrap onto multiple lines to check for artifacting or other unintended consequences of the attempt's code.

em {
  display: inline-block;
  font-style: normal;
  position: relative;
  transform: skewX(-10deg);
  
  border-radius: 0.2em;
  background: var(--gradient);
  padding: 0.1em 0.3em;
}

Attempt B
— 1 element and a pseudo element

  • If multiline, preceding and following text is ejected to the next line.
  • Multi line text like this won't always sit on the background properly or will escape outside the background.
  • Some weird z-index to get the background behind the text.

Test with a very short string of text.

Test with a much longer string of text that would wrap onto multiple lines to check for artifacting or other unintended consequences of the attempt's code.


em {
  font-style: normal;
  padding: 0.1em 0.3em;
  position: relative;
  display: inline-block;
}

em::after {
  content: '';
  position: absolute;
  background: var(--gradient);
  inset: 0;
  z-index: -1;
  border-radius: 0.2em;
  transform: skewX(-10deg);
}

Attempt C
— 1 element, box-decoration-break: clone;

  • No skew - seems to be ignored on inline elements (in Webkit at least).
  • Multi line consistency
  • No multiline following text eject to next line.
  • If multiline, following lines can cover the decenders of previous line's characters.
  • Poor multiline left-to-right gradient background color; visible lines in most cases.

Test with a very short string of text.

Test with a much longer string of text that would wrap onto multiple lines to check for artifacting or other unintended consequences of the attempt's code.


em {
  font-style: normal;
  position: relative;
  display: inline;
  box-decoration-break: clone;
  padding: 0.1em 0.2em;
  border-radius: 0.2em;
  background: white;
  background: $g1;
  transform: skewX(-10deg);
  /* transform skew is ignored
    on inline elements
    (in Webkit at least). */
}	

Attempt D
— double wrapping elements

  • Avoids pseudo element z-index issues.
  • Don't know if double skewing elements causes subtle text rendering changes
  • If multiline, preceding and following text is ejected to the next line.

Some text with some skewed words in the middle.

Lorem ipsum dolor sit amet, consectetur adipisicing elit. Assumenda, eius consequuntur. Culpa possimus rem quibusdam odit facilis dolorum mollitia, voluptates sapiente dolor doloribus quos nam quasi, repellendus blanditiis saepe fuga.


em {
  display: inline-block;
  font-style: normal;
  padding: 0.1em 0.3em;
  border-radius: 0.2em;
  position: relative;
  background: white;
  background: $g1;
  transform: skewX(-10deg);
}

em span {
  display: inline-block;
  transform: skewX(10deg);
}

Attempt E
— double pseudo elements at ends

  • Poor multiline rendering. Pseudo elements don't always line up nicely. Can cause sharp corners or gaps.
  • If multiline, preceding and following text is ejected to the next line.

Test with a very short string of text.

Test with a much longer string of text that would wrap onto multiple lines to check for artifacting or other unintended consequences of the attempt's code.


em {
  font-style: normal;
  padding: 0.1em 0;
  margin-left: 0.5em;
  margin-right: 0.5em;
  position: relative;
  background: var(--gradient);
  display: inline-block;
}

em::before,
em::after {
  content: '';
  border-radius: 0.2em;
  background: white;
  display: inline-block;
  width: 1em;
  transform: skewX(-10deg);
  position: absolute;
  top: 0;
  bottom: 0;
  z-index: -1;
}

em::before {
  left: -0.3em;
  background: var(--color1);
}

em::after {
  right: -0.3em;
  background: var(--color2);
}
I originally wrote this on CodePen