How to apply CSS to second word in a string?


Tags: css

Problem :

If I have the following string: John Smith, how could I use CSS to set font-weight: bold on the second word in order to achieve: John Smith.

Can this be done in pure CSS?

Update: I am retrieving user's name from the server, so in my template it is #{user.profile.name}.



Solution :

Since a js solution was suggested and pure CSS isn't presently possible: Live demo (click).

Sample markup:

<p class="bold-second-word">John Smith</p>
<p class="bold-second-word">This guy and stuff.</p>

JavaScript:

var toBold = document.getElementsByClassName('bold-second-word');
for (var i=0; i<toBold.length; ++i) {
  boldSecondWord(toBold[i]);
}

function boldSecondWord(elem) {
  elem.innerHTML = elem.textContent.replace(/\w+ (\w+)/, function(s, c) {
    return s.replace(c, '<b>'+c+'</b>');
  });
}

    CSS Howto..

    how to set width,height etc to particular table in html

    How to get elements by class and provide css effects?

    How to make button that changes grid dimensions using JS?

    How to give the color to * without using span Tag

    How to select odd child of dl element with css?

    How to see the source code of a particular html tag by onClick() javascript without showing its css?

    how to test css(3) style property value support, repeat value support?

    How do you create a html scrollable area, that just uses the main browser scrollbar

    How to keep CSS style If I change the html content dynamically?

    How to apply multiple css transform properties using Jquery

    How does CSS height get applied to input fields?

    How to create round plus minus using css?

    CSS column header with img - how to dock img/div to the right?

    CSS: How to put form input fields and button next to each other in the same line?

    How to style every odd p element after h2 element occured in css?

    How to change the css class name dynamically in angular 2

    How to get a tag the focus

    How can I make a DIV background change color on hover if the DIV has a class of “disabled”? [closed]

    How to make a vertical CSS menu with arrows on the right of each entry?

    Understanding how Flexbox works with Bootstrap

    Blogger Template: how to change the color of the border css

    How to add CSS tag to ruby on rail loop?

    How to highlight selected tab in Web Forms including Master Page

    How to change tinymce scrollbar color?

    How do I get this CSS to work in safari

    CSS: How to make background color around text not flush to the text?

    How to keep an :active css style after clicking a link?

    How can I force clients to refresh JavaScript/CSS files rendered with Jade in Node.js?

    How to remove this head css styling with javascript?

    How to get a property from other class in CSS?