Combining CSS Pseudo-Elements, ":After" the ":Last-Child"

Is it possible to combine the css3 pseudo classes :after and :lastchild?

Try this:

HTML:

<ul>
<li><a href="#">Check this out</a></li>
<li><a href="#">Check this out</a></li>
<li><a href="#">Check this out</a></li>
</ul>


CSS:

a:after {content: "what what"}
li:last-child > a:after {content: ""}​

Here is a Fiddle to demonstrate.

Also, keep in mind, if you have many users that use IE7 and IE8, the :after pseudo class does not work in IE7 and below, and the :last-child pseudo class does not work in IE8 and below. See here: http://www.quirksmode.org/css/contents.html

Can I have multiple :before pseudo-elements for the same element?

In CSS2.1, an element can only have at most one of any kind of pseudo-element at any time. (This means an element can have both a :before and an :after pseudo-element — it just cannot have more than one of each kind.)

As a result, when you have multiple :before rules matching the same element, they will all cascade and apply to a single :before pseudo-element, as with a normal element. In your example, the end result looks like this:

.circle.now:before {
content: "Now";
font-size: 19px;
color: black;
}

As you can see, only the content declaration that has highest precedence (as mentioned, the one that comes last) will take effect — the rest of the declarations are discarded, as is the case with any other CSS property.

This behavior is described in the Selectors section of CSS2.1:

Pseudo-elements behave just like real elements in CSS with the exceptions described below and elsewhere.

This implies that selectors with pseudo-elements work just like selectors for normal elements. It also means the cascade should work the same way. Strangely, CSS2.1 appears to be the only reference; neither css3-selectors nor css3-cascade mention this at all, and it remains to be seen whether it will be clarified in a future specification.

If an element can match more than one selector with the same pseudo-element, and you want all of them to apply somehow, you will need to create additional CSS rules with combined selectors so that you can specify exactly what the browser should do in those cases. I can't provide a complete example including the content property here, since it's not clear for instance whether the symbol or the text should come first. But the selector you need for this combined rule is either .circle.now:before or .now.circle:before — whichever selector you choose is personal preference as both selectors are equivalent, it's only the value of the content property that you will need to define yourself.

If you still need a concrete example, see my answer to this similar question.

The legacy css3-content specification contains a section on inserting multiple ::before and ::after pseudo-elements using a notation that's compatible with the CSS2.1 cascade, but note that that particular document is obsolete — it hasn't been updated since 2003, and no one has implemented that feature in the past decade. The good news is that the abandoned document is actively undergoing a rewrite in the guise of css-content-3 and css-pseudo-4. The bad news is that the multiple pseudo-elements feature is nowhere to be found in either specification, presumably owing, again, to lack of implementer interest.

How could I use pseudo-element :after :before conditionally

In general it is not possible to select elements based on their next sibling.

In your specific case, you can use ul li a:not(:last-child)::after, because it happens that your anchors that are not followed by an <ul> element are also the last child element.

CSS Using :after and :last-child together

You should use this css for the label of last child:
li:last-child input[name='happy-score'] + label:after {
left: 0px;
}

css :nth-child() :after

You can, but you are doing it wrong..

The issue that that all your p elements are inside li. So all of them are the first child of their li container.

You will need to put the nth-child on the li elements.

#id li:nth-child(1) p:after,
#id li:nth-child(2) p:after,
#id li:nth-child(3) p:after {
content: 'OM';
color: pink;
}

#id li:nth-child(4) p:after,
#id li:nth-child(5) p:after,
#id li:nth-child(6) p:after {
content: 'Nom';
color: blue;
}

Quoting the W3C documentation

The :nth-child(an+b) pseudo-class notation represents an element that has an+b-1 siblings before it in the document tree, for any positive integer or zero value of n, and has a parent element.


Update 1

You could also simplify this by using

#id li:nth-child(-n+3) p:after {
content: 'OM';
color: pink;
}

