This is a follow-up to last week’s post, Should we design for wider screens?. In this post I’ll demonstrate with a simple example how easy it is, with CSS and a bit of jQuery, to make layouts that adapt to the many different screen sizes out there.
If you’re feeling impatient, jump straight to the demo!

Example HTML

At the top level, my example HTML page has a #wrapper div, which contains a #header, #main div containing all the interesting stuff, and a #footer. The main section has #content, #primary, #secondary and #tertiary divs for the different content areas of my page. The first two content areas, #content and #primary are wrapped up in an additional #container div, but your own markup may obviously differ.

<body class="normal>
<div id="wrapper">
	<div id="header"> ... </div>
	<div id="main">
		<div id="container">
			<div id="content"> ... </div>
			<div id="primary"> ... </div> 
		</div>
		<div id="secondary"> ... </div>
		<div id="tertiary"> ... </div>
	</div><!-- #main -->
 
	<div id="footer"> ... </div>
</div><!-- #wrapper -->
</body>

Prepare your styles

The basic idea here is to have different CSS styles for each width. The styles could be changed directly through jQuery, but that would quickly result in a lot of messy code. Instead, we’ll use jQuery only to change one class attribute. Initially I’ve added the class ‘normal’ to the body element to ensure a sensible default for browsers with JavaScript disabled, but we’ll also need styles for ‘wide’, ‘slim’ and ‘narrow’. This is easy to do with CSS descendant selectors:


#wrapper { margin: 0 auto; }
#header {overflow: hidden;}
#container,#content,#primary,#secondary,#tertiary
  {float:left;}
#footer {clear:left;width:100%;}

/* Wide ( over 1100px ) */
.wide #container {width:60%;}
.wide #content {width:66%;}
.wide #primary {width:34%;}
.wide #secondary,.wide #tertiary {width:20%;}

/* Normal ( over 800px ) */
.normal #container {width:50%;}
.normal #secondary,.normal #tertiary {width:25%;}

/* Slim ( over 600px ) */
.slim #container{width:66%;}
.slim #secondary,.slim #tertiary {width:34%;}

/* Narrow ( under 600px ) */
.narrow #secondary,.narrow #tertiary {width:50%;}
.narrow #secondary {clear:both;}

Unfortunately versions 7 and older of Internet Explorer deal differently with percentage widths compared to most other browsers, and can result in some funny behaviour. I see two primary ways to deal with it: either feed IE 6 and 7 it's own rules (e.g. a tested, fixed-width layout) or just make sure that the percentages never add up to 100 %. A nice explanation of this annoying feature can be found over at OJTech.com.

The jQuery

First of all I need to include jQuery, and perhaps the easiest way is to use Google’s jQuery for this. The main bit is a function, checkWidth() that (you guessed it) checks the width of the browser window and sets the body class attribute accordingly. The code below should be pretty self-explanatory.


	function checkWidth () {
		if ($(window).width() > 1400) {
			$('body').attr('class','superwide');
			$('#title').text('Wide ('+$(window).width()+')');
			}
		else if ($(window).width() > 1100) {
			$('body').attr('class','wide');
			$('#title').text('Wide ('+$(window).width()+')');
			}
		else if ($(window).width() > 800) {
			$('body').attr('class','normal');
			$('#title').text('Normal  ('+$(window).width()+')');
			}
		else if ($(window).width() > 600) {
			$('body').attr('class','slim');
			$('#title').text('Slim ('+$(window).width()+')');
			}
		else {
			$('body').attr('class','narrow');
			$('#title').text('Narrow ('+$(window).width()+')');
			}
		}

In addition to this, all I need is some code that will tell the browser when to run the checkWidth() function.

	$(document).ready(function(){

		// check the window size when page loads
		checkWidth();

		// check on resize
		$(window).resize(function() {
			checkWidth();
		});

	});

That's it! View the finished demo.

