Posts Tagged with CSS

Box Models and Quirks Mode

Like nearly every web designer and developer on the planet Internet Explorer has long been the bane of my existence. Each version consistently botches W3C specifications in some way, leaving out support for or mishandling technologies and features that other browsers have supported for years. Countless hours have been wasted creating browser check scripts, workarounds, and CSS hacks to support an outdated piece of software that many people still refuse to abandon despite obvious (and harmful) deficiencies.

There are, however, two things that Microsoft did correctly with Internet Explorer. The first is conditional comments, which allow knowledgeable web designers and developers to gracefully hide any CSS hacks or special IE code in simple HTML comments that other browsers ignore.

The second, and slightly less renowned, is the IE box model.

Understanding the box model can take a little effort if you aren’t already familiar with web design and development, so let’s use an example. We’re going to have a friend build a box for us, and it needs to be four feet wide exactly. We need exactly one foot of space around whatever it is we’re going to stick inside this box for padding material. And just for the sake of argument, let’s say that the box we’re building has really thick one-inch sides (it’s an awesome box).

Our box-building friend needs certain information from us to build this box, specifically the width, the padding (space between what we’ll be putting in the box and the box edges), and the border (or sides) of the box. We know that our box border is going to be an inch thick, and we want a foot of padding. We also know that the box has to be exactly four feet wide. How wide is the box?

If that looks like a trick question, you’re right. Anyone with any sense would say that the box is four feet wide. If we were to describe our box using CSS, it’d look something like this:

.box {
 border: 1in solid;
 padding: 12in;
 width: 48in;
}

How wide is the box? According to the CSS specification, the box we just created is actually six feet and two inches wide. This is because CSS defines the total width of a box as the width plus the padding plus the border (48in + 12in * 2 + 1in * 2). To get a box that is exactly four feet wide, we actually need to do this:

.box {
 border: 1in solid;
 padding: 12in;
 width: 22in;
}

As you can imagine, this is incredibly counter-intuitive. It also prevents us from doing cool things with relative dimensioning. Let’s try a more abstract example, shall we? We need two boxes this time, and like our last box we want a foot of padding around the contents of our boxes. They also need to have those one inch sides. We’re going to stick them into a storage room, and each box needs to take up exactly half of the width of the storage room. Unfortunately, we have no idea how large the storage room is going to be. All we can tell our box building friend is that each box needs to be half the width of the storage room (or 50%). How wide are our boxes?

Again, this probably looks like a trick question, and it is. Regardless of the size of our storage room, our boxes need to be exactly half as wide as the storage room, or 50% of the storage room width. If we were to describe these boxes using CSS, they might look something like this:

.box1,.box2 {
 border: 1in solid;
 padding: 12in;
 width: 50%;
}

But wait! We already know that CSS determines the total width of a box by adding the width, padding, and border together. That means that these boxes are actually half the width of the storage room plus another two feet and two inches. This is also where the CSS spec fails us, as there’s no way to figure out what the actual “width” of these boxes without knowing the exact width of the storage room.

This is also where the IE box model steps up to the challenge. The IE box model is actually a rendering bug, one that has resulted in at least a few CSS hacks to alleviate. It also makes a lot more sense than the CSS spec, however, because when we tell Internet Explorer that the width of the boxes is fifty percent it assumes we mean the total width. Any additional padding or borders are subtracted from this width, not added to it. With the Internet Explorer box model, we can get exactly the boxes we need, regardless of the size of our storage room.

All modern browsers (including IE8, shockingly) support a “box-sizing” property of some kind that allows us to us the IE box model for creating boxes. Ironically enough, the only browser that doesn’t support “box-sizing” is Internet Explorer 7 and below. And unfortunately, the IE box model bug was corrected in Internet Explorer 6 (previous versions are essentially always stuck in Quirks Mode). So what if we want to use this more flexible, intuitive way of creating boxes in more recent versions of IE?

The answer is Quirks Mode. It’s actually really easy to get Internet Explorer 6 into Quirks Mode; including anything before the DOCTYPE declaration will do it, and the XHTML specification states that an XML prolog should be included prior to the DOCTYPE declaration. By sticking this simple, completely valid line of code into our documents, IE6 switches over to Quirks Mode and we get the desired box model.

Quirks Mode also “corrects” a number of other IE rendering issues. In fact, by forcing Internet Explorer into Quirks Mode, we’re essentially limiting our design and development needs to only two browsers: standards compliant browsers and legacy (Internet Explorer) browsers. Quirks Mode has it’s own set of, uh, quirks, but overall it’s much easier to correct for these than to spend hours implementing various hacks that may or may not break with the next version of Internet Explorer.

My Selectors, Let Me Show You Them

Work continues on the MORPG. I’ve slowed quite a bit, largely because the burst is over, and partly because I want to make sure what I’ve done so far is good enough that it won’t cause trouble later on when I start getting into the really complex stuff (heh). This is good, because it’s already shown me a few things I could have done better, and I’ve gone back and improved them.

One thing that had me stuck for a bit was the character creation page: users get to select their characters class (Fighter, Caster, or Balanced) and an elemental affinity, using big ‘ol buttons that are kinda’ cool and light up when you hover over them and stay lit up when you click one to select it. It’s clean and simple and works really well; it’s very elegant. At least, that was the idea. Getting the stupid buttons to stay lit when a user clicks is a bigger pain than it should be: it requires javascript wizardry, and mine–while adequate enough to accomplish this simple trick–isn’t good enough for my own standards. Sure, I can get it to work, but the code is ugly and cludgy. Javascript tends to look that way to me anyways, but I’m not a huge fan of most of its uses (’cept AJAX, AJAX is spiffy). Here’s the code I used to highlight our buttons:

