How to highlight last html-element with specific c

2019-09-21 19:42发布

问题:

How to highlight last div with class="block" in the following html-structure, if the number of div with class="block" is unknown? (With help of css-selectors)

http://codepen.io/Feel_codepen/pen/Ifjzd

There, for example, I can highlight third div.

回答1:

You can try this using :last-of-type

div.block:last-of-type{
 color:yellow;
}

CodepenDemo



回答2:

Use last-child:

.main .block:last-child{
    background-color:#FCC;
    /* Your styles*/
}

CodePen



回答3:

You could use the :last-child selector.

More information could be found at http://css-tricks.com/almanac/selectors/l/last-child/

.main .block:last-child {
  background-color: yellow;
}

http://codepen.io/anon/pen/vpqad



回答4:

By using the last-child pseudo class

.block:last-child{
   background-color: yellow; /* or some other color */
}

http://www.w3schools.com/cssref/sel_last-child.asp



回答5:

Try this

.block:last-child
{
background-color:blue;
} 

DEMO



回答6:

And Magic!

.main>div[class=block]:last-child{
  background-color:blue;
}

this should do it :P i hope



回答7:

Try to use

$("div:last-child").addClass("last-child");

With this you can add a css class to the last child of a certain div or element