views:

73

answers:

6

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.

+7  A: 

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.

Dave Markle
+3  A: 

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.

jao
Automagically is my favourite new word of the day! :-)
Greg Hluska
+1  A: 

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

Kaaviar
A: 

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

Sarfraz
+1  A: 

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/>');
Andreas Niedermair
You should include your code snippet inline, instead of referencing http://jsfiddle.net/ -- it will make it easier for other to search for your code here. Not that JsFiddle isn't useful (it is), but your answer is small enough that it makes sense to include it here.
Craig Trader
A: 

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.

Scott