Archive for March, 2010

CSS Selectors – a real world example

Friday, March 5th, 2010

Today a co-worker of mine had a simple problem that he wanted some help with. He needed to find all the “a” tags within a specific div, and if its href attribute ended in .pdf, then add a background image to it. I am going to guess that most people would just go and add a class to all the “a” tags that needed the image. That just puts more html on the page, when you can effectively do this with just some css.

Take into account, this example that we used today, works in all modern browsers, but not IE6-. Worked in IE7, so that was a good plus.

Here is the css:

#divId a[href$=".pdf"] {
     background-image: url(../images/imageName.gif);
}

This says get the div with whatever id you are looking for, with an “a” tag, that has an “href” attribute that ends with exatly “.pdf”, and add the background-image to it.

I then did the same thing but for all links that got to “.docs”, and displayed a different image. So the second style was:

#divId a[href$=".doc"] {
     background-image: url(../images/imageName2.gif);
}

If you actually wanted to hit ALL the “a” tags within the site like that, just remove the #divId from the style.

New WordPress theme installed!

Wednesday, March 3rd, 2010

Woo-hoo! My new WordPress theme is now installed. I obviously still have some tweak to do…such as a nav, style the footer, and some other minor stuff, but all-in-all, a success! I learned a lot by doing this re-theme. Mainly because I did it the correct way this time, instead of my quick and dirty method. Let me know what you think!

Almost done!

Tuesday, March 2nd, 2010

Check the link in the previous post! I am almost done with the new theme. I may even install it here tonight! There was a lot of trickiness involved, and some CSS even I haven’t seen before. Such as this:

.entry ul li:before, #sidebar ul ul li:before {
      content: "0BB 020";
}

You know what that does in the default theme in WordPress? It adss >> in front of all the sidebar list items. REALLY damn annoying. I searched the code for about 20 minutes and googled my rear off. Finally got the right search terms to bring up a forum post of someone trying to do the same thing.

Apparently you can use the content property to add things to the html…such as this example:

a:after {
     content: " (" attr(href) ")";
}

That adds the URL in parenthesis after each link. Interesting bit of css right there. Can’t think of a reason why I would use it off the top of my head, but still cool.