#id li:nth-last-child(-n+3) p:after { /*this means last 3*/
content: 'Nom';
color: blue;
}

Demo at http://jsfiddle.net/gaby/4H4AS/2/


Update 2

If you want the first six only to be different (and not first 3 and last 3) you could

#id li:nth-child(-n+6) p:after { /*this means first 6*/
content: 'Nom';
color: blue;
}

#id li:nth-child(-n+3) p:after {/*this means first 3 and since it comes second it has precedence over the previous for the common elements*/
content: 'OM';
color: pink;
}

Demo at http://jsfiddle.net/gaby/4H4AS/3/

CSS selector for other than the first child and last child

Try:

#navigation ul li:not(:first-child):not(:last-child) {
...
}

How to use pseudo selectors in material-ui?

After a while fighting to have your code up and running I found what is wrong with your code.

Everything seems to be fine, the selector for rootListItem works right out of the box, the problem is that you can not use the pseudo-selector :hover on an element that has display: none. Instead you should be using opacity: 0 and opacity: 1, it will hide your ListItemSecondaryAction but at the same time it will allow you to hover. So, elements with display: none, doesn't technically display and thereby, you cannot hover them.

About your pseudo selector in global, you just wrote it wrongly. Using colon instead of dot after div and changing backgroundColor to 'yellow' instead of "'yellow',"

'li > div:nth-of-type(1)': {
display: 'block !important',
backgroundColor: 'yellow',
},

I didn't know how does your TreeMenu look like as a component, so I just created a list with ul / li / div nodes.

const styles = {
root: {
backgroundColor: 'white',
'&:hover': {
backgroundColor: '#99f',
},
},
hoverEle: {
visibility: 'hidden',
'&:hover': {
visibility: 'inherit',
},
},
rootListItem: {
backgroundColor: 'white',
opacity: 0,
'&:hover': {
opacity: 1,
backgroundColor: '#99f',
},
},
'@global': {
'li > div:nth-of-type(1)': {
display: 'block !important',
backgroundColor: "yellow",
},
},
};

And:

<div>
{treeNode.map(node => (
<ListItem
key={`${node.Type}|${node.NodeID}`}
id={`${node.Type}|${node.NodeID}`}
className={classes.root}
button
divider
disableGutters={false}
dense
onClick={() => {}}
title={''}
onMouseOver={() => {}}
>
<ListItemText primary={node.NodeName} />
<ListItemSecondaryAction classes={{ root: classes.rootListItem }}>
<ul><li><div>Elem 1</div></li><li><div>Elem 2</div></li></ul>
</ListItemSecondaryAction>
<div className={classes.hoverEle}>
<ul><li><div>Elem 1</div></li><li><div>Elem 2</div></li></ul>
</div>
</ListItem>
))}
</div>

*I am using treeNode that is an array for me and I removed the rest of the functions and TreeMenu.

Using :not(:last-child):after pseudo elements for each row inside an unordered list

What about something like this --> https://jsbin.com/mereqex/edit?html,css,output

CSS:

ul {
padding-left: 0;
margin-left: 0;
text-align: center;
}

li {
display: inline;
}

.lineup-list li:not(:first-child):before {
content: " \B7 ";
}

.lineup-list li:nth-child(3n):before {
content: "\A";
white-space: pre;
}

HTML:

<ul class="lineup-list">
<li>Amazing Band</li>
<li>Great Band</li>
<li>Great Band</li>
<li>Great Band</li>
<li>Cool Band</li>
<li>Nice Band</li>
<li>Cool Band</li>
</ul>

CSS :not(:last-child):after selector

If it's a problem with the not selector, you can set all of them and override the last one

li:after
{
content: ' |';
}
li:last-child:after
{
content: '';
}

or if you can use before, no need for last-child

li+li:before
{
content: '| ';
}


Related Topics



Leave a reply



Submit