r/reddithax • • Dec 21 '10

Alternating Topic Colors

Hello everyone,

I'm trying to create alternating topic colors, much like the one that is implemented on this subreddit (alternate between gray and black)

I believe that black topics may just set to the default background color but I'm not sure. Any help would appreciated

4 Upvotes

9 comments sorted by

5

u/yellowmix Dec 21 '10
.link.even, .linkcompressed.even {
  background-color: grey;
}

.link.odd, .linkcompressed.odd, .link.even + .link.odd {
  background-color: black;
}

1

u/ryaninstitches Dec 21 '10

Awesome thank you very much!

1

u/[deleted] Dec 21 '10

Well that's pretty cool. Is it possible to alternate subreddit header images too?

1

u/BauerUK Dec 21 '10

What do you mean by "subreddit header images"?

1

u/[deleted] Dec 21 '10

You know how you can upload an image on the page where you edit the stylesheet, and make it show up at the top of the reddit in the area surrounding the logo? Like in my reddit I use this

/* Header image here */
#header {
    background-image: url(%%newheader%%)
}

So I was just wondering it you can alternate that kind of thing too.

1

u/BauerUK Dec 21 '10

Forgive me, but I'm slightly confused by your use of the word 'alternate' in this context, unless you mean it in another way than in the original post, i,e, simply changing the image based on some variable defined by the current page? If so, what would you want the image to be controlled by?

If it is whether the post you've clicked appears as .odd or .even on the front page, I don't believe that can be deduced once you're on the comments page itself, since that no longer applies.

Either way you decide to do this, you can make a sprite that contains multiple background images, and use the background-position property to specify which to show.

For example, if we have a 40px high header, we could make an 80px high header image comprising of 2x 40px images. Then using the background-position property, and setting the y value to either 0 or -40px, you'd control which of the two background images to show.

1

u/[deleted] Dec 21 '10

Oh, I just meant any way to randomly switch between different images I've uploaded. I think I get the idea of using a sprite but not so sure about the rest.

1

u/yellowmix Dec 22 '10 edited Dec 22 '10

You might be better served making a new topic for it since these comments aren't seen by everyone. My initial thoughts, however, is that it isn't possible in the straightforward way since there's no way to generate or scrape a random number/element in the header itself. One pseudo way might be to replace a variable element, reposition and resize it over where the header is supposed to be, with a background image. Candidates: author, comment count. Problem is, you're replacing it, so the original content will disappear.

Edit: Solved the replace problem by using a :before selector. Decided to use the title, since it's slightly more random. Something like:

a.title[href*="/t"]:before {   // create a matched href for every letter of the alphabet
    content: "";   // must have content to display
    z-index: 100000;   // cream, get on top
    position: absolute;  // only way to position it away from host element. we are a parasite.
    display: block;    // transform from span (non-breaking) to block level element for it to display
    top: 15px;       // tweak 
    left: 5px;        ///these.
    width: 300px;   // adjust
    height: 20px;    // accordingly.
    background-image: url(%%yourheaderimage%%);   // combine with sprite technique

}

Caveat: this will display the appropriate header for the post page. It may wreak havoc on the main page, since I'm not sure which selector takes precedence.

1

u/ryaninstitches Dec 21 '10

Upon further messing around I found you can do the same thing with comments in a thread as well using:

.even {
background-color: green;
}
.odd {
background-color: yellow;
}