Archive for the ‘html’ Category

Preloading resources

Monday, October 3rd, 2011

You might have read that when optimizing your site for speed HTTP requests are something to look at – less requests mean less waiting and usually a faster site. When doing that for CSS you’re faced with a dilemma:

  1. Put all of CSS in one file, which means less requests, but also means that the first time people land on your site, they will wait longer than needed to see the content which could mean a ruined first impression.
  2. Put CSS in per-section files, which means less loading up front but more request as people drill down into different sections of the page.

When using the first option your only focus can be on how to optimize the CSS so that it loads as fast as possible and is as small as possible. As you have less files you can easily have that file minified and gzipped even if you’re not using a deployment solution that will do that for you.

Using the second variant gives you more options – you should still optimize the files, but now you have the option of deciding when to load them to make sure that the landing page does not get hit by the added request.

Even though I started with CSS preloading can be done for any resources needed on the page – fonts, scripts, images.

When to preload?

An very obvious case is a search results page. It usually has a very distinct design that requires certain resources not needed anywhere else. But that’s not enough – you need to know when the user will these resources so you don’t go preloading everything just in case. With search it’s when they focus inside a search box – they start typing the query, while you start preloading the resources in the background.

Other obvious places to preload resources:

  • My account – when users click log-in or focus into the login form
  • Checkout – when users add stuff to their cart

Other less obvious places are landing pages when the choice a user makes turns into many sub-options, especially when products have the same resources used on the content pages.

To the code!

If you’re already using a library it probably provides AJAX methods and event methods. If not, you can search MicroJS to find one and adapt the syntax.

A simple preloader is almost as simple as helloWorld – the only thing you need to make sure is that the type of data type is set to text so that it does not get executed.

window.preload = function (src) {
	$.get(src, null, null, 'text');
} // call with preload('somefile.css');

If you want to allow the loading of multiple files at once you can detect if the passed element is an array.

window.preload = function (data) {
	var a = $.isArray(data) ? data : [data];
	$.each(a, function (i, n) {
		$.get(n, null, null, 'text');
	});
}
// call with preload('somefile.css');
// or preload(['somefile.css', 'otherfile.js']);

If you prefer to call the function with many parameters you can just use arguments in the each function call.

window.preload = function () {
	$.each(arguments, function (i, n) {
		$.get(n, null, null, 'text');
	});
}
// call with preload('somefile.css');
// or preload('somefile.css', 'otherfile.js');

Key element is to load the resources after the window.onload has happened – this means that any resources needed for the page to function properly have been loaded. If you do stuff sooner you might have to wait for other resources like fonts, images, videos. This means you need to know if the onload event happened – if it has, preload immediately, otherwise wait for the event to fire.

(function () {
	var win = window,
		$ = window.jQuery,
		$win = $(win);
	$win.load(function () {
		$win.data('loaded', true);
	});
	win.preload = function (data) {
		var a = $.isArray(data) ? data : [data],
			fn = function (i, n) {
				$.each(a, function () {
					$.get(n, null, null, 'text');
				});
			};
		if ($win.data('loaded')) {
			fn();
		} else {
			$win.load(fn);
		}
	};
}());
// call with preload('somefile.css');
// or preload(['somefile.css', 'otherfile.js']);

As you can see I also did a few other things – wrapped the code into a function to isolate all the variables. I also assigned window.jQuery to a local $ to make it a bit more bulletproof.

Needless to say this script needs to be loaded during the load stage. If you intend to load it afterwards you need to make sure that you properly detect the onload event – if you don’t nothing will get preloaded as it will be waiting for that event to fire.

Taking your webapp offline

Sunday, August 21st, 2011

I’ve recently done a few things that would benefit from having an offline mode. Empowered by Appcache Facts I tried to make the newly published La Vuelta 2011 results page an offline-capable app.

Goal

The goal of this exercise is twofold:

  1. To force caching of resources that slow down the loading of your page.
  2. To actually be able to use the app when offline.

Solution

