Showing posts with label css. Show all posts
Showing posts with label css. Show all posts

Sunday, December 30, 2007

Images and Mysterious Gaps

While rewriting a page on Sepia Mutiny, I found yet another reason why being a site designer is a highly-paid full-time job. It's impossible for anyone else to keep up with browser standards and implementations.

Consider the simple HTML below

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Sepia Mutiny</title>
</head>
<body>
<div style="border: 1px solid black; background-color: red;">
<img style="" src="/sepia/images/SMB3.jpg"/></div>
</body>
</html>

This is a well-formed strict HTML document that validates. You'd be surprised to see what it produces. See below.



If you haven't realized what's wrong, there is a gap below the banner image, before the div tag ends. Why? Probably stupid IE doing it's own thing, is anybody's first guess. Actually this is Firefox 2. Huh? I tried setting margin, padding and any other property that I could think of to 0. No effect. What gives? The same document renders differently in Internet Explorer 7. No gap below the image. Weird. Upon further tests, I made another HTML document.

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Sepia Mutiny</title>
</head>
<body>
<div style="border: 1px solid black; background-color: red;">
<img style="" src="/sepia/images/SMB3.jpg"/></div>
</body>
</html>

What's the difference? Only the Doctype is missing. So of course this is not a valid strict HTML document. Here's how it renders in Firefox?



I spent scratching my head for the better part of this sunday over this. Is Firefox buggy and IE7 getting things right now? Traditionally, sites are designed for non-IE browsers first because they are standards-compliant, and code exceptions/hacks for IE because of all its quirks. Has the situation reversed? After much Googling, I finally found the story behind this mysterious gap. Apparently, Firefox is too good at being standards compliant.

Lesson: Because the html element <img> is an inline element by default, it is rendered with a baseline whose height from the bottom border of its container depends on the font applied to the container. The element's property must be set to block to render the element differently without this baseline (assuming there is no other element such as inline text that may need a baseline within the same container). Thus, re-writing legacy HTML to conform to today's standards will break a template design. This is because a well-formed standards compliant HTML document is rendered by today's A-grade browsers in 'standards mode', whereas badly written html documents of yesterday are rendered by browsers in 'quirks mode'. As some of us who don't have enough background in designing sites usually just wing it, this is going to be a problem because of the all the bad habits we've learned over the years working in browsers' quirks mode. Now doing it the standards way is rather hard because we have to re-learn or rather learn correctly html and css standards.

References:
  1. Eric A. Meyer, Images, Tables, and Mysterious Gaps. Mar 21, 2003.
  2. Eric A. Meyer, Images, Tables, and Mysterious Gaps. Mar 3, 2002.

Tuesday, March 21, 2006

DIVs and Javascript

I just discovered that I cannot manipualte multiple DIVs using Javascript's document.getElementsByName().

Apparently, there is no legal way of using the name attribute for such tags such as div or span, according to W3C HTML 4.01 specs. You must confine the usage of this attribute to such tags as input, img, frame, iframe, form, map, param, meta, object, A, select, applet, textarea, or button.

Click Here.

So remember to use document.getElementsByTagName() instead and go through all DIVs and check for a user-defined attribute to achieve your goal. Although this seems inefficient if you have a lot of DIVs, it's legal and the DOM model should be efficient enough to not cause too much processing time.

Also bear in mind that replacing document.getElementsByName() with document.getElementsByTagName() may not be backward compatible with some older browsers such as IE4.

IE CSS Bugs

Wow! IE Bugs have been so prevalent that there are entire websites dedicated to workarounds. I've found the use for one today.

How To Attack An Internet Explorer (Win) Display Bug
http://www.communitymx.com/content/article.cfm?page=1&cid=C37E0

Table-less div pages are a boon, in the sense that it is very easy to manage / modify. But IE CSS implementation is so buggy that it can become a nightmare.

I had some nested divs that were static, absolute, visible, hidden, floating. Basically a reasonably complicated page for the intended look I wanted. Then certain divs starting behaving erratically when they would disappear along with text inside them. More popularly knows as ...

The Peekaboo bug
http://www.positioniseverything.net/explorer/peekaboo.html

Some basic hacks will help solve the problems.

- Whenever possible, give an explicit dimension to all block elements. This helps IE a lot.
- If you want to keep certain elements liquid, then use the following hacks

{position: relative}

This CSS code solves some IE bugs such as Eric Meyer's Punch Out Demo. Divs contained inside another, get hidden if you try to use them to hide the borders (for example) of the container div (using negative margins for example). By using the above CSS hack, this problem is solved without any side effects on other browsers.

The Holly Hack
{height: 1%}

When you need a liquid div and provide no dimensions to a div, sometimes IE fails to display it properly during resize, etc. This hack helps IE and solves the problem (only when the content actually exceeds the prescribed dimension of the div. Since 1% is small enough for even a little content, it works). Not exactly W3C compliant but it does. But this can have a side effect on other non IE/Win W3C compliant browsers. So we need to apply this and other such IE specific hacks in combination with other hacks.

To hide the holly hack side effect from IE5/Mac, use the IE5-Mac hack

/* Hides from IE5-mac \*/

/* End hide from IE5-mac */

The backslash at the end of first line serves as an escape character in IE5/Mac and any code in between the 2 lines is treated as comments.

Tan Hack

* html .buggybox {height: 1%;}

"* html" is an IE (Win and Mac) only specific CSS element and is ignored by other browsers. So the above code is an implementation of the Holly Hack using the Tan Hack.

Monday, January 30, 2006

CSS Order Matters

Order matters in CSS.

When you define text-decoration for an a tag as follows

a:link {text-decoration: none}
a:hover {text-decoration: underline}
a:visited {text-decoration: none}


visited links do not get the hover style. They continue to use the visited style, since that is the last defining attribute. The following is the right way.

a:link {text-decoration: none}
a:visited {text-decoration: none}
a:hover {text-decoration: underline}