tomhoppe.com

Archive for the ‘CSS’ Category

Use it or lose it…..

Monday, April 14th, 2008

Its amazing how fast you lose a skill if you don’t use it for a few years

In my current day job of product development and client-side engineering, I haven’t been messing much with ASP, other then the occasional support for some in-house apps

I recently took a side job to make a database driven website for a jewelry store. The front end was a piece of cake, and took less time then I estimated. The asp back-end/adming tool/shopping cart on the other hand, bleh. Having been working pretty much 100% with Javascript, I “lost” the ASP syntax, methods, etc. I had to look all that up on google. I knew “what” I wanted to do, but I just couldn’t write it off the cuff like I can with JS these days.

In the end, it took certainly longer then planned, but I’m glad that I did it. I got the “asp mindset” back, and I haven’t lost a step in my day job either. Guess I came out smarter overall :)

Dynamically adding CSS through Javascript

Tuesday, March 18th, 2008

I found this little tip by accident while surfing the web to solve a different problem. Thanks to the Yahoo User Interface Blog for the solution!

When adding in a piece of HTML dynamically, what do you do with the styles for it? In the past, I usually included the styles in a global or page specific stylesheet where I knew the HTML would show up. The current snippet I was writing through, was going to add some code in random places throughout a 4000+ page website as well as some partner sites, so it needed to come with its own CSS. FF allows you nicely to just embed a <style> tag anywhere in a page with your styles and be done with it. IE and Safari, not so much.

What I found on the YUI Blog was the cssText property of the stylesheet property. This allows for a cross browser way for us to insert a string of css into a style element in the head. Beautiful! Just create your css as a string and pass it into the addCSS function below.

function addCss(cssCode) {
var styleElement = document.createElement("style");
  styleElement.type = "text/css";
  if (styleElement.styleSheet) {
    styleElement.styleSheet.cssText = cssCode;
  } else {
    styleElement.appendChild(document.createTextNode(cssCode));
  }
  document.getElementsByTagName("head")[0].appendChild(styleElement);
}

Assign the child’s href to the parent’s onclick

Monday, July 16th, 2007

This is a cool little snippet for making realistic CSS buttons. It will do an onclick location on the element to the href of its first a element.

Makes it nice when you have a “fake button” done through CSS and you want the whole thing to be clickable.

What I usually try to do is CSS the A tag display:block and assign it a width, height and padding, but when you can’t do that, this little snippet is a life saver

onclick="if (this.getElementsByTagName('a')[0])location=this.getElementsByTagName('a')[0].href;"