开发者

CSS selector involving pseudo class first-child and dropcap

I need to format HTML similar to below. Basically a quote is optional, and I need to dropcap the first letter of the body paragraph.

<article>
 <p class="quote"> <!-- quote is optional -->
   Genius begins great works; labor alone finishes them.-- Joseph Joubert
 </p>
 <p> <!-- "L" is a dropcap -->
  Life is like a box of chocolates.
 </p>
 <p>...</p>
 <p>...</p>
</article> 

My CSS looks l开发者_开发百科ike this:

article > p:first-child:first-letter
{
 float: left;
 font-family: Georgia, serif;
 font-size: 360%;
 line-height: 0.85em;
 margin-right: 0.05em;
}
p.quote {
 font-weight: bold;
}

It doesn't work currently when the quote is introduced. AFAIK I can't select the article's first child P which is not class "quote." I'll use jQuery if I can't figure this out, but for now I'm looking for a way to do it CSS only.

Thanks in advance!


If you're OK with using CSS3 selectors, try using these (grouped together):

/* Select the first child if it's not a .quote */
article > p:not(.quote):first-child:first-letter, 
/* Or, if the first child is a .quote, select the following one instead */
article > p.quote:first-child + p:first-letter
{
 float: left;
 font-family: Georgia, serif;
 font-size: 360%;
 line-height: 0.85em;
 margin-right: 0.05em;
}

See the jsFiddle demo

Otherwise I think you'll have to play with some overrides to get the desired effect.

Some explanation

The negation pseudo-class :not() is always processed independently of all other types, IDs, classes and pseudo-classes in the compound selector. This is regardless of how you arrange it with your other selectors.

To put it another way: you can't use :not() to remove, or filter out, elements that match what's in the negation, from a selection matched by the rest of the simple selector. It also means that the set of the elements matched by the :*-child() and :*-of-type() pseudo-classes is not affected by :not().

So the first selector above,

article > p:not(.quote):first-child:first-letter

works roughly like this:

  1. Find every p element.

    • If not found, ignore.

  2. If found, check whether this p is the :first-child and if it's :not(.quote).

    • If it's not the first child, ignore.
    • If it has the quote class, ignore.

  3. If it matches both pseudo-classes, check whether this p is a child of article.

    • If not, ignore.

  4. If so, grab its :first-letter.

  5. Apply rules.

The second selector, on the other hand, is relatively straightforward:

article > p.quote:first-child + p:first-letter

All it does is take the p that comes right after the first child of article if it's a p.quote, and apply rules to its :first-letter.


Why don't you use <q> or <blockquote> for the quote? then you can use

p:first-of-type:first-letter


 p.dropCap:first-child:first-letter {
float: left;
color: #903;
font-size: 75px;
line-height: 60px;
padding-top: 4px;
padding-right: 8px;
padding-left: 3px;
font-family: Georgia;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