views:

298

answers:

6

How to style first paragraph <p> of the content style differently without using inline css, css class , ID or javascript. ? with IE 6 compatibility too.

+2  A: 

:first-child

Won't work in IE.

Azeem.Butt
it work on IE7+ but not on IE6 or lower
metal-gear-solid
A: 

Use the :first-child selector in the CSS.

div:first-child {
  background: #C00;
  color: #FFF;
}

Note: For :first-child to work in IE, a <!DOCTYPE> must be declared.

Ref: http://www.w3schools.com/css/pr_pseudo_first-child.asp

bhays
Hmm, I guess IE6 is exempt from that <!Doctype> addition...
bhays
no it's not working on IE6 http://jsbin.com/olawo
metal-gear-solid
A: 

Using first-child with IE6 which can solve your problem has already been answered here:

http://stackoverflow.com/questions/982759/is-there-any-fix-for-child-selector-in-ie6

Sarfraz
i can't add class
metal-gear-solid
A: 

From your question it seems like your only option is to use different tags (since you've excluded everything else). You could write an entire paragraph in h6 or something.

Your response to Kobi seems rude to me and not helpful. You're only telling people what you don't want, but maybe if you told us what you do want, you'd get better answers. The only thing I'm seeing is you really want the solution to be IE6 compatible. I don't see how that excludes all the other options you've decided don't work for you.

Stephane
+1  A: 

There is no way of doing this that works in IE6 without:

  • using inline style on the first paragraph;
  • giving the first paragraph a class to use in a selector; or
  • using Javascript to achieve one of the above.

IE7+ supports the :first-child pseudo-element:

p:first-child { color: red; }

The best solution is to give that paragraph a class that you can explicitly style if IE6 support is required. Alternatively style the element with Javascript. With jQuery it's simply:

$(function() {
  $("p:first").addClass("first");
});

with:

p.first { color: red; }
cletus
I didn't know this, thanks! :)
Nimbuz
That doesn't work on my IE6... and listed as `no` on http://www.quirksmode.org/css/contents.html
Kobi
Oh... it didn't actually mention that on the page. Usually it does. Will change to reflect.
cletus
+1  A: 

You could use the <font> tag. It's not standards compliant, but most browsers will support it, and it doesn't use CSS at all.

<p><font size="+1" color="red">Paragraph 1</font></p>

<p>Paragraph 2</p>
Tobias Cohen
but it's inline styling which i can't do
metal-gear-solid