Tutorial :Is there a cross-browser css way to decorate only last word in a span block?



Question:

I need to show user's list which should look like in the example below:

Helen Burns
Edward Fairfax Rochester
Bertha Antoinetta Mason
Adèle Varens

Is there a way to achieve this without using javascript? Each row should be one span, i.e. <span>Helen</span><span>Burns</span> is not acceptable.


Solution:1

No, there is not. You are going to have to use some form of scripting to accomplish this if you don't want your last names to be in their own tags.

To the browser, each row is an element, and the "words" themselves have no separate meaning as far as CSS is concerned. You must place the words in different tags in order to do what you want.


Solution:2

The browser does not automagically know what part of the name is the last name so you have to add extra markup to achieve what you want.


Solution:3

There's no solution for common used browser for know using only CSS. You should use javascript or HTML + CSS as you already made.


Solution:4

without pure css this is impossible (as you don't want a separation in the markup)...

<span>Monty Burns</span><br />  <span>Bart Simpson</span>  <script type="text/javascript">  $(document).ready(function() {      var spans = $('span');      spans.each(function(index, element) {          var span = $(element);          var spanText = span.text();          var spanTextArray = spanText.split(' ');          var spanTextArrayLength = spanTextArray.length;          var lastName = spanTextArray[spanTextArrayLength -1];          spanTextArray.pop();          var firstName = spanTextArray.join(' ');          span.text(firstName);          var spanLastName = $('<span/>');          spanLastName.css('font-weight', 'bold');          spanLastName.css('margin-left', '5px');          spanLastName.appendTo(span);          spanLastName.text(lastName);      });  });  </script>  

working demo.

edit: if you do not want an extra span-tag in there, just change

var spanLastName = $('<span/>');  spanLastName.css('font-weight', 'bold');  

to

var spanLastName = $('<strong/>');  


Solution:5

I don't think this is possible with CSS because your example doesn't show any order:

Helen Burns
Edward Fairfax Rochester
Bertha Antoinetta Mason
Adèle Varens


Solution:6

I don't know why you might desire not to have an extra tag surrounding the last name (as other answers and comments have suggested), but if you are looking simply for minimalist mark-up, this works (no span even used):

Html:

 <body>      First Middle <strong>Last</strong>      First Middle <strong>Last</strong>      First Middle <strong>Last</strong>   </body>  

Css:

strong:after {content:' '; display: block;}  

Which creates "rows" with your desired styling without anything more than a single tag (which could be a span rather than a strong if you desired).

Edit: Of course, this will not work for IE7 or under.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »