Tips and Tricks About Computers, Web Development, Linux, the Internet and the Like
HTML
Generating CSS Based on Images, also SEO-Friendly
Feb 20th
I’ve typed out a lot of CSS that’s focused on using a background image to replace text. I’ve been using a cross-browser CSS trick that’s SEO-friendly, I’m not sure if it’s documented anywhere. I came upon the solution myself when I was searching for a clever way to do this years ago and it’s become the standard method for me. However, I dislike the tedious task of typing out the CSS and referencing the image pixels and at work, I don’t want my developers wasting that time either.
I figured it’d be best to come up with a simple solution, so I started coding something out in bash and it worked:
#!/bin/bash
# css.sh - generate common css and html
# Tyler Mulligan (z@interwebninja.com)
# Last Update: 02/16/2011
# MIT License
create_block_image() {
feh -lr ${1:-.} | awk '{ print $3" "$4" "$8 }' | sed '1d' | while read l; do N=$((N+1));
cbi_css $l
done
feh -lr ${1:-.} | awk '{ print $3" "$4" "$8 }' | sed '1d' | while read l; do N=$((N+1));
cbi_html_a $l
done
feh -lr ${1:-.} | awk '{ print $3" "$4" "$8 }' | sed '1d' | while read l; do N=$((N+1));
cbi_html_div $l
done
}
cbi_css() {
f=${3##*/}
echo "#${f%.*} {
display: block;
background: url('$3') no-repeat 0 0;
width: 0;
height: $2px;
padding-left: $1px;
overflow: hidden;
}"
}
cbi_html_a() {
f=${3##*/}
echo "<a href=\"#\" id=\"${f%.*}\" alt=\"$f\" title=\"$f\">${f%.*}</a>"
}
cbi_html_div() {
f=${3##*/}
echo "
<div id=\"${f%.*}\"></div>
"
}
s=$1; shift; case $s in
--image-block|--cbi|-i) create_block_image $@;;
*) help help;;
esacWhich ouputs something like:
z@zygon:~/scripts/css$ ./css.sh -i img/
#dumbtubes-fav {
display: block;
background: url('img/dumbtubes-fav.png') no-repeat 0 0;
width: 0;
height: 14px;
padding-left: 16px;
overflow: hidden;
}
#submit_new {
display: block;
background: url('img/submit_new.png') no-repeat 0 0;
width: 0;
height: 106px;
padding-left: 244px;
overflow: hidden;
}
#dumbtubes-logo {
display: block;
background: url('img/dumbtubes-logo.png') no-repeat 0 0;
width: 0;
height: 70px;
padding-left: 274px;
overflow: hidden;
}
<a href="#" id="dumbtubes-fav" alt="dumbtubes-fav.png" title="dumbtubes-fav.png">dumbtubes-fav</a>
<a href="#" id="submit_new" alt="submit_new.png" title="submit_new.png">submit_new</a>
<a href="#" id="dumbtubes-logo" alt="dumbtubes-logo.png" title="dumbtubes-logo.png">dumbtubes-logo</a>
<div id="dumbtubes-fav"></div>
<div id="submit_new"></div>
<div id="dumbtubes-logo"></div>
To explain the way this CSS works, it uses the padding-left as the actual width, setting the width 0 and then pushes whatever content you have out of view using overflow:hidden to hit it from view. This makes it easy for search engines, keeping your HTML clean and CSS simple.
The bash script relies of “feh” a lightweight image viewer for linux that outputs a list of images and their dimensions. It also generates some sample HTML to quickly drop in and modify.
This is not an elegant solution and I’m not happy with the fact that it relies on feh. I’ve been slowly getting into python and I was amazed at how fast I was able to recreate this script in Python.
#!/usr/bin/python
# css.py - generate common css and html
# Tyler Mulligan (z@interwebninja.com)
# Last Update: 02/17/2011
# MIT License
from PIL import Image
import sys
import os.path
CSS_FORMAT = """#%s {\n\
display: block;\n\
background: url('%s') no-repeat 0 0;\n\
height: %spx;\n\
width: 0;\n\
padding: %spx;\n\
overflow: hidden;\n\
}\n"""
HTML_A_FORMAT = """<a href="#" id="%s" alt="%s" title="%s">%s</a>\n"""
HTML_DIV_FORMAT = """
<div id="%s">%s</div>
\n"""
css=""
html_a=""
html_div=""
for tf in os.listdir(sys.argv[1]):
f = os.path.join(sys.argv[1],tf)
if os.path.isfile(f) == True :
img = Image.open(f)
fid = os.path.splitext(tf)[0]
(width, height) = img.size[0:2]
css += CSS_FORMAT % (fid,f,height,width)
html_a += HTML_A_FORMAT % (fid,f,fid,fid)
html_div += HTML_DIV_FORMAT % (fid,fid)
print css
print html_a
print html_div This is just the basic idea, I didn’t spend more than 15 minutes on this rewrite, it does what I need it to do so far. It was suggested by friends if I make it any bigger, to look into a templating engine, such as mako. I hope this script can be useful to someone :). PS, HTML_DIV_FORMAT should be on one line, not sure why my syntax highlighter is trying to put it on 3.
Optimizing CSS selectors for load time; #home is faster than #menu li a#home
Jun 27th
I was trying to figure out how to cut rendering time with smarter CSS. In my quest, I came across a plugin by google for Firebug called Page Speed which not only answered my question in the ‘Use efficient CSS selectors’ section (* #header #menu a:hover — Tag key with 2 descendant selectors and hover pseudo selector) but provided more details on the subject on the Page Speed Documentation.
“Descendant selectors are inefficient because, for each element that matches the key, the browser must also traverse up the DOM tree, evaluating every ancestor element until it finds a match or reaches the root element. The less specific the key, the greater the number of nodes that need to be evaluated.”
So, #home is faster than #menu li a#home; at least in Mozilla’s case. More on this here
Creating a simple news script that reads from an XML file
Mar 29th
I recently recoded a website for a friend because his markup was pretty ugly (thanks to WYSIWYG editors). While I was doing that, he mentioned he’d like to have an xml file he could edit to update his news. Simple enough I said and banged out the following which includes a little helpful function that wraps paragraphs in HTML p tags. My friend alpha helped me rewrite some ugly regex with a novel idea:
print join '', map {qq[
$_
]} split (/\r?\n/,$object->content)Of course, he’s a big perl coder so I had to create a php equivalent, nls2p.
Edit: Mookow mentioned he wanted to write links, so obviously a system would have to be put in place to support this. Using HTML tags in XML gets a little sloppy so I opted to use bbcode which is a very popular method to format text in web applications and very simple to add more filters.
The news post creator:
<?php
if (file_exists('news.xml')) {
$xml = simplexml_load_file('news.xml');
foreach ($xml->post as $post) {
echo "
<h4>".$post->title." - ".$post->date."</h4>
\n";
echo "
<div>".bbcode(nls2p($post->content))."</div>
\n";
echo "
--".$post->author."
\n";
}
} else {
echo "
Failed to open news source
";
}
The functions for formatting:
// Wrap paragraphs with
tags
function nls2p($str) {
$split_str = preg_split('#[\t]+?[\r\n]{1}#',$str);
foreach ($split_str as $line) {
$new_str = $new_str."
".preg_replace("/[\t\n]/","",$line)."
\n";
}
return $new_str;
}
// Parse bbcode as HTML
function bbcode($string) {
$search = array(
'@\[(?i)b\](.*?)\[/(?i)b\]@si',
'@\[(?i)i\](.*?)\[/(?i)i\]@si',
'@\[(?i)u\](.*?)\[/(?i)u\]@si',
'@\[(?i)img\](.*?)\[/(?i)img\]@si',
'@\[(?i)url=(.*?)\](.*?)\[/(?i)url\]@si',
'@\[(?i)code\](.*?)\[/(?i)code\]@si'
);
$replace = array(
'<b>\\1</b>',
'<i>\\1</i>',
'<u>\\1</u>',
'<img src="\\1">',
'<a href="\\1" target="_blank">\\2</a>',
'<code>\\1</code>'
);
return preg_replace($search , $replace, $string);
}
?>
The XML data file:
<?xml version='1.0' standalone='yes'?> <news> <post> <date>03-29-09</date> <author>Moo</author> <content>The site has been completely recoded by a friend of mine, -z- from NexuizNinjaz.com. He even added this fancy news script. Now, to update the news, I don't even have to touch index.php! Alright, I'm out for now. OMG A [b]NEW LINE[/b]! Now with [i]italics[/i] and [u]underlines[/u] and [url=http://www.nexuizninjaz.com]links[/url] [code]code blocks[/code] and images [img]http://www.xepic.net/pics/pic_091622001182920068.jpg[/img] </content> </post> <post> <date>03-29-09</date> <author>Moo</author> <content>Yeah, I removed the stupid gray background, because after a while it got boring. I changed the "Random Shiznit" column into something that could be a bit more productive. Once all of the other sites are up, I'll update the main page with the latest news from those. This main news column will be for the more intriguing news. I also decided to add titles to the news articles...Duuhhhhhhh OMG A NEW LINE! and again, LOL! </content> </post> <post> <date>03-25-09</date> <author>Moo</author> <content>I added a boring gray background to the main page, as you can see. The hover text-changing effect was removed because of its stupidity. A cool guy named Fabzor re-colored the header for me.</content> </post> <post> <date>03-23-09</date> <author>Moo</author> <content>Website updated, not much. Cool hover effects were added, but we still don't have much color in the page. Hopefully, I can find a good background and get this show on the road.</content> </post> <post> <date>03-22-09</date> <author>Moo</author> <content>Most of the links up top will not work for the time being, this site is just getting edited. Soon, we'll have a better webpage than this one, but for now, deal with this one!</content> </post> </news>
That’s pretty much it verbatim, including mookow’s news ^_^. Hope this is helpful to someone.
Free Resume Templates – Attribution-Noncommercial-Share Alike 3.0 Unported
Dec 24th
With more and more people using the Internet as a way to market themselves or find employees, it’s only a matter of time before it becomes the norm. As an employee, I find it an easy way to convey information about myself to employers. As an employer, it gives me an idea of a person’s professionalism and understanding of the age that I can easily reference or ask a friend for an opinion on.
I present to you my first two pieces of work released under the creative commons, 100% XHTML/CSS valid resume templates.
Resume Template – Modern v1 by Tyler Mulligan
Released under the Attribution-Noncommercial-Share Alike 3.0 Unported License you may freely use and modify this resume template as long as you leave in the credit to me in footer, don’t sell it and release it under the same or similar license (see the link for details).
To use it Download the zip from the Resume link in the footer, extract the files, edit them for your liking and information, upload them to your server and then upload the zip so others may easily download the template from your footer.
Resume Template – Neo v1 by Tyler Mulligan
Released under the Attribution-Noncommercial-Share Alike 3.0 Unported License you may freely use and modify this resume template as long as you leave in the credit to me in footer, don’t sell it and release it under the same or similar license (see the link for details).
To use it Download the zip from the Resume link in the footer, extract the files, edit them for your liking and information, upload them to your server and then upload the zip so others may easily download the template from your footer.
If you make any changes to these, I’d be glad if you told me. The XHTML is pretty solid but I was pretty conservative when it came to the CSS to keep the classic look and feel of a document.

