Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS for changing color of last word in h1

Tags:

html

css

colors

I have a website that I am developing using CSS3 and I have h1 tag for the title:

<h1>main title</h1> 

Now I want the title to be in a different color:

<h1>main <span>title</span></h1> 

So I do:

h1 {  color: #ddd; } h1 span {  color: #333; } 

Is there a way not to use the span tag and only specify in CSS the last word to be a different color?

like image 800
Alexei Avatar asked Feb 13 '12 21:02

Alexei


People also ask

How can I change the color of h1 tag in CSS?

The code snippet below shows the syntax of using the color property to set the color of the <h1> element. The value of the color property can be specified in three ways: Using RGB values e.g. rgb(250,0,0), rgb(0,250,0), etc. Using a Hexadecimal(HEX) value e.g. #FFFFFF, #000000, etc.

How do you make words different colors in CSS?

Simply add the appropriate CSS selector and define the color property with the value you want. For example, say you want to change the color of all paragraphs on your site to navy. Then you'd add p {color: #000080; } to the head section of your HTML file.

What is the CSS property to change the color of the text?

Text ColorThe color property is used to set the color of the text. The color is specified by: a color name - like "red" a HEX value - like "#ff0000"


1 Answers

This is not possible with pure CSS. However you can use lettering.js to get a ::last-word selector. CSS-Tricks has an excelent article on this: CSS-Tricks: A call for nth-everything. You can then do the following:

h1 {   color: #f00; }  /* EDIT: Needs lettering.js. Please read the complete post,  * before downvoting. Instead vote up. Thank you :)  */ h1::last-word {   color: #00f; } 
like image 140
iblue Avatar answered Sep 22 '22 16:09

iblue