7

I'm attempting to put CSS styles on the list items in the first line of a list but it seems that neither Chrome, Firefox, nor Safari will accept the style.

ul:first-line > li {
    display: inline;
    /* my styles here */
}

Have I overlooked the way in which I'm specifying the style, is this an oversight in CSS implementation or a deliberate CSS specification? If it is the latter, is there a good rationale behind this?

JSFiddle: http://jsfiddle.net/e3zzg/

Edit:
Please note, it seems pretty definitive that this can currently not be achieved using CSS alone but from a research standpoint and for posterity, I'm curious as to why this is. If you read the W3C CSS specification on the firstline pseudo-element there doesn't seem to be any mention of inner elements. Thanks to everyone trying to provide alternate solutions, but unless there actually is a CSS solution, the question here is 'why', not 'how' or 'is it possible'.

James Donnelly
  • 126,410
  • 34
  • 208
  • 218
Godwin
  • 9,739
  • 6
  • 40
  • 58

4 Answers4

9

Here's "Why" What You Want to Do Cannot Be Done

The selectors 3 spec is a little more up to date. The following is taken from that.

The "why" is because the :first-letter is pseudo-element, that is, a "fake" or "false" element. It is producing a "fictional tag sequence", which is not recognizable in relation to other real elements. So your...

ul:first-line > li 

...suffers from the same issues as this selector string...

ul:before + li

...where the combinator (whether > or +) is only looking at the "element" not the "pseudo-element" for selection. The second string does not target the "first" li of the ul that is following a :before pseudo-element. If it were to work at all, it would target an li that follows the ul in the html sequence (which, of course, there would never be one in a valid html layout).

However, a selector string similar to the second one above would not work anyway, because in actuality, the form of the above strings is not valid, as confirmed by the statement in the specifications that says:

Only one pseudo-element may appear per selector, and if present it must appear after the sequence of simple selectors that represents the subjects of the selector.

In other words, a pseudo-element can only be positioned dead last in the selector sequence, because it must be the target of the properties being assigned by that selector. Non valid forms apparently are simply ignored just like any invalid selector would be.

ScottS
  • 71,703
  • 13
  • 126
  • 146
  • Interesting, sort of makes sense. It's easy to see the `before` pseudo element as `
  • 1.First element
  • ` but I'm having a hard time seeing `first-line` in the same way when there are extra elements in there. – Godwin Jul 04 '13 at 16:54
  • I guess it could be imagined as `
    • LoremIpsum
    • dolor sit amet
    • `...
    – Godwin Jul 04 '13 at 17:02
  • See this for a similar concept, but in reverse: [Can I target an :after pseudo-element with a sibling combinator?](http://stackoverflow.com/questions/7735267/can-i-target-an-after-pseudo-element-with-a-sibling-combinator) – BoltClock Jul 04 '13 at 17:08
  • Yes & no. It's more complicated, for the `background-color` is like `
    • ...
    • etc...
    `, but does not recognize those real `
  • `'s as being in relation to it. What it is really relating to is the beginning and end of the "text" that happens to be in those `li` elements. So you can set your `color` property on the `ul:first-line` and the color of the _text_ in the `li` will pick it up by normal inheritance, but you cannot get the `background` of the `li` itself to inherit, because the `ul:first-line` background effects the _text_ not the `li`.
  • – ScottS Jul 04 '13 at 17:12