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 can I make the menu to wrap when I resize my browser?

    How to create change image option on mousehover like in gmail account?

    How do I integrate a jQuery script in to my html file?

    How to style menu in CSS to look like a button

    How did Google make these 3d buttons? [closed]

    how to fix menu bar css-adapter stuffs with IE

    How to make input text bigger in bootstrap css framework?

    How to get parent DIV width and set child DIV width

    How to modify CSS or JS file from “materialize:materialize” package in Meteor

    How Do I Code a Popover in Rails 4.2.5 Using gem bootstrap-sass (Bootstrap 2)?

    How to move a div up and down infinitely in CSS3?

    How to convert this xpath to css?

    how to read the following css class

    How can I add image to facebook login button?

    How to apply class only for IE?

    How can I make the header's border/other content not overlap the img on the left?

    How to Set Up a Responsive Background-Image with CSS

    How do I add hover cursor and still get move cursor on JQuery Table Sortable?

    How to remap CSS shortcut key in Mac Bluefish text editor

    How to make my .css and .js file work with my html code loaded in a WebView on Android

    How to make this in html and css?

    How to change the size of an image inside a div without changing the div size

    How to change 'inherited' font families using the CSS style sheet

    How to add css to a HTML div which has no class or id

    How to change CSS property in Magnific Popup

    How to transform a production to LL(1) for a list separated by a semicolon?

    how to place div next to centered div css

    How to make a unique line thinner using NVD3

    how to position heading in the center of the page

    How to combine the two same css selector into one?