DEV Community

Cover image for Making CSS perspective text
Chris Bongers
Chris Bongers

Posted on β€’ Originally published at daily-dev-tips.com

15 2

Making CSS perspective text

Today I wanted to create a fun CSS effect and try and figure out how it's done.

Today we are making CSS perspective text to make text look like stairs.
In essence, we will use some skew transforms and position offsets to create this effect.

The end result for today:

HTML Structure

As for the HTML goes, we don't need that much fancy stuff.

<h1>
  <span>Always</span>
  <span>deliver</span>
  <span>quality</span>
</h1>
Enter fullscreen mode Exit fullscreen mode

In my case, I want to have three words to use as our effect.
You could alter the codebase to work with paragraphs as well.

CSS Perspective text

Now on to the magic element, CSS.
We'll start by styling the main h1 element. The goal is to make it look sans-serif and quite big.
This works best for this effect.

h1 {
  font-family: Helvetica, Arial, sans-serif;
  font-size: 70px;
  font-weight: 900;
  text-transform: uppercase;
  position: relative;
  margin-left: -350px;
  margin-top: -150px;
}
Enter fullscreen mode Exit fullscreen mode

As you can see, I use quite a big font and transform everything too uppercase.
Then I offset the whole element. Since we will be using absolute positions, it's hard to position the whole element at once.
You might have to change these positions based on the words you want to use.

Then each span element inside our h1 should be absolute.

h1 {
  span {
      position: absolute;
  }
}
Enter fullscreen mode Exit fullscreen mode

The next rule will be to make the step skew in opposite directions. I'm using an odd/even child pseudo-selector.

h1 {
  span {
    &:nth-child(odd) {
      transform: skew(60deg, -30deg) scaleY(0.66667);
    }
    &:nth-child(even) {
      transform: skew(0deg, -30deg) scaleY(1.33333);
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

That brings us very close, but all the elements are on top of each other now.
We need to modify the offset position for every second and above element.

With my words, that comes down to the following:

h1 {
  span {
    &:nth-child(2) {
      left: 27px;
      top: 52px;
    }
    &:nth-child(3) {
      left: 54px;
      top: 105px;
    }
    }
}
Enter fullscreen mode Exit fullscreen mode

And that is it. You now have this super cool stair-looking perspective text.

CSS Perspective text

Thank you for reading, and let's connect!

Thank you for reading my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter

Neon image

Build better on Postgres with AI-Assisted Development Practices

Compare top AI coding tools like Cursor and Windsurf with Neon's database integration. Generate synthetic data and manage databases with natural language.

Read more β†’

Top comments (2)

Collapse
 
alonsoaguayo profile image
Alonso Aguayo β€’

Great effect, and simple to implement!

Collapse
 
dailydevtips1 profile image
Chris Bongers β€’

Thank you Alonso!

Image of Stellar post

Check out Episode 1: How a Hackathon Project Became a Web3 Startup πŸš€

Ever wondered what it takes to build a web3 startup from scratch? In the Stellar Dev Diaries series, we follow the journey of a team of developers building on the Stellar Network as they go from hackathon win to getting funded and launching on mainnet.

Read more

πŸ‘‹ Kindness is contagious

Explore a trove of insights in this engaging article, celebrated within our welcoming DEV Community. Developers from every background are invited to join and enhance our shared wisdom.

A genuine "thank you" can truly uplift someone’s day. Feel free to express your gratitude in the comments below!

On DEV, our collective exchange of knowledge lightens the road ahead and strengthens our community bonds. Found something valuable here? A small thank you to the author can make a big difference.

Okay