At the moment it is not possible, as we do not have a selector ::last-word
, or ::first-word
. Even in the Level 4 Selector Drafts there is no mention of this type of selector as you can see here: https://drafts.csswg.org/selectors-4/
If you want to try something with JS here is the plugin that might interest you. http://letteringjs.com/
And here’s an example with jQuery. It basically puts the last word inside a span and arrow the class
$(".lastWord").html(function(){
var text= $(this).text().trim().split(" ");
var last = text.pop();
return text.join(" ") + (text.length > 0 ? "(<span class='red'>" + last + "</span>)" : last);
});
.red {
color: red;
font-weight: bold;
}
<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
<div class="lastWord">jQuery is a fast, small, and feature-rich JavaScript library. It makes things like HTML document traversal and manipulation, event handling, animation, and Ajax much simpler with an easy-to-use API that works across a multitude of browsers. With a combination of versatility and extensibility, jQuery has changed the way that millions of people write JavaScript.</div>
Source of the code https://codepen.io/mel/pen/jLEKH
I believe this is not possible, you will need to use javascript.
– Marconi
It’s what I imagined at first... but I decided to share the doubt with the experts here rs
– Leandro Castro
For now, I don’t think you can. But CSS has been bringing more specific features to texts. Like, for example, the letter picker:
:nth-letter
, the word selector::nth-word
, etc. Take a look here: (TL-DR) CSS-Tricks 2019 Wishlist– LipESprY
Thanks for the recommendation. I’ll give a read.
– Leandro Castro