function $(element) {
 return document.getElementById(element);
}
function strip(element) {
 $(element).className = '';
}
unction activate(group,object){
 if(group == 'class'){
  strip('fighter');
  strip('caster');
  strip('balanced');
  $(object).className = 'active';
 }
 if(group == 'affinity'){
  strip('holy');
  strip('earth');
  strip('air');
  strip('neutral');
  strip('fire');
  strip('water');
  strip('shadow');
  $(object).className = 'active';
 }
}

Notice that those first two functions, $() and strip(), were created only to clean up the code in the third function, activate(). If I hadn’t done that, the code above would be an even more jumbled mess. So we have an ugly script that keeps our button lit up when a user clicks it. Is there a better way? Perhaps one that doesn’t even require the general ugliness of javascript?

There is. So bleeding-edge, in fact, that I both didn’t know it existed and was not surprised at all to find out that it doesn’t work in any version of Internet Explorer. Here’s how we do it:

input:checked+label {/* style rules */}

…that’s it. What took three functions in javascript takes one simple selector in CSS3

This method does have it’s own problems. No version of Internet Explorer supports it. Firefox 3 Beta 3, Opera 9.25, and Safar 3 Beta do, but I doubt earlier versions of those browsers do (well, maybe Opera). It’s so much simpler, though, that I’m hard pressed to really care.

Update: The character creation page is finished (for now). I ended up wiping all registered users while testing it, though. If you registered before, you’ll need to register again… or just wait until the thing is finished. ^_^

The Power of Print CSS

Another fine day of classes, yes? Intro to Computer Literacy wasn’t quite as bad as Web Application Design, so I should be able to get through it. It certainly helps that the class is only one and a half hours long (as compared to Web Application Design’s three hour length). The instructor said we would need the book, but didn’t mention anything in particular that we would be doing out of it, so I’m hoping I can get by without purchasing it. In an interesting twist, one of the three required books for Accounting I isn’t actually required for the particular class I’m taking, so I can take it back and get some of my money. Always a good thing (unless I have to go back and spend it on the Intro to Computer Literacy Books).

Interestingly enough, the only paper assignment I have this semester, coming by way of Intro to Computer Literacy, has given me a great excuse to play with some new site designs. The assignment allows for the paper to be presented in alternative fashions, such as video or graphic art. So, having nothing better to do, I asked if a web page would be suitable. The instructor seemed hesitant, but allowed it, making sure to let me know that I would need to touch base with him as I worked on it. I have a feeling he’s expecting some kind of Geocities prefab site, with all sorts of ad banners and animated .gif’s. It is an Intro to Computers class, after all, and I didn’t really give him any reason to think that I’m anything more than an average computer user (my nefariously wicked plan of world domination is progressing nicely, by the way).

I wasn’t really planning anything fancy–just a single webpage to present the paper in a stylish way. I finished a mockup earlier today and sent off an e-mail letting the professor know about it, so he can look it over and let me know if I’m not getting something (it’s bound to happen).

There are a couple of reasons, apart from the fore mentioned “nothing better to do,” that I wanted to do the paper in the form of a web page. For one, it allows me to test out some of things I’d like to implement here on Fractured Mentality code and design wise. The look is, in my always humble opinion, terribly slick.

A nice side effect of playing around with various sorts of designs was working with more print-specific CSS coding. It still troubles me that hardly any commercial sites, or websites in general, use CSS. It may not always be as straightforward as some would like, but it’s power is undeniable. And let’s face it, laying out a page with nested tables isn’t exactly straightforward.

Ah, but wait! The power of CSS extends far beyond the computer screen. If you’ve ever tried to print out a page on Fractured Mentality, you may have noticed that it looks quite a bit different from the standard, on-screen version. Such is the power of CSS, giving us the ability to alter the appearance of a page for different forms of media (screen, print, projection, handheld, etc.).

Designing screen and print style sheets has become the standard for myself, since the print style sheet rarely takes more than a few extra minutes (if that). With this project paper, though, an interesting idea came to mind: what if I could design the print media style sheet in such a way that it would print the webpage in standard APA style? With a title page, double-spaced body text, a trailing reference page?

With this fairly cool idea at heart, I went to work. The challenge: create a sleek, stylish looking three-column web page that printed out as a no-frills APA-style paper. The result: it works. Really, really well. So well, in fact, that if the professor doesn’t like the webpage, he could just print it out and get a perfectly formatted APA style paper, with title page, double-spaced body text, and a reference page. The only thing it’s missing is the printed page-header text (APA style requires you to print either a shortened title or your last name and the page number in the header of a paper), which can be added through your respective browsers Page Setup dialogue. Very cool stuff.

So Long TextPad

For the past year or so I’ve been using TextPad for all my web-coding needs. I have access to Dreamweaver, but it’s far too unwieldy for my tastes, even as a text-based editor. Textpad, on the other hand, is nice and clean with a code library that helps with a lot of the more redundant tasks (like setting up a new page).

TopStyle, though, seems to be a perfect balance between the two. It has built-in previewing for both the Microsoft (IE) and Gecko (Netscape/Mozilla) rendering engines, a clip library, integrated html and CSS validation (via the W3C), a “Style Checker,” which lets you know which styles won’t work in a fairly large number of browsers, and a kick-ass color selector; it’s not quite Photoshop, but it has some nice features even PS lacks (like complementary color suggestions). And the insight feature is quite awesome; it’s a lot like Dreamweavers, where you start typing a tag or CSS selector and it gives you a list of attributes right there at your cursor.

If you like working in plain text for your web coding, I highly recommend TopStyle. Its default state is a bit heavy, so you’ll probably have to do some customizing before you get everything just the way you want it, but it’s worth it.