Tag Archives: stylesheets

Giving Users Style Choice

Sometimes your site’s style doesn’t meet the needs of all your visitors. And once in a great while you get a chance to see what your users are seeing and make a change to benefit them.

Having recently recieved an Eee PC for graduation, I suddenly saw the world of 800×640 resolution and understood that fixed width dual sidebars was a BAD design decision.

Small Screen User: What? Only see one word per line? Preposterous!

My site should now be more friendly to small screens because of several changes I made to the CSS. The same changes should also benefit users with screen readers and text based browsers. The method should also work with users who have JavaScript disabled. If you’d like to use CSS to change your site to be more friendly, here’s what I did.

Organize Your Content

Break your content up into divs. Organize those divs so that the most important content is at the top of the HTML and the least important is at the bottom.

Organizing your content this way will make it easier to use CSS, make it naturally more friendly for text based browsers and screen readers. On my site the first div has the page navigation. The second has the content. The ads, search box and my doodles all are added at the very end of the HTML.

Use CSS to put the divs where you want them. If you don’t know CSS, then it’s time to learn!

Provide alternative styles

Firefox and other good browsers make it easy to switch between provided stylesheets. Most of you will be here with Firefox. In your Firefox menu go to ‘View’ –> ‘Page Style’ –> and choose a style!

Add these alternative styles with a line of code in the <head> like that below. The key is the rel=”alternative stylesheet” part.

<link rel="alternate stylesheet" type="text/css" href="http://stuporglue.org/freestyling.css" media="screen" title="Small Screens" />

You can offer all sorts of different styles. Ideas include a) Alternative themes, b) high contrast version, c) small screen version, d) no images version.

Automatically Pick The Right Style

Sadly, browsers don’t send window or screen size to the server. JavaScript knows though! Since this is the case, we’re going to need to use client side scripting to change stylesheets automatically.

The first part of this step is to give the default CSS line (in your <head> section) an id so that JavaScript can get a handle on it more easily. Then we use the screen.width value to determine if we’re going to switch the CSS on the user. My code goes a little something like this:

<script type='text/javascript'> if(screen.width <= 800){ document.getElementById('maincss').href = 'freestyling.css'; }</script>

The above snippet will realize that the user has a small screen and change the CSS to a small-screen friendly version

Of course this part won’t work for visitors who have JavaScript disabled, which is why we’re going to add one more piece to the puzzle so that they too can benefit from alternative stylesheets.

Give The User Choices

Your visitors know what size works best for them, so give them a choice. You may notice a new menu item on the side of this page which gives the options “Small Screen CSS”, “Normal CSS”, “Default CSS”. These links call change-css.php with different parameters, causing a cookie to be set or unset.

In the head section include (you ARE using includes on your pages, aren’t you?) we now add a little bit of code to check if there is a cookie, and if so to use the style sheet specified by the cookie

<?php
if($_COOKIE['screensize'] == 'normal'){
    print "<link id='maincss' rel='stylesheet' type='text/css' href='http://stuporglue.org/ohyesivegotstyle.css' media='screen' title='Default (Big Styles)'/>";
}else if($_COOKIE['screensize'] == 'small'){
    print "<link id='maincss' rel='stylesheet' type='text/css' href='http://stuporglue.org/freestyling.css' media='screen' title='Default (Small Styles)'/>";
}
else
{
    print "<link id='maincss' rel='stylesheet' type='text/css' href='http://stuporglue.org/ohyesivegotstyle.css' media='screen' title='Default Styles'/>\n";
    print "<script type='text/javascript'> if(screen.width <= 800){ document.getElementById('maincss').href = 'freestyling.css'; } </script>\n";
}
?>

Note that I only print the JavaScript in the default case. This prevents us from changing the CSS stylesheet automatically if they’ve selected one already.

Cookie Setting Code

This is the code I use to set the cookies

<?php

// Check if they have the GET parameter we are expecting
if($_GET['screencookie']){
    switch($_GET['screencookie']){
    case('normal'):
	// Set the normal CSS cookie
	setcookie('screensize','normal',time()+60*60*24*30,'/');
	break;
    case('small'):
	// Set the small CSS cookie
	setcookie('screensize','small',time()+60*60*24*30,'/');
	break;
    case('ihatecookies'):
	// Remove the cookie
	setcookie('screensize','removing',-1,'/');
	break;
    }
}

//if they're coming from one of my pages already, send them back where they came from.
if(strpos($HTTP_REFERER,'http://stuporglue.org') === 0){
    $place = "Location: $HTTP_REFERER";
    header($place);
}
else {
    // If for some reason they got linked to the change-css.php
    // page without already being on the site, send them to the homepage.
    header('Location: http://stuporglue.org');
}
?>
Posted in Programming, Projects | Tagged , , | Leave a comment

CSS color extracter

This is one of the reasons I love Ruby. I wrote some code that would take a CSS file and print out the unique hex colors, and it was about 10 lines long. I then realized that if I had been thinking in Ruby, I could do it in just one line.

This page is OLD and probably contains errors, out of date information, security flaws or other problems. I am keeping it around because it might be helpful to someone.

File.readlines(ARGV[0]).to_s.downcase.scan(/#[0-9a-f]{6}/).uniq.sort.each {|one| puts one} if (ARGV[0] != nil)

Put that in a Ruby file, and run it with a parameter of the CSS file to parse, and it will print each unique color on a single line.

Expanding on that work, we can now create Gimp palettes for use in a variety of Open Source graphics programs, including Inkscape and The Gimp. Here is a program that will do just that.
Download it here

#!/usr/bin/env ruby
# Create a gimp palette from a CSS file's unique hex colors
# Weakenesses: Doesn't account for 3 digit colors (ie. #abc;)
# or for named colors
#
# By Michael Moore stuporglue@gmail.com
# Released to Public Domain April 5, 2007
if (ARGV[0] != nil)
    gimpstring = "GIMP Palette\nName: "
    if(ARGV[1] != nil)
	gimpstring << ARGV[1]
    else
	gimpstring << ARGV[0]
    end
    gimpstring << "\nColumns: 0\n#"
    # This could have problems if you have ids that are only hex chars for the first 6 values
    # eg. if this were your CSS
    #
    #	#abc123nurserystyle {
    # 		background-color: green;
    #	}
    #
    #	the color #abc123 would be added to the palette

    File.readlines(ARGV[0]).to_s.downcase.scan(/#[0-9a-f]{6}/).uniq.sort.each do |onecolor|
	colorparts = onecolor.scan(/[0-9a-f]{2}/)
	gimpstring << "\n" + colorparts[0].hex.to_s.rjust(3) + " " + colorparts[1].hex.to_s.rjust(3) + " " + colorparts[2].hex.to_s.rjust(3) + "\tUntitled"
    end
    print gimpstring
else
    puts ""
    puts "CSS to Gimp Palette"
    puts "Use 6-digit hex colors from a CSS file to create a Gimp palette (gpl) file"
    puts ""
    puts "USAGE: csstogpl.rb cssfile [Palette Name]"
    puts "If Palette Name is ommited, the CSS file's name will be used instead"
    puts ""
    puts "Note: csstogpl does not write to a file, it prints to stdout."
    puts "To save to a file, use redirection. eg."
    puts "csstogpl.rb style.css MyAwesomeColorScheme > MACS.gpl"
    puts ""
end
Posted in Programming, Projects | Tagged , , , , | Leave a comment