It’s as simple as writing a cache manifest. It pays to name it [something].manifest, as Apache already serves the correct header with that suffix. What you do is list all the files you want cached, add stuff you allow on the network (check Appcache Facts and Dive Into HTML5 – Offline Web Applications) and it all works. Except it doesn’t.

CACHE MANIFEST
# version 1

CACHE:
resource.js

NETWORK:
*
http://*
https://*

AJAX stuff

If you’re using AJAX to get data from a server using jsonp/script, your framework of choice (jQuery in my case) will probably default to using a no-cache approach to loading it. This will mean that it will request the file + some suffix to prevent browser caching. This will mean that the resource will not be available if you’re actually online.

You can use navigator.onLine to switch cache on/off, but I suggest you first try requesting the no-cache resource and if it errors out, request the cached resource. The benefit is that even if you are online but the server is not, the users will still see the data / use the app.

$.ajax({
	dataType: 'script',
	url: 'resource.js',
	cache: false,
	success: successHandler,
	error: function () {
		$.ajax({
			dataType: 'script',
			url: 'resource.js',
			cache: true,
			success: successHandler
		});
	}
});

iPad issues

Fixing the AJAX meant that it worked properly on the desktop (test with Firefox that has offline mode) and in Safari on the iPad. The trouble started when I added the app to Home Screen – data failed to load. It was the same app as in Safari and it should have worked.

After some debugging I found out that the data actually was loaded but the eval failed (I was using $.getScript). Some weird testing showed that the problem was a newline character in the data. As I really liked the newline there I added some code to the error handling that removed the newline and evaled the data, then ran success. And it worked!

$.ajax({
	dataType: 'script',
	url: 'resource.js',
	cache: false,
	success: successHandler,
	error: function () {
		$.ajax({
			dataType: 'script',
			url: 'resource.js',
			cache: true,
			success: successHandler,
			error: function (xhr) {
				eval(xhr.textResponse.replace(/\n/g, ''));
				successHandler();
			}
		});
	}
});

Debugging

It’s somewhat hard to debug offline stuff. I suggest using a clearly visible version indicator in the file to make sure you know which version of the file you’re looking at. Also remember that the first time you load the app after changing the file & cache manifest it is served from cache. At the same time the manifest is checked and on the iPad the files are downloaded after everything else is done (app finished loading and rendering).

It works!

After these problems were solved I used the aforementioned navigator.onLine to hide stuff that normally comes from the network but is not relevant to the offline app (banners, share links, like/+1 buttons) and you can now check the La Vuelta 2011 results page.

Form protection

Friday, August 5th, 2011

I’ve seen a discussion recently on how to protect your forms from spammers/bots that come and fill the forms to either fill your database with crap data or fill your page with porn links. When I read the answers I figured out that none of the people read the amazing article I did years ago, so I decided to try to remember what it said. So, a big fat disclaimer: I read this in an article somewhere and I don’t remember where. If you know the original article please post it in the comments, I’d love to link to it, I bet it has way more info than this one.

The problem

Almost all websites now have some forms on them, some of them are contact / registration forms, others use the data submitted and display it on the site itself (comment forms). But letting others submit data to your site/database opens you to all sorts of attacks. If you actually show the content of the submitted form, you’ll get a bunch of spammers posting comments with lots of links. If you only store data and not show it anywhere you’re still at risk – if you don’t notice your disk can fill up, your database may grow beyond its limits,… So what we want to do is to prevent bogus form posting.

Spammer approach

If you think about writing a spam-bot that will try to spam as many sites you possibly can you have two basic approaches.

Record / replay

This is a very simple approach – you use a person to submit the form, preferably with something that looks like real input and record the request made. Then you hand that data off to the bot, it changes some content and tries to resubmit it.

Automation based on heuristics

I wanted to say AI, but it really isn’t. What it is is a set of simple rules and randomized values that the bot thinks might trick your site into accepting the submit. Let’s say you have three fields, two are inputs with field names “name” and “email” and the third field is “comment”. A simple script can fill these with “valid” data and try to submit it.

Human entry

