tags:

views:

4887

answers:

7

This one has me kind of stumped. I want to make the first word of all the paragraphs in my #content div at 14pt instead of the default for the paragraphs (12pt). Is there a way to do this in straight CSS or am I left wrapping the first word in a span to accomplish this?

+2  A: 

You have to wrap the word in a span to accomplish this.

hazzen
+9  A: 

What you are looking for is a pseudoclass that doesn't exist. There is :first-letter and :first-line, but no :first-word.

You can of course do this with JavaScript. Here's some code I found that does this: http://www.dynamicsitesolutions.com/javascript/first-word-selector/

Robby Slaughter
Why wouldn't they have specified a :first-word in the spec?
alex
Probably because a "word" is not a well-defined concept in a layout language. Is "half-baked" one word or two? What about "McDonald" and "O'Shea"? What's the first word in "5 + 3 = 8" or "5+3=8"?
Robby Slaughter
+4  A: 

Use the strong element, that is it's purpose:

<div id="content">
    <p><strong>First Word</strong> rest of paragraph.</p>
</div>

Then create a style for it in your style sheet.

#content p strong
{
    font-size: 14pt;
}
Dale Ragan
It's ironic that your "First Word" is actually 2 words! ;)
Bobby Jack
Yeah, I thought the same thing when I was writing the answer.
Dale Ragan
Except strong is semantically incorrect in this case. It appears that the sizing is just a styling issue, and strong indicates that the affected section is to be stressed upon.
Akoi Meexx
Pluz, if you want to use strong anywhere else in the content it will be made bigger. Best to class that bad boy and target more directly.
Adrian Lynch
A: 

There isn't a plain CSS method for this. You might have to go with JavaScript + Regex to pop in a span.

Ideally, there would be a pseudo-element for first-word, but you're out of luck as that doesn't appear to work. We do have :first-letter and :first-line.

You might be able to use a combination of :after or :before to get at it without using a span.

Rob Allen
+7  A: 

I have to disagree with Dale... The strong element is actually the wrong element to use, implying something about the meaning, use, or emphasis of the content while you are simply intending to provide style to the element.

Ideally you would be able to accomplish this with a pseudo-class and your stylesheet, but as that is not possible you should make your markup semantically correct and use <span class="first-word">.

Prestaul
The javascript answer accepted as correct is potentially more useful, but I believe this one to be the most semantically correct.
Reynolds
A: 

You can use PHP (or other server-side scripting) too.

rolfen
+1  A: 

Same thing, with jQuery:

$('#links a').each(function(){
    var me = $(this);
    me.html( me.text().replace(/(^\w+)/,'<strong>$1</strong>') );
  });

or

$('#links a').each(function(){
    var me = $(this)
       , t = me.text().split(' ');
    me.html( '<strong>'+t.shift()+'</strong> '+t.join(' ') );
  });

(Via 'Wizzud' on the jQuery Mailing List)

Jon Hadley