Quick CSS for WordPress Images

If you install WordPress and use the media uploader to manage your images in your posts – you can easily style your images quickly by adding the following code to your theme’s stylesheet.

img.alignright, img.aligncenter, img.alignleft {padding:4px;background:#efefef;border-color: #aaa #ccc #ddd #bbb;border-style: solid;border-width: 1px;}
img.alignright {float:right;margin-left:5px;}
img.alignleft, img.alignnone {float:left;margin-right:5px;}
img.aligncenter {display: block;margin-left: auto;margin-right: auto;}

This code will set up your images to float appropriately, as well as give a nice border as seen below.

Christmas in DC

Note* This information is now updated: Updated CSS

Using !important With CSS

I am not a “quick fix” or “work around” type of person. When a CSS debugging project comes my way I look first at fixing the problem systemically vs. applying the quick fix. Recently though I was brought into a project with an insane amount of CSS and conflicting styles all over the place. Since I did not have ample time to fix the issues systemically (this was a super stat job) I was forced to use something that would take precedence over the problematic styles already in place to quickly fix the issues.

If you have worked with CSS before I am sure you have seen “!important” used but probably were not really sure what it was. I ended up using a “fixes.css” style sheet which was called in after all the other style sheets and then using “!important” with the styles I needed to correct (override). This was very helpful and if you ever run into a similar situation or have a certain style that you cannot seem to figure out why something is not showing up (“I set this to have a margin-bottom:20px; but there is no margin at all”) try putting in the !important (margin-bottom:20px !important;) and see if that helps.

More information on !important: http://www.w3.org/TR/REC-CSS2/cascade.html#cascading-order

Simple 3 Column Layout

I was talking to one of my friends today who does a lot backend programming but often needs a quick and easy 3 column layout that has the floats all figured out. His frustration with floats prompted me to make a very basic layout for him to use. If you find yourself in the same situation – check out my basic 3 column layout.

Always Use a Doctype

After spending an hour on something that should have taken me 20 minutes – I wanted to pass on something I should have caught right away. The problem: you use CSS to lay something out and it looks great in Firefox and Safari – but not in IE. At this point I validate the CSS to make sure I did not forget a bracket or something stupid. Next step – validate the XHTML. This is where I would have solved my problem, but I failed to take this step. At that point I would have seen that the script I was working with did not have a doctype defined. Once I added in the doctype – everything loaded as expected and I could not believe I missed something so basic. Lesson learned – and hopefully you will know now as well to make sure a doctype is always set.

Add a "Pseudo" Pseudo Class Hover in Inline CSS.

I have been busy this week with a few really cool projects – one of them to create a widget for any site that will show the latest news items from a RSS feed – while not limiting the widget to servers with PHP. After putting that together, I had to add inline CSS to style the feed output. Everything looked great but I could not figure out how to add a :hover to an inline style…until I realized I could fake it by adding some basic javascript: onmouseover and onmouseout to change the font color.

<a href="file.html">Title&lt;/a

Jappler Menus in WordPress

Awhile back, I put something together (sons of suckerfish and sliding doors) and then was recently asked how to get this to work dynamically in WordPress.

The key to getting this to work dynamically is to first choose how you want to organize your site. I have done numerous sites based on categories and a lot also done using pages. Once you figure out what you want your menus to be driven from (page hierarchy or category hierarchy) you can then use some built in WordPress functions to dynamically fill out your menus.

For categories: you will want to use the wp_list_categories function
Example: (using the CSS and JS from the article I posted previously)

<div id="nav">
<ul>

</ul>
</div>

With that example, you can exclude certain categories, and their will not be a “Categories” text as the first level of the list. For more options, check out the wp_list_categories documentation.

For categories: you will want to use the wp_list_pages function
Example: (using the CSS and JS from the article I posted previously)

<div id="nav">
<ul>

</ul>
</div>

With that example, you can exclude certain pages, and their will not be a “Pages” text as the first level of the list. For more options, check out the wp_list_pages documentation.

There is also another option (plugins) I have used that extends both of those functions: Fold Category List, Fold Page List.

Sliding Doors + Sons of Suckerfish = Jappler Menus!

There have been two great menu and navigation techniques I have used over the last few years:

  1. Sliding Doors of CSS
  2. Sons of Suckerfish (for drop down menus)

I have used both of these techniques on several web sites, but crazily not together…until a week ago when a client decided they wanted to move away from their antiquated menus that relied on hundreds of lines of javascript, HTML tables, and over 40 image files (for seven tabs). This technique works in IE 6, IE 7, Firefox, and Safari.

After I had put the code into place I thought it might be helpful for others to see exactly how it is done. These techniques give you a lot of flexibility with very lightweight code.

Code/Files Needed

  1. Two tab images (left and right, with both an active and hover (highligted) option
  2. Sub-menu arrow (not required)
  3. Sons of Suckerfish javascript
  4. CSS for both the drop downs and the sliding doors techniques
  5. Menu list code (your menu items)

Two tab images
The sliding door technique is great because you do not need to make tab specific images, nor do you need to worry about the tab length – it is all taken care of by two images and some CSS. For this example, I am going to use two basic tab images that I quickly created for this example:

  1. Left
  2. Right

Sub-menu arrow (not required)
If you want to easily let your visitors know you have a sub-menu within your menus, add in a sub-menu arrow.

Sons of Suckerfish javascript
After years of using nasty javascript – it is refreshing to see that this technique only requires a few lines of javascript (check out the javascript).

CSS for both the drop downs and the sliding doors techniques
The CSS for the drop down menus and the tabbed navigation is pretty slick (check out the CSS).

Menu list code (your menu items)
The menu list code is the actual menus/tabs. The code can be dynamically created by using something like wp_list_pages() in WordPress, or you can create the code yourself. I have created some sample code for this example.

Everything put together
Now that you have an understanding of all the files and code involved, let’s see everything put together This is a very good way to have sustainable code/files for a small or large web site when dealing with navigation.

Questions, comments? Contact me!

Swamped, But Big Things to Come

I apologize for not blogging as much as I would like over the last few weeks, but I have been swamped with a number of big projects. As soon as I have more than five minutes at one time to blog – I have a great blog post to write on some really cool stuff I have been doing with CSS. Stay tuned!