Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS wildcards with :first-child

I have an unwieldy chunk of css that I am using to set the margin-top of the first child of an element. The first child can be any tag.

.comment-description  p:first-child,
.comment-description  ol:first-child,
.comment-description  ul:first-child,
.comment-description  pre:first-child,
.comment-description  blockquote:first-child
{
    margin-top:0px;    
}

I'm sure that I can chop this down, but since I don't get to design too often, I can't remember a better way. Can I use something like:

.comment-description *:first-child
{
    margin-top:0px;    
}

Unfortunately this doesn't work.

like image 920
Kev Avatar asked Dec 05 '25 06:12

Kev


2 Answers

You may be interested in:

.comment-description > :first-child {} - select only immediate children

or

.comment-description :first-child - select first child of all children elements

See:

http://jsfiddle.net/9VqsW/1/

like image 112
Rob W Avatar answered Dec 07 '25 21:12

Rob W


To clarify things a little:

  • .element selector - selects all descendants that match selector. It doesn't matter if selector is a class, pseudo-class or ID.
  • .element > selector - selects only on the direct children that match selector

It looks like you want:

.comment-description > :first-child{
   ....
}
like image 34
nice ass Avatar answered Dec 07 '25 23:12

nice ass



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!