10

I have phrase as Client Testimonial and i want to change only the Client i have used only first-letter but is there any method in css to change color .. no javascript please.

BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
Rafee
  • 3,975
  • 8
  • 58
  • 88
  • 2
    There is no :first-word pseudo-element in CSS so it's not possible (without, like you dont want, javascript). See http://stackoverflow.com/questions/55612/css-to-increase-size-of-first-word for reference. – Henrik Ammer Aug 31 '12 at 15:34
  • In case you change your mind concerning the use of js: http://www.dynamicsitesolutions.com/javascript/first-word-selector/ – Kai Mattern Aug 31 '12 at 15:36
  • Thanks!, then i need to use javascript. – Rafee Aug 31 '12 at 15:43

5 Answers5

14

How about using :before?

I would change the text from "Client Testimonial" to "Testimonial", and then with CSS apply the :before rule:

HTML:

<div class="word">Testimonial</div>​​​​​​​​

CSS:

.word {
 color: black;       
}
.word:before {
 color: red;
 content: "Client ";
}

Example: http://jsfiddle.net/LvZt7/

jackJoe
  • 11,078
  • 8
  • 49
  • 64
  • 9
    While this is viable in terms of the way the page visually appears, it is TERRIBLE for accessibility. Most screen readers skip over CSS generated content (http://cssgallery.info/testing-the-accessibility-of-the-css-generated-content/). In addition, this violates the concept of separating content/structure from formatting. – Kevin Finkenbinder Feb 15 '16 at 16:02
10

As noted by others, there is (unfortunately*) no :first-word pseudo-selector available in CSS (even version 3 or 4, so far as I currently know). However, there are two possibilities that exist without JavaScript, though both have their failings.

The first, and easiest, is to simply wrap the first word in a span:

<p><span>Client</span> Testimonial</p>​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​

And style the span with the highlight:

p span {
    color: #f90;
}

JS Fiddle demo.

While this approach does require adding an extra, in this case, span element for styling purposes it is simple to implement, and works reliably cross-browser.

The second is slightly more fragile, though avoids adding the extraneous span tag, but requires, instead, that you add an attribute:

<p data-highlightword="Client">Client Testimonial</p>​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​

With the following CSS:

p[data-highlightword] {
    position: relative;
}

p[data-highlightword]::before {
    content: attr(data-highlightword);
    color: #f90;
    position: absolute;
    top: 0;
    left: 0;
}​

JS Fiddle demo.

This approach relies on the addition of a single attribute to a single element, but does require extra CSS and will only work in compliant browsers. Which is almost all of them, now, with only IE 8, or perhaps 9, and below proving problematic.

David Thomas
  • 249,100
  • 51
  • 377
  • 410
  • what about browser support issues using data-hihglightword or :before? for :before I found at least this http://css-tricks.com/browser-support-pseudo-elements/ IMHO I would use basic "stuff" for easier solutions which most likely work with higher amount of older browsers :) but definitely interesting styles you shared even for me hehe. – Mauno Vähä Aug 31 '12 at 15:57
  • The browser support issue is the precise reason for the final paragraph in the answer. Perhaps I didn't raise the issue sufficiently, but, honestly, I'm becoming, as time goes on, more and more jaded by the need to support IE. I *will* support IE, and happily, but I've become somewhat inured to the fact of its (previous) complacency as regards standards. Now, IE 10, on the other hand actually excites me... – David Thomas Aug 31 '12 at 16:00
  • 1
    ok, thnx for explanation - and there are rarely any developers who gets excited because of IE :) – Mauno Vähä Aug 31 '12 at 16:06
3
<p><span style="color: #c0ff33;">Client</span> Testimonial</p>

so yes, just style it with <span></span> its perfect for that kind of situations.

Edit:

This edit is not directed for the post author but for someone just learning to use css: based on "best practises" one should consider using separate .css file for setting styles in a manner like:

.client {
   color: #c0ff33;
}

and using it like:

<p><span class="client">Client</span> Testimonial</p>

If you want to specify more and be certain that you only use your span style inside <p></p> you could also introduce it like:

p span.client {
   color: #c0ff33;
}

Fiddle: http://jsfiddle.net/LvZt7/97/

You could also do it other way around specifying your p class and not span:

<p class="client"><span>Client</span> Testimonial</p>

and

p.client span {
   color: #c0ff33;
}

or just specifying all p span html markings to have text inside span with color #c0ff33:

<p><span>Client</span> Testimonial</p>

and

p span {
   color: #c0ff33;
}
Mauno Vähä
  • 9,688
  • 3
  • 33
  • 54
0

You could wrap the word in a span and style it instead. As Henrik Ammer stated in a comment, there is no :first-word.

Corey Ogburn
  • 24,072
  • 31
  • 113
  • 188
0

i'd recommend doing something like this:

<span class = "redcolor">Client </span> Testimonial

CSS:

.redcolor
{
    color: red
}

That way if you want anything in red, just give it a div/span with that class

tehdoommarine
  • 1,868
  • 3
  • 18
  • 31