By far the simplest, but also most costly for spammers. Go on Amazon Turk or whatever other service, send a link to a Google Spreadsheet and have people manually enter the stuff into your forms. This is the source of “Sorry, this is my job” endings to spam comments.

Popular solutions

Turing test

Add a field to the form that the user must fill with something that humans can do easily, but machines can’t. The biggest group here are Captchas (display an image with distorted characters, possibly an audio link that reads out the same characters, and have the user somehow figure it out and write the solution), but there have been others, like a “Human?” checkbox, or “3 + 4 =” fields, “Is this a kitten?” with a pic next to it.

2-step process

Supposedly by far the easiest way to do this is by introducing a 2-step process. After the initial submit, you get back a preview of the submitted data, possibly with a checkbox that says “Is this correct?” and another submit button. Robots are usually not good at following through and thus don’t actually submit the content.

Both solutions have an impact on user experience. With Captchas it’s sometimes really hard to see what they are and even if they have a “different image” link, it just seems like the owner of the site wants to make your life hell before you can submit your data. The other challenges might be easier on the user, but also easier to figure out if you’re being targeted by bots. The 2-step process works great for comments, that usually don’t have an edit link, so it might actually be good for user experience if done right (not Wikipedia style), but are less appropriate on other types of forms.

Protect yourself differently

These are the techniques that should prevent most bogus form entries from random passing bots, except “Human entry” – no protection for that, even though Captchas try hard. There is not much you can do when you’re targeted…

Honeypot field

Use this field to trick autoguessing bots to submit something in a field you know should be empty.

  • Add an input field to your form with a regular name (state, maiden-name,…) that does not appear on your form otherwise.
  • Use a label that will clearly communicate that it needs to be empty.
  • Hide it with CSS, preferably not by adding class=”hidden”.

If the form post includes content in this field discard it and redirect back to the form. The trick is to make sure the bots don’t figure out this is a honeypot, so use valid looking but nonsensical classes…

Date field

Use it to prevent resubmit of data too far from the creation date. Allow users a few hours to post the form.

To prevent manual modification you can use either proper encryption (symetric or asymetric) that will allow you to decode it on form post or use this date in combination with the onetime token.

Onetime token

Use this field to prevent replay of request data. If you can, save it into the database.It is a good idea to make this token in a form that it cannot be faked (say one character changed ad you have a valid one). This can be done with hashing data or encryption.

This one can be as tricky as you want. What I usually do (disclaimer: I don’t know much about encryption so this might be crap advice) is use a plain datetime field with the onetime token generated from IP address, UserAgent and the date field with HMAC. There is no need for this token to be reversible – I can recreate the same thing with the data from the form post and check if it matches.

When using these techniques make sure you take care of the user experience. If you detect a problem on what might be valid user input (“timeout” on the date field with a non used onetime token, wrong onetime token from an ip change by the service provider), you might want to display a second step from the “2-step process”. Whatever you do, don’t call your users spammers or bots – be nice, bots don’t read the text anyway.

Did I miss anything?

I know of no plugin that uses all of these techniques, but I haven’t really looked for it. What I do know is that I don’t want to ever use a Captcha, cause it often keeps me out, and the 2-step process in just too weird sometimes. Hope this helps. And again – if you find the original article (must be some 5 years old now at least if not more) or have any other solutions you use or endorse, do leave a comment.

An Event Apart Seattle review – day 1

Wednesday, June 2nd, 2010
An Event Apart Seattle
Image by Heather L via Flickr

“This is your pilot speaking. We’ve been notified that the passenger bridge has a flat tire.” were the first few words after landing in Chicago, the third airport of the day. I left Ljubljana at 7:15 CEST towards Amsterdam, switched planes and continued towards Chicago. Fortunately the issue with the gates was small enough not to endanger my connection for the last leg – to Seattle, where I landed around 16:15 PST (around 18 hours after taking off from Ljubljana).

I came to Seattle to attend An Event Apart, a conference I wanted to attend since it was first announced. Meeting people like Jeffrey Zeldman and Eric Meyer and learning from them is just amazing. But first I needed to get to the hotel in downtown Seattle and get some sleep.