After starting to write this post, I read the latest issue (#200) of .net magazine and noticed a mention of a very cool way to do a similar trick using only CSS media queries! Media Query demo by Bruce Lawson. Haven’t tried it out yet, but I definitely will.

Hope you liked the post, feel free to tweet it, or leave a comment below.


More and more people are using wide-aspect monitors to browse the web. According to forecasts by Tobii (Swedish eye-tracking equipment manufacturer) wide aspect monitor penetration will be almost 100% by 2012. I think those numbers might be a bit optimistic, since Tobii obviously wants to sell their new wide-screen eye-tracker. Despite that, a very large number of web users are using screens with well over 1024px width, but almost all web sites out there are designed for lower resolutions. This got me thinking about fluid layouts again, and I decided to share my thoughts.

Fixed-width equals compromise

In the Beginning web sites were fluid, that is the content adapted to browser width. Today, most web design is fixed-width. That is, we build web pages to a maximum of about 900-980 px to accomodate the majority of screens. I’ll be the first to admit that most of the stuff I’ve made is fixed-width. Even my own blog is designed to a 700px width! It’s generally easier to design this way, especially if the design originates in Photoshop, which is essentially static. Also, fluid layout generally used to mean ridiculously wide and unreadable blocks of text.

Problem is, designing this way is a compromise. Many users today have wide screen laptops with 1280 px width or more, but their screen height may be a mere 700-800 px. Subtract from that the space taken by browser toolbars, taskbar/dock etc and you’re not left with much. On these height-challenged displays we should be using all the horizontal space we can to provide users with the information they need. But what about those narrower displays then, or people who simply choose to have their browser window at a narrower width despite having a large display? We’re not going to force them to *gasp* scroll horizontally or resize, are we? That would be silly. The answer is “intelligent” fluid layouts.

Fill that width!

On sites that have clearly separable content blocks we could be building layouts that intelligently re-arrange according to browser width. That of course does mean some more work put into the design phase, and possibly ditching Photoshop as a layout tool! The above image is a wireframe of  what a user would see on a wide screen (over, say, 1100px).

Narrowing down

On screens about 1024px wide we rearrange slightly, moving one content block (eg. recent blog posts) below the first one.

The narrower the screen is, the more we pile blocks on top of each other. This obviously requires more vertical scrolling, but all the content still remains accessible. Most importantly, we’re always using the best arrangement possible for the width of the browser.

Your thoughts?

What are you’re experiences of designing fluid layouts? Are there downsides to this, apart from the increased time needed for design? I’d like to here your thoughts, so please comment and share this on Twitter if you think it’s worth a discussion.

In Part 2, I’ll show an example of implementing the above with CSS and some jQuery, with sensible degrading in browsers with Javascript disabled.


Make your own short URLs with WordPress

You might have noticed that I recently restored the main URL of my site to http://danielkoskinen.com . I decided it was a better idea to have my full name in the URL, and use my other domain (dani.fi) to generate my very own short URLs for use in Twitter & elsewhere.

There are many good URL shortening services around, like bit.ly, twu.rl and the WP-centric wp.me, and I’m not saying there’s anything wrong with those. However, if like me, you happen to have a nice short domain lying around that you’d like to use, why not make your own?

Install WordTwit

WordTwit is a plugin for WordPress by BraveNewCode for integrating your WordPress website with your Twitter account. I owe big thanks to Mikko Saari for telling me about it. The reason why I liked this specific plugin is it’s feature for creating short URLs using your own domain.

You can install WordTwit either directly from your WordPress admin (under Plugins > Add new) or by downloading the zip and uploading it to your server under wp-content/plugins/.

Redirect short domain to longer domain

There are different ways to do this. Because I’m running this site on a dedicated server, I created a new VHOST entry for the second domain (dani.fi) and inserted the following:

   
<VirtualHost *>
   ServerName dani.fi
   Redirect 301 / http://danielkoskinen.com/
</VirtualHost>

Replace dani.fi with your own short domain name and danielkoskinen.com with the main domain for your site. If you’re running on a shared web host, you can probably just create a redirect in CPanel or equivalent.

Configure WordTwit settings

Enable WordTwit, find it’s settings page and the section marked URL Shortening. Choose “Local” from the menu and save your settings. This will reload the page and reveal more options. You can now enter your short domain in the field marked “Use Alternate Domain”.

Save your settings, and you’re done!

Try it out

Go to edit any post, and you’ll find the short URL for the current post in the right sidebar, using your new nice-and-short domain name! There’s also a button to post a message to Twitter (which can be set up automatically for new posts with WordTwit).


Bad Google!

Can anyone suggest a good online feed reader and webmail which doesn’t begin with a G?

Google Buzz created quite a stir from the start, with concerns over it’s privacy policy. The first time I logged in to see what all the buzz was about, I thought is was odd enough to see “followers” who I definitely know have never used ANY social media service. Some of them were probably just confused by the “Try Buzz” -notice and have since forgotten about it.

Look! You know all these people!

I only realized how totally screwed up the Buzz launch was after reading this post: F*ck you Google (summary: abusive ex-husband automatically added to follower list along with some not-so-nice crazy people who had emailed the author a lot). Apparently it has also been hard to get rid of Buzz, but the googlers might have corrected that (see below).

There’s been reason before not to trust Google in issues of privacy (*cough*Gmail ads*cough*) but this latest thing really seems to be a case of beating Facebook at the “we don’t care about privacy” game. Google does a lot of cool stuff (GReader, Gmail, Android, Wave etc.) so it’s really disappointing to see them pull something like this.

How to get rid of Buzz

Might have to start looking for a replacement. Meanwhile, apparently to get rid of Buzz you need to delete your Google Profile. To do so, go over to your Google account (https://www.google.com/accounts/ManageAccount), click on “Edit profile” and then right at the bottom of the page click on “Delete profile and disable Google Buzz completely”.


Experimenting in HTML5 and CSS3

I decided to experiment with some of the new elements in HTML 5 and built my blog theme from scratch (with the help of the Carrington framework though). I’m now rolling article, section, header, footer and other stuff. Because IE needs a Javascript hack to enable support for these elements, I wouldn’t consider doing this in any client work, but hey, this is MY blog!

If you see subtle gradients here and there, congratulations: you’re running a recent build of Safari, Chrome or Firefox. In all versions of IE everything should still look just fine, even IE6. I haven’t done thorough checking yet though!

My other aims with this theme were to create a very readable blog, with clearly visible titles and separation of content. I hope I succeeded, although this is obviously a work in progress, just like any other website out there.