You should not be lost! Like I said The first three need a different top margin. the third <li> of every row has no right margin (which I solved with my server side scripting) which is working fine the way I have it.
I have it solved by giving the <ul> the negative top margin needed, but I really would like to know how to target those three first <li’s> in the structure given!
<div class="content">
<img class="yiannis" src="http://www.sitepoint.com/forums/images/site/photos/yiannis.jpg" alt="" />
<p>Yiannis Tsapatori is one of the most talented and renowned hair ....</p>
<p>His magical hands have transformed a range of known and unknown...</p>
<p>Yiannis was born a hairdresser and was......</p>
<p>With his weapons... </p>
<p>Yiannis Tsapatori now lives and works in New York.</p>
</div>
Note again: the content is coming from the database! what you see as html is what I took from the uploaded page. Nether the top-margin or the font-size is changing?
Edit: I tried .content p:first-child, .content img p.first and .content img p:first-child as well
p.first is a class of .first attached to the p element and not a pseudo class. You don’t have an element with a class of .first so it will never be matched anyway
Here is an example that clearly shows first-child working in IE7 and above:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<style type="text/css">
.content{
background:red;
padding:1px 0;
}
.content p {
margin: 15px 0 0;
font-size: 90%;
}
.content p:first-child {
margin-top: 0;
font-size: 110%;
}
</style>
</head>
<body>
<div class="content">
<p>Yiannis Tsapatori is one of the most talented and renowned hair ....</p>
<p>His magical hands have transformed a range of known and unknown...</p>
<p>Yiannis was born a hairdresser and was......</p>
<p>With his weapons... </p>
<p>Yiannis Tsapatori now lives and works in New York.</p>
</div>
</body>
</html>
Another option to consider would be .content p:first-of-type, which matches the first paragraph within an element of class=“content”, even if there are other things (like images) that come before it.
The downside? Yup, you’ve guessed it, our old friend IE
It doesn’t work in IE8 or below. The compat chart doesn’t show IE9 yet so I can’t say whether it will work there. Apart from that, it’s fine and funky in Firefox 3.5, Safari 3.1, Opera 9.5 and Chrome 2.0.
Thanks, I meant to say something about that but got distracted
The other alternative which was mentioned earlier in the thread would be to do this:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<style type="text/css">
.content{
background:red;
padding:1px 0;
}
.content p {
margin-top: 0;
font-size: 110%;
}
.content p + p {
margin: 15px 0 0;
font-size: 90%;
}
</style>
</head>
<body>
<div class="content">
<div><img src="img.gif" alt="" width="500" height="10" /></div>
<p>Yiannis Tsapatori is one of the most talented and renowned hair ....</p>
<p>His magical hands have transformed a range of known and unknown...</p>
<p>Yiannis was born a hairdresser and was......</p>
<p>With his weapons... </p>
<p>Yiannis Tsapatori now lives and works in New York.</p>
</div>
</body>
</html>
That will work from IE7 and up but does depend on structure being as above.
.content p {
margin-top: 0;
font-size: 110%;
}
.content p + p {
margin: 15px 0 0;
font-size: 90%;
}
bugs me, and it may seem like I’m not consistent, since I advocate the drop of IE6- exactly for this reason: the ability to use selectors like +.
But in this case I believe it’s not a good option, when it comes to fast and efficient. The adjacent sibling selector, I think, it’s not put to a good use in a situation like this.
A class targeting that first p would be far more fitted here:
Maybe I misunderstand you than? Because I’m targeting the first <p> right now the way I want it. The first <p> has no top margin and all others(it’s not just one) have a top margin of 15px!
Global action: All p in content will have a 15px top margin assigned and a 90% font size.
Single corrective action: Only one p having firstPar class will be given 0 instead of 15px on that top margin, and 110% font size instead of 90%.
Using p+p, adjacent sibling selector, means more steps:
Global action: All p in content will have a 0 top margin assigned and a 110% font size, as opposed to just one p, the one we need.
Repetitive corrective sequence: every p having a sibling p will have its properties modified to: 15px margin top instead of 0, and a 90% font size instead of 110%.
I’m not sure if the browsers actually apply and then correct, like the CSS describes, I suspect not, but it will take longer to decide which property from which selector takes precedence. Milliseconds, or even less, it doesn’t matter. The principle of optimization applies still. I never test it, but it seems like that.
I use + selector in scenarios when there aren’t so many possible matches, in fact, I use it when I’m sure there is but one match, possibly occurring several times though, and in my experience it has to do more with classes than it has to do with the elements.