After a really long day the light rail ride from the Sea-Tac airport to downtown Seattle was really amazing. Going through the suburbs, enjoying the displays of American culture – the highways, the trucks, the architecture, the people. There were only a few of us on the train at the first stop, but at the next stop loads of people got on wearing bright green shirts, scarfs and even a few kids with their faces painted green/blue – fans of Seattle Sounders FC. I thought to myself – nah, it can’t be soccer.

As I arrived a day early I had a day to spare to see the city. I woke up late and then visited The Space Needle – amazing views even in cloudy weather. I didn’t take the old-school monorail built for the world fair in 1962 thinking I’ll do that some other day. After registering at the wonderful Bell Conference Center (thanks to Marci for resolving the issues and sorry I woke you up Gašper) I walked through town to the Pike Place Market and to the high street stores – and wandered into a huge Anime convention (Sakura-Con) and a bunch of kids (not even teenagers?) wearing totally inappropriate clothes.

The day ended with a karaoke meet-up set up by Jeff Croft. I met Mike Davidson of the Newsvine fame (thanks for the beer!) and I heard Andy Clarke and Jeremy Keith sing Ace of Spades together.

The conference started on Monday with breakfast – a really good one. And then two days of talks and an additional day of workshops. I’ll review them in different depth.

Jeffrey Zeldman – Put Your Worst Foot Forward

I wanted to see Jeffrey talk for some time now. I also got to meet him just before the conference started which made me want to see this even more as he’s a really friendly guy with years of experience to share. And the talk proved to be all that and more. Explaining his mistakes from the past and the ways he is solving them – teaching what to do with anti-patterns (to quote Jeremy Keith) was really effective and I think we were all nodding as it seems we all do the same mistakes.

The checklist

  1. Know before you go.
  2. Keep expectations on track and in sync.
  3. Constantly course-correct.
  4. Tell the TRUTH.
  5. Phrase it from the client’s/boss’s point of view.
  6. Report bad news before the client/boss notices it.
  7. Have a recovery plan.
  8. Apologize-but never denigrate yourself or your team.
  9. Have an exit strategy.
  10. Know when to quit.

Takeaways

Working with clients is a long distance relationship – away from sight, away from heart. You need to put more energy into syncing and you need to make sure you see things with their eyes. And as in any relationship – you need to know when to leave.

Nicole Sullivan – Object Oriented CSS

Nicole used to work for Yahoo! and recently helped Facebook optimize their stylesheets, so you might say she has some experience in building and maintaining CSS systems. But unfortunately it also means that a lot of us cannot relate to some stuff she is saying. One of the first thoughts I had was that she might be a good person to write the “CSS – The Good Parts” – she even quoted Douglas Crockford in her presentation.

Controversial

There were a few points that I couldn’t agree with when she said them and decided that I will think about them later. I’m not saying they’re bad practice, I just don’t think they’re good advice for most of us.

  1. Don’t use specificity was one of the things that seem like throwing away a really powerful tool because some people can’t handle the power. I could probably agree with this in big systems, but it sounds like one of the reasons to adopt Java – it’s easy for beginners to start doing productive stuff and hard for them to screw things up.
  2. Don’t use .class1.class2 as that causes all sorts of cross-browser issues. I would classify this as good stuff but it seems only IE6 is affected. So I couldn’t care less…
  3. Hacks shouldn’t change specificity as you’re not using specificity at all. That means that Modernizr and all other tools that add a class to the HTML/body elements are out of the question. The solution – using _property:value; – was something I don’t feel good about – using such invalid hacks just seems wrong.
  4. To define headers use h1, .h1 {} and in HTML use <h2 class=”h1″>…</h2> if necessary. That just seems wrong even though I agree that reusing styles is important.
  5. Avoid specifying location when targeting elements. When you do that moving an element into a different context loses the styles.

Good stuff

This list is what I think can mostly be used today for most of the people writing CSS. It is not a set of rules to abide in every case, but it should be your main modus operandi.

  1. Reuse code as much as possible. If you’re copy pasting, you’re doing it wrong. One of the ways to do this is by following the second rule.
  2. Don’t use ids, inline styles and !important to write easily applicable code. You should not write location specific code. Don’t use .sidebar ul, but rather add a class (eg. sidenav) to that ul and use .sidenav for the rule. Smaller CSS yes, but it will also get you bigger HTML (and classitis?).
  3. Think in modules and provide styles that are easily reusable by just using a class name in HTML. Only elements that are strictly bound to modules should have location specific selectors (but with .class, not #id).
  4. Put defaults into .class and use elm.class to apply specifics. Many elements can have .error – and all errors should have a similar look, whether they’re divs, lists or paragraphs.

Wish-list

  1. Variables are something a lot of people want. What I want is for them to be simple enough that people can’t abuse them to make CSS a programming language. The proposed syntax:
    • To set the variable: @variables hex {myblue:#006;}
    • To access the variable a {color:hex.myblue;}
  2. Prototypes are a really good way of providing defaults to a lot of elements at once and gets rid of rules that have many comma-delimited selectors. The proposed syntax:
    • Set a prototype with allowed child nodes: @prototype .box {margin:10px;children:b,.inner;}
    • Add styles to child nodes: .box .inner {position:relative;}
    • Use a prototype: .weatherBox {extends:.box;}
    • Under the hood this translates to: @prototype .box, .weatherBox {…} .box .inner, .weatherBox .inner {…} .weatherBox {…}
    • Also allows checking code: .leftCol .inner {color:red;} is invalid as .inner is part of .box prototype and .leftCol does not extend it
  3. Mix-ins were skipped in the presentation as she was running out of time. You can think of them as small pieces of repeatable code that is only set in one place and used in others. Syntax:
    • Set a mixin: @mixin .clearfix {zoom:1}
    • Any selector that matches the mixin selector modifies it: .clearfix:after {content:”.”;display:block;height:0;clear:both;visibility:hidden;}
    • Include a mixin: .line {include:.clearfix;}
  4. Prototype sub-nodes were also skipped. They seem to allow calculations based on values defined in different sub-nodes of prototypes – they’re not meant to access computed style:
    • Use calculations: .box .bottom {height:5px;} .box .bl {height:10px;margin-top:.bl.height-.bottom.height;}

Some of these changes will require us to write code for new and old browsers independently or to write a “compiler” that will compile code for older browsers. Is there one already written?

Takeaways

Building a CSS system means thinking about the selectors (and not the properties) and Nicole probably knows more than anyone else on that subject (to make you feel more comfortable, Jeremy Keith of Clearleft said they arrived to the same conclusion independently). Another, probably even more important takeaway is that you should think about flexible modules – sometimes stuff is more similar that it might seem at first. If you write CSS for a module that supports variations you’ll write less code that will apply faster and your visitors will be happy. If you want to look into an Object oriented CSS framework – check Nicole’s project OOCSS project at GitHub.

Dan Cederholm – The CSS3 Experience

Dan told us that we can and should use CSS3 now in non-critical areas such as experience, visual rewards, feedback and movement for users with the latest & greatest browsers. Not so much progressive enhancement as progressive enrichment.

Some ideas for use of CSS3:

  • Hover on items with RGBa background, a text-shadow and a border-radius with a transition (Sam Brown style).
  • Hover with opacity change. Create a single image, make it transparent normally and less transparent on hover. With a transition of course.
  • Multiple backgrounds to achieve a Silverback parallax effect.
  • Enriching form elements with a background gradient and border radius.
  • Making form buttons prettier with text-shadow, border-radius, box-shadow and a background gradient. Animate the focus styles.
  • Use scale transform with box-shadow and a transition for hover on images in gallery.
  • Rotation on hover for a single degree with a transition.

Takeaways

You can use CSS3 today, but know what others are missing so they don’t miss critical visual cues. Be subtle with these things or we’ll end back at using transitions to make stuff blink.

Luke Wroblewski – Mobile First!

Web products should be designed for mobile first. (Even if no mobile version is planned.)

Mobile is a big opportunity for growth, but you need to think about different things than when you’re doing web development like:

  • Multiple screen sizes and densities
  • Performance optimization
  • Touch targets, gestures, and actions
  • Location systems
  • Device capabilities

Designing for a smaller screen size will make you focus on core actions. To do that you’ll need to know your users. You should focus on iPhone, not (only) because of its popularity but also because it sets the design expectations very high. It also doesn’t allow any hidden features that hide in menus pressed by buttons – everything needs to be on the interface. When designing you should define device groups, create a default reference design and define rules for content and design adaptation – opt for web standards and a flexible layout. Technically you need to take care that you reduce requests and file size. You should take advantage of HTML5 that allows you to cache things locally and gives you the canvas tag that might sometimes be smarter than loading images. Think outside your web box – less cross browser issues means some new tricks come into play (like data URLs).

The context of using mobile applications is different. It’s not a long time sitting in front of a computer but rather quick bursts of attention everywhere, using mostly just one hand.

Mobile is innovating fast and you should think about the new capabilities to innovate yourself. Touch interfaces mean no hovers, thinking about bigger touch targets and a bunch of gestures that differ from platform to platform. Location information (from GPS, WiFi, cell towers or IP) is almost ubiquitous and can be used for positioning and filtering, but you should not forget other innovations that are less obvious like orientation information, audio & video input and output, compass, push notifications, Bluetooth connections, proximity sensors, ambient light detectors,…

Takeaways

You need to think about mobile because it’s an opportunity for growth, the constraints will give you the focus you need to make a great product and the capabilities will drive innovation in your product. But don’t forget that the design considerations are different.

Aarron Walter – Learning To Love Humans—Emotional Interface Design

There’s a lot of talk about usability of web pages, but is this enough? Usable is just edible. Would you say you go to the restaurant because their food is edible? We have a few options on how to trigger an emotional response to our designs – one of them is giving our sites personality. It’s a platform for emotional response as we like to empathize and personality invites empathy.

People will forgive shortcomings, follow your lead and sing your praises if you reward them with positive emotion.

You can use treats to give users something more. Let users discover new things. It’s the little positive surprises that make us happy.

Takeaways

Usability is not enough, we need to think about designing pleasurable experiences. We need to create an emotional response from our users and make them want to come back.

Jared Spool – Anatomy of a Design Decision

How do we make design decisions and what kind of designs exist? There are a few decision styles:

  1. Unintentional design – when users will put up with whatever we give them and we don’t care about support costs and frustration (think airlines & hotels).
  2. Self design – works great when users are like us and we are our own users (think 37signals).
  3. Genius design – when we have domain knowledge that informs our decisions and we’re solving same design problems repeatedly.
  4. Activity focused design – when we can identify users and record their activities to go beyond our previous experiences.
  5. Experience focused design – when we want to improve our users’ complete experiences, in between specific activities.

There are ways of moving up the chain:

  • “Eat your own dog food” to get from unintentional to self design.
  • Do usability testing to get from self design to genius design.
  • Field studies get you from genius design to activity focused design.
  • Personas & patterns help you get to experience focused design.

There are two fundamentally opposite ways we can make decisions:

  1. Rule-based decisions are based on design books, brand identities and other rules. They don’t allow exceptions and ignore the knowledge of the person deciding.
  2. Informed decisions are based on design patterns and put the person deciding behind the wheel. They are good for handling exceptions.

With this in mind we can look into what is needed to do one or the other:

  1. Dogma
  2. Methodologies
  3. Process
  4. Techniques
  5. Tricks

The first two are typical for rule-based decision making as they rely on a set of rules and don’t require a lot of knowledge from the person deciding. Techniques and tricks on the other hand come with experience and a lot of domain knowledge.

Takeaways

You need to know which decision style you’re using and encourage informed decisions, avoiding rule-based decision making. Techniques and tricks are more effective than methodologies and dogma even though/because they’re harder to come by.

Pete LePage – Help Us Kill IE6

A sponsored talk that didn’t really turn out as bad as some I’ve seen at other conferences (eg. FOWD). Pete presented the history and some IE9 features. He also suggested that we let IE6 users know that they might want to upgrade their browser as Facebook does.

MediaTemple Party

The party was nice – being fashionably late meant that it wasn’t too crowded and that most of the snacks had already gone. I had a brief chat about designing Drupal 7 with Mark Boulton, met Aarron Walter and Petra Gregorová formerly from Slovakia and a police man from Denmark that does web development in spare time. And I grabbed a (mt) beer and a coaster as a souvenir.

Enhanced by Zemanta

Fluid searchbox

Sunday, October 4th, 2009

There’s been a lot of fluid layouts recently. When you use a fluid layout it’s hard to make everything fluid as you need to stretch certain elements and have other elements fixed.

I was approached some time ago by a designer who was working on a fluid design but was having problems with a HTML/CSS only solution for a fluid searchbox. The idea to create a searchbox that grows when the window grows is simple, but becomes a tad more difficult when you add stuff to the left and right of it, some being fixed width (width in px or em) and some fluid width (width in %).

As I like challenges I took it on and produced a sample that worked on most modern browsers in a single evening. They didn’t like it because it didn’t work in IE7 (cause I never checked it) even though the fix for IE (it was only IE7 that didn’t work) was trivial. After a while I went back and checked it out again, changed the code a bit and I think it’s much better now.

The simple form

Creating a fluid search box when you only have a single element next to it is trivial. What you do is wrap the input in an element and use padding to create space for the fixed element, then position the fixed element absolutely (or relatively) in the space created by the padding.

HTML:
<fieldset>
	<div>
		<input type="text" name="q" value="" />
	</div>
	<input class="s" type="submit" value="Search" />
</fieldset>
CSS:
fieldset {position:relative;padding:0;}
div {padding-right:6em;}
div input {width:100%;}
input.s {width:5em;font-size:1em;position:absolute;right:0;top:0;}
Sample

You could target the input with input[type=submit] but that wouldn’t work in older browsers.

A complex form

The complex form from the intro includes a title, a search type drop-down (select element), the input box and a submit button. This gives us four elements, two of which are fixed and two fluid – and more stuff to hack around.

HTML
<fieldset>
	<div class="l">
		<span>Find</span>
		<select name="type">
			<option>in Books</option>
			<option>in DVDs</option>
		</select>
	</div>
	<div class="c">
		<input type="text" value="" name="q" />
	</div>
	<div class="r">
		<input type="submit" value="Search" />
	</div>
</fieldset>
CSS
fieldset {margin:0;padding:0;position:relative;border:0;}
fieldset .l {height:0;}
fieldset .l span {width:3em;display:inline-block;}
fieldset .l select {width:10%;}
fieldset .c {margin:0 60px 0 4em;padding:0 .7em 0 10%;}
fieldset .c input {width:100%;}
fieldset .r {position:absolute;right:0;top:0;}
fieldset .r input {width:60px;}
* html fieldset .l {float:left;width:100%;margin-right:-100%;}

The basics

I created three elements that help me align elements. I’m using classes l for left, c for center and r for right. The left element includes the title and the type, the central element includes the search box and the right element includes the submit button.

fieldset .l {height:0;}
fieldset .r {position:absolute;right:0;top:0;}

The left element has its height set to 0 so it seemingly doesn’t take any space. The central element includes the input and needs no special positioning, while the right element is positioned similar to the simple solution – absolute and set to right.

The surrounding elements

You can set the width of these elements in any unit you like. I set the width for the span element in em, type drop-down in % to make it fluid and submit button in px.

fieldset .l span {width:3em;display:inline-block;}
fieldset .l select {width:10%;}
fieldset .r input {width:60px;}

The only thing you need to do in CSS is set the width to these elements. You need to set the display property on the span element to work around the fact that inline elements don’t allow setting of width.

The fluid input

fieldset .c {margin:0 60px 0 4em;padding:0 .7em 0 10%;}
fieldset .c input {width:100%;}

The input needs its width set to 100% so it will stretch the whole available width of the parent. That happens due to the fact that block-level elements will grow to 100% automatically and the margin and padding will “shrink” its contents. This allows setting the spacing in two different units – one for margin and one for padding. If you look at the CSS rule, you’ll see that the widths are similar to those set for the surrounding elements – of course with some spacing added.

  • Right: 60px margin (width of the submit button), .7em padding (spacing)
  • Left: 4em margin (3em for the title + spacing), 10% padding (type select width)

The browsers

You can check out a sample page with the complex form. Working on Windows I’ve tested it in these browsers:

  • Firefox: 2.0, 3.0, 3.5
  • Opera: 10
  • Safari: 4
  • Google Chrome: 3
  • Internet Explorer: 8 (with its rendering of IE7 Standards and Quirks)

As always at least one browser had to fail – IE6 did. The problem was height:0; as IE6 will render elements as high as their content. We can work around this by changing the way we make it zero width/height so it doesn’t affect the central element.

* html fieldset .l {float:left;width:100%;margin-right:-100%;}

I’m using the IE6 CSS filter to target IE6, you can use conditional comments or whatever you like best. The rule floats the element left, making it 100% wide, but resets the position by setting the right margin to -100%.

As I’m using display:inline-block; this will work a bit different on browsers that don’t support inline-block. In Firefox 2.0 you can use display:-moz-inline-box; in the rule for the span, but make sure you put it before the inline-block.

fieldset .l span {width:3em;display:-moz-inline-box;display:inline-block;}

Last words

You might want to fix some stuff in some browsers so everything aligns nice. The techinque used is derived from the multi-unit any-order column layout by Eric Meyer as it opens your mind on how to use multiple units in a single layout without fuss. I’ve tried all the browsers I have on my dev machine and I’m not saying it works everywhere. If you find a browser where it doesn’t post a comment; if you find a better way of doing things also post a comment.

Reblog this post [with Zemanta]

Developing for Opera

Thursday, July 16th, 2009

I’ve recently put a lot of time into Zemanta stuff working in Opera. There are a few things I’ve noticed that really bother me as a developer when developing for Opera.

OperaImage via Wikipedia
  1. The easiest way to be sure you’re getting a non cached file is to actually have it open in a tab and reload that tab. Emptying the cache does not seems to work as I’ve found out while caught in an alert loop.[1]

  2. I found the “don’t run scripts on this page anymore” checkbox in the alert box fascinating, but less so after I clicked it and couldn’t find a way to turn scripts back on for that specific page. Fortunately restarting the browser did the job.

  3. We load a loader.js that in turn loads other Javascript and CSS files. Unfortunately it seems I have to manually load these files in tabs in order to get them to work – even though I can see them in Dragonfly when I click them they seem to be blank. When I reload, the content appears. After that they work as expected. But even then I don’t see them loaded in the Network tab of Dragonfly.

  4. Can’t get the CSS loaded from a script to work. I don’t have any ideas, they just don’t work. I don’t see them in Dragonfly network tab (but I don’t see Javascript files either and they work).

  5. When using Dragonfly I cannot get out of the inspect DOm mode so I can’t use the site to do something while watching what is happening in the DOM – what you must do is switch to a different tab, do your stuff and then switch back.

  6. When you look at errors on a WordPress page Opera spits out loads of “-… is an unknown property” – thanks very much but properties that start with a – are supposed to be unknown to most of the browsers as they are vendor specific. This means that finding the error you’re looking for is much more difficult than it should be.

What I really hate the most when working with Opera is the lack of information about what is going on. They’ve added some really nice features in the recent releases but it is still quite far away from being a browser that people develop for. In this way it is sort of similar to IE but IE is a must and Opera isn’t…

If any of these are my mistakes I’d be glad if someone set me straight and taught me to use Opera as a developer. I bet others would too.

  1. When you have a loop that alerts something and you keep getting the alert – the only way of getting out is removing the alert from code, emptying cache and trying to hit reload between alerts. In Opera this does not seem to work as the only way to get the new file is to reload the file and you can’t do that between alerts. back
Reblog this post [with Zemanta]