CSS to select/style first word

后端 未结 11 1727
礼貌的吻别
礼貌的吻别 2020-11-22 13:30

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

相关标签:
11条回答
  • 2020-11-22 13:47

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

    0 讨论(0)
  • 2020-11-22 13:52

    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.

    0 讨论(0)
  • 2020-11-22 13:55

    Pure CSS solution:

    Use the :first-line pseudo-class.

    display:block;
    Width:40-100px; /* just enough for one word, depends on font size */
    Overflow:visible; /* so longer words don't get clipped.*/
    float:left; /* so it will flow with the paragraph. */
    position:relative; /* for typeset adjustments. */
    

    Didn't test that. Pretty sure it will work fine for you tho. I've applied block rules to pseudo-classes before. You might be stuck with a fixed width for every first word, so text-align:center; and give it a nice background or something to deal with the negative space.

    Hope that works for you. :)

    -Motekye

    0 讨论(0)
  • 2020-11-22 14:01

    Here's a bit of JavaScript and jQuery I threw together to wrap the first word of each paragraph with a <span> tag.

    $(function() {
        $('#content p').each(function() {
            var text = this.innerHTML;
            var firstSpaceIndex = text.indexOf(" ");
            if (firstSpaceIndex > 0) {
                var substrBefore = text.substring(0,firstSpaceIndex);
                var substrAfter = text.substring(firstSpaceIndex, text.length)
                var newText = '<span class="firstWord">' + substrBefore + '</span>' + substrAfter;
                this.innerHTML = newText;
            } else {
                this.innerHTML = '<span class="firstWord">' + text + '</span>';
            }
        });
    });
    

    You can then use CSS to create a style for .firstWord.

    It's not perfect, as it doesn't account for every type of whitespace; however, I'm sure it could accomplish what you're after with a few tweaks.

    Keep in mind that this code will only execute after page load, so it may take a split second to see the effect.

    0 讨论(0)
  • 2020-11-22 14:02

    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;
    }
    
    0 讨论(0)
  • 2020-11-22 14:04

    What you are looking for is a pseudo-element 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/

    0 讨论(0)
提交回复
热议问题