CSS to select/style first word
CssCss Problem Overview
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?
Css Solutions
Solution 1 - Css
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/
Solution 2 - Css
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">
.
Solution 3 - Css
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)
Solution 4 - Css
Sadly even with the likes of CSS 3 we still do not have the likes of :first-word
:last-word
etc using pure CSS. Thankfully there's almost a JavaScript nowadays for everything which brings me to my recommendation. Using nthEverything and jQuery you can expand from the traditional Pseudo elements.
Currently the valid Pseudos are:
:first-child
:first-of-type
:only-child
:last-child
:last-of-type
:only-of-type
:nth-child
:nth-of-type
:nth-last-child
:nth-last-of-type
And using nth Everything we can expand this to:
::first-letter
::first-line
::first-word
::last-letter
::last-line
::last-word
::nth-letter
::nth-line
::nth-word
::nth-last-letter
::nth-last-line
::nth-last-word
Solution 5 - Css
You have to wrap the word in a span to accomplish this.
Solution 6 - Css
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
Solution 7 - Css
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;
}
Solution 8 - Css
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.
Solution 9 - Css
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.
Solution 10 - Css
An easy way to do with HTML+CSS:
TEXT A <b>text b</b>
<h1>text b</h1>
<style>
h1 { /* the css style */}
h1:before {content:"text A (p.e.first word) with different style";
display:"inline";/* the different css style */}
</style>
Solution 11 - Css
You can select first Letter or Line
p::first-letter {
font-weight: bold;
color: red;
}
Solution 12 - Css
I find JavaScript being the best way to achieve this. Below is the JS code to set an element's First Word as the element's innerText
let text = document.querySelector('.menu_text');
const words = menu_text.innerHTML.toString().split(' ');
text.innerText = words[0];
Solution 13 - Css
Insert Span Tag in your paragraph text. For Example-
<p><span>Hello</span>My Name Is Dot</p
and then style the first letter.