CSS


Lately I’ve had a few users respond on my CSS Tooltips post about it not working correctly in Opera, so I decided to investigate myself.  I looked at the example I created and sure enough, it tries to work, but fails.  My first suspect was the :hover css filter since this is what the css tooltips are based off of.  Then I decided to do a few tests with Impromptu.  My test was to use the :focus selector on the focused button to change the background color in one of my themes:

div.jqi button:focus{ background-color: #31B639; }
div.jqi button:focus:hover{ background-color: #21a629; }

Just as I suspected it worked like a champ in Safari and Firefox, but Opera had trouble.  It would flicker when I hovered over the focused button, but would not return correctly.  I did a few quick google searches but come up empty.  Anyone know if this is a known issue?

Nearly every website these days have columns. Since we have grown out of the old table days and into this new era of CSS we tend to over think certain things like how to create two columns side by side which can vary in height, but still appear to be the same height. The solution is so simple its rediculous. We start out with a left column and a right column(could be three columns.. this will work just as easily, but I’ll keep it simple), two div’s floating side by side via float:right; or float:left;. Next we add a left border to the right column, which is the same width and color as the left:

border-left: solid 150px #aaaaaa;

This will shove your right column out of line, but no worries, step three we slide it right back into place. With a negative left margin on the right column we can literally slide the right column back up under the left column:

margin-left: -150px;

So basically we added two lines of code to the independent columns, now it never matters which one is taller, they fill in for each other. A simple Example.

CSS is a very simple language, however it can also be the most difficult when considering all browsers. There are five easy steps you can take along the way to help avoid any hacks or problems.

1. Doctype: Always include a doctype in your html
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Strict//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml”>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=UTF-8″ />

2. Global Reset: many browsers have different default padding, margin, or border values. This is where most problems occur. To avoid this always use this handy line in the very top of your CSS

*{ margin: 0; padding: 0; border: 0; }

Yes this does make your lists and all look unpleasant, but keep reading!

3. Initialize Elements: Always initialize your elements right after your global reset(padding, margin, and border). This way before you begin any class styles you know all of your elements will react the same. Most importantly your body, ul, ol, li, dl, dt, dd, p, input. Dont worry, if you missed one you will notice right away. Now all of your elements should have the exact same padding, margin, and border.

4. Use the Proper Elements: Pay special attention in your html elements. If its essentially a paragraph, use a paragraph tag. Don’t waist time in your CSS trying to change a div to look like a p.

5. Separate Appearance and Alignment: Try your best to declare things like line-height, padding, margin, font in your elements or in “global” classes. By global I mean a class that you will use on several pages. Then try to do all of your alignment(float, position, top..) in your more local styles to fit that page. This way your theme carries across the site, but your layout is able to change.

6. Use Local or Page Specific Styles: Once you have your basic theme down you don’t want to touch those. Try using local styles so they don’t effect anything else.

form.myloginform fieldset{ }
form.myloginform fieldset legend{}

You may wind up with a little more code that way, but it will be much more organized in the end.

7. Buttons Are Easier: When creating forms consider using a button(type=submit) instead of input(type=”submit”). Buttons are more consistent to style across all browsers and will give the same functionality.

8. Occasionally Validate: Occasionally validate your CSS and html. If nothing else this will let you know if you have any ending tags missing, and can save you a big headache when you’re a million lines deep in code!

There you have it, 8 ways to Improve your CSS life. Now go delete those ie5.css, ie6.css, and ie7.css stylesheets!

Finally a new version of jQuery Impromptu has been released. This release in my opinion has a few new cool features. First off you can now control the speed of the fade and the prompt box. Also you can now set which jQuery effect to use to bring in the prompt(show, fadeIn, slideDown, or your own custom effect).

Now to the good stuff. Now you can have a submit function. Just as you have form.submit(), you now can have the same function which operates about the same. You can validate your fields and determine whether to proceed sending the data and closing the prompt or not(returning true or false accordingly). Then you will still have your separate callback to be called after the prompt completely closes.

I hope this is of some use to someone, I know I’ve already used it several times!  There are examples of these new features on the Impromptu site.

Well I gave it a go at the jQuery plugin deal, and well.. I dont think version 0.1 turned out too darn bad.  jQuery Impromptu as I’m calling it is a nice little plugin to simulate a nice css/js alert or prompt You may also add forms custom forms and elements to your prompts as well.   A few of the options allows you to narrow the allert down to an html element on the page(given it is relative or absolutely positioned) and create as many buttons on your prompt as you wish! A callback function lets you process your forms.   Please give it a shot on my examples page and please let me know if you have suggestions or constructive criticism or any other comments.

I ran into a need for a print preview on my website the other day and was unable to find a quick solution, so with a little thought I whipped up a small function which does just that with CSS and Javascript. Here’s what I come up with:

<link id=”screenCSS” media=”all” rel=”stylesheet” type=”text/css” />
<link id=”printCSS” media=”print” rel=”stylesheet” type=”text/css” />
<script language=”javascript”>
function togglePrintPreview()
{
     var currCSS = document.getElementById(’printCSS’);
     if(currCSS.media == ‘all’)
         currCSS.media = ‘print’;
     else currCSS.media = ‘all’;
}
</script>

Pretty simple theory.. just toggle the media attribute of the link tags from “print” to “all”. Maybe a little dumb,.. but very practical. View Example

Avoid cross-browser javascript when you can use css to make tooltips with less code. Honestly you were going to use css to style your tooltips anyway right? Your probably already have most of this code in your css already too. Besides that if you have an advanced site in the first place you probably have enought javascript already.

Also with the IE hack for the :hover state, you can do this with elements besides anchors.

If you dont like how it allows you to hover over the tooltip also then you can adjust the padding and top to separate the tool tip from the link.

View Full Example and Code

Download Source