Reputation: 151036
Is it possible to use CSS only, to have a solution that works even on IE8, that
<ul>
list has 2 or more items, then keep the bullets in front of the items as usual.An example is shown on http://jsfiddle.net/SzQL6/37/
<input id="an-input">
<ul id="the-list">
<li>error msg 1</li>
<li>error msg 2</li>
</ul>
(The error messages should be left-aligned with the left edge of the input box above the error messages, when there are bullets and when there is no bullet.)
Using JavaScript, I could do it: http://jsfiddle.net/SzQL6/40/
But can this be done by CSS only (and that it can work on IE8)?
Upvotes: 1
Views: 290
Reputation: 5156
Yes, you can do it with CSS only, though it is only compatible with CSS3 (which anybody with a updated browser will have, or as Guffa pointed out, IE >= 9), by using the :only-child
pseudo-class selector:
.error-list { list-style-position: inside; margin-top: 0; color: #f77 }
.error-list li:only-child { list-style-type: none; }
The rule is being applied to any li
element that is the only child of its parent (.error-list
).
According to MDN:
The
:only-child
CSS pseudo-class represents any element which is the only child of its parent. This is the same as:first-child:last-child
or:nth-child(1):nth-last-child(1)
, but with a lower specificity.
See the example on this fiddle: http://jsfiddle.net/SzQL6/41/
Upvotes: 3