Last-word (faux pseudo class)

Because its quiet around here I thought I’d just throw this out for fun :slight_smile:

An old client just asked me if I could colour the last word in the heading of his site without changing the html. He wanted a quick fix as the html or content of the heading was not going to change.

I came up with one way of doing it but maybe you’d like to have a go on your own and see what you can come up with. Note that the html can wrap to multiple lines depending on screen size so the effect has to be maintained for wrapping lines and user font-size scaling

Simply (or not so simply) colour the last word only in this heading with css only.

<h1>Your test is to make the very last word in this heading to be a different colour</h1>

Here’s my attempt but don’t peak if you want to have a go yourself.

CSS has :first-letter but not :first-word or :last-word so don’t try looking for those:)

The solution is just for fun and not one to be used where content may change (that’s a hint ;)).

7 Likes

Too bad you can’t change the html just a hair…it’d be much more flexible…

3 Likes

After I made this I checked yours and it seems we have thought alike!

4 Likes

You’re all far too clever :slight_smile:

5 Likes

Too bad we couldn’t use EQCSS. Coulda come up with something cool there too :smiley:

2 Likes

I found a solution.
Do we still have the teaser format to post hidden code, so I don’t spoil for others.

Ok, I found the details tag

h1::after {
  content:"colour";
  color:red;
  margin-left:-3.058em;
}

I didn’t look at previous examples. :grin:

4 Likes

I get a partial overlap with that measurement in my Chrome Ray.

There is a more efficient way that doesn’t require any guesswork of fractions etc. :slight_smile:

1 Like

Hi Paul, yes I looked at the solution after I posted my attempt.

Mine was a bit magic numberish, It worked for my default font on FF. I realized it could be different elsewhere after I posted it.

3 Likes

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.