admin管理员组

文章数量:1397031

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.

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.

Share Improve this question asked Jul 20, 2010 at 13:21 RomanRoman 66.2k93 gold badges244 silver badges339 bronze badges 4
  • What about JS that changing the HTML structure (wrapping last words with <span>) at runtime? – Makram Saleh Commented Jul 20, 2010 at 13:26
  • Would <span>Helen <span>Burns</span></span> be acceptable? Otherwise there's no :last-word CSS selector. – jasongetsdown Commented Jul 20, 2010 at 13:28
  • 1 You need to be more specific, I think. This could easily be done by doing <span>Helen <strong>Burns</strong></span>, but something tells me you may be trying to do something else. – davidcelis Commented Jul 20, 2010 at 13:29
  • What if someone has two last names? you can't select only the last word, there has to be some sort of semantics. – jao Commented Jul 21, 2010 at 5:48
Add a ment  | 

6 Answers 6

Reset to default 8

No, there is not. You are going to have to use some form of scripting to acplish 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.

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.

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

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/>');

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

I don't know why you might desire not to have an extra tag surrounding the last name (as other answers and ments 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.

本文标签: javascriptIs there a crossbrowser css way to decorate only last word in a span blockStack Overflow