Forum Moderators: not2easy

Message Too Old, No Replies

Alternating the style of elements

Alternating the style of items on the page purely using CSS

         

bhonda

8:27 pm on Aug 7, 2006 (gmt 0)

10+ Year Member



Hey guys.

I'm having fun with CSS at the mo - got a website to design from scratch, so I'm doing a CSS-Zen garden thing and using the same HTML files but only changing the CSS file. Got 2 designs done so far...3 more on the way!

My problem is this.

I have a number of paragraphs on my page.

For example...

<p>Contents of the first paragraph</p>
<p>...and the second</p>

Simple stuff.

Now - I want to apply a style to these P tags, but make the style alternate...so the first paragraph will, for instance, be in blue, the second in red, then the third in blue again...and so on.

Is there any way, using posh CSS-trickery that I can do this, without changing the HTML at all?

A simple 'No' will do if I can't!

Cheers,

B

AdamLC

8:29 pm on Aug 7, 2006 (gmt 0)

10+ Year Member



You will need to change the HTML I would say.

A simple style tag inside the P tag should achieve what you want :)

Fotiman

9:19 pm on Aug 7, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Not with CSS 2.1, no. However, maybe possible with CSS 3 selectors:

[w3.org...]

(Don't expect any browsers to actually support this yet though).

bhonda

9:34 am on Aug 8, 2006 (gmt 0)

10+ Year Member



...I guess it's not such a good idea to hold my breath until CSS3 becomes standard, right?

Cheers for the responses...I guess I'll scrap my alternate P plans for now...and wait a couple of decades 'til I can actually do it properly!

B

Setek

10:12 am on Aug 8, 2006 (gmt 0)

10+ Year Member



I had an idea for something like this... from what I see in the CSS3 working draft, there's
nth-child
etc, but how would one, say cover odds and evens?

A lot of people, esp. in tabular data, want to have every second row coloured a different way. The way we do it is assigning a class to every second row. Or using javascript/some other scripting method to do it?

What about an

:odd-child
,
:odd-child-of-type
,
:even-child
, and
:even-child-of-type
?

Wouldn't those be awesome pseudo-selectors?

encyclo

10:26 am on Aug 8, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>test template</title>
<style type="text/css">
p {color:green;}
p+p {color:red;}
p+p+p {color:green;}
p+p+p+p {color:red;}
</style>
</head>
<body>
<p>green</p>
<p>red</p>
<p>green</p>
<p>red</p>
</body>
</html>

In all conforming CSS browsers - in other words, not IE. ;)

Otherwise, you just add a

class
to each second paragraph with a style to match. Check out some of the "zebra tables" scripts too - you should be able to adapt the Javascript to work for paragraphs easily enough.

bhonda

10:35 am on Aug 8, 2006 (gmt 0)

10+ Year Member



..but how would one, say cover odds and evens?

...from the CSS3 spec...

tr:nth-child(2n+1) /* represents every odd row of an HTML table */
tr:nth-child(odd) /* same */
tr:nth-child(2n) /* represents every even row of an HTML table */
tr:nth-child(even) /* same */

...how cool is that!

Just as a rough estimate, anyone got any idea when this sort of thing will be implemented in browsers?

B

Fotiman

12:08 pm on Aug 8, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month




Just as a rough estimate, anyone got any idea when this sort of thing will be implemented in browsers?

Considering that IE still isn't even implementing all of CSS 2.1 (min-width anyone?), I wouldn't hold my breath. This is probably still another 5 - 10 years out with the snails pace that Microsoft moves at.

Robin_reala

6:29 pm on Aug 8, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Actually, IE7 does support min-width, which is nice.

Fotiman

6:35 pm on Aug 8, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Oh yeah, I keep forgetting that. I think I was so used to hearing that IE7 wasn't going to support it that it's all I think of when I think of IE7. :-)

maxxtraxx

11:05 pm on Aug 8, 2006 (gmt 0)

10+ Year Member



ummm, why not just create a class for each color you want (or any other styling for that matter)?

.red {color:red;}
.salmon {color:salmon;}

<p class="red">here's some red text.</p>

bhonda

8:16 am on Aug 9, 2006 (gmt 0)

10+ Year Member



why not just create a class for each color you want

I could, but I really want to do this without changing the HTML at all, ie, through CSS alone.

Setek

8:24 am on Aug 9, 2006 (gmt 0)

10+ Year Member



It's not currently possible without assigning a class, or doing something else funky - the CSS required isn't supported by browsers yet.

And the CSS probably never will be supported by IE properly.

</cynic>

penders

11:59 am on Aug 9, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Just a quick note...

.red {color:red;}
.salmon {color:salmon;}

<p class="red">here's some red text.</p>

I think it may be best to declare class names that are separate from the style (separating style and content blah blah), in case you later want to change the colour then it still makes sense, ie:

.odd {color:red;}
.even {color:salmon;}

<p class="odd">here's some odd text, it maybe red, maybe not.</p>

encyclo

5:08 pm on Aug 9, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If you choose this method, you don't need two classes, just one for the alternate style. :)

CSS:

p {color:red;} 
.second {color:salmon;}

HTML:

<p>standard style (red text)</p>
<p class="second">alternate style (salmon text)</p>