It would be quite useful to have a previous element selector (equivalent of element + next element
for previous child) and there are others who want this/have asked if it is available too. When an element that can’t easily be selected comes before an easily selected element, it needs a selection method, a previous selector. Sometimes this scenario can be reorganised to use next element selectors, but when it can’t there will be problems.
Syntax idea
Since parent element > child element
is used to select child elements with a specified parent and preceding element(s) ~ element
is used to select elements that are preceded by a specified element, using element < previous element
would be a bad format as because of the >
use it could be confused with referring to the parent element.
So I think a good format would be element - previous element
as it doesn’t seem to be in use and relates to use of the +
symbol for “next”.
Example
HTML:
<div>
<div>The item with no easy selection method that comes before.</div>
<div data-important-thing>Something important that can be selected.</div>
</div>
Pseudo-CSS (idea):
//div[data-important-thing] <some selector> div
div[data-important-thing] - div {color: red};