Forum Moderators: not2easy

Message Too Old, No Replies

Box Model Hack?

\width, then w\idth? (does this make sense?)

         

modo

6:48 pm on Feb 21, 2004 (gmt 0)

10+ Year Member



I know this is the "box model hack" that everyone is talking about (which I'm still learning about, since I'm a beginner with all matters CSS):

div.content {
width:400px;
voice-family: "\"}\"";
voice-family:inherit;
width:300px;
}

++++++++++++++++++++++++++++++++++

But what is this?

#sidebar-b {
float: right;
width: 180px;
\width: 188px;
w\idth: 180px;
margin: 0;
margin-left: 2px;
padding: 4px;
background-color: rgb(235, 235, 235);

Longhaired Genius

7:00 pm on Feb 21, 2004 (gmt 0)

10+ Year Member



Never seen it before. Whatever it is, don't worry about it.

choster

7:39 pm on Feb 21, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This is an implementation of Andrew Clover's Simplified Box Model Hack developed for IE5.x Windows, originally posted on css-discuss.

[doxdesk.com...]

DrDoc

7:44 pm on Feb 21, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Never seen it before. Whatever it is, don't worry about it.

But I have seen it... Just because you haven't seen something doesn't mean you should dismiss it without knowing what it does. ;)

As you can see, it seems like there are three

width
declarations:
width: 180px;
\width: 188px;
w\idth: 180px;

Many older browsers (such as Opera 5 and earlier) do not understand rules that contain escaped characters. IE5.x understands rules as long as only the first character is escaped. All modern browsers (including IE6) understands rules with escaped characters, no matter where in the word the character is. (Just keep in mind -- letters A-F cannot be escaped).

Thus, Opera 5 (and other browsers that choke on escaped characters) will pick up the first line (width: 180px;), but will ignore the following two.
IE5.x will pick up the second rule (\width: 188px;), but ignore the last one.
Finally, browsers that do not have the parsing bug (such as IE6, Mozilla, Netscape, Opera 6+) will pick up the last line (w\idth: 180px;).

This hack is usually refered to as the "simplified box model hack". It is more stable than the "standard" box model hack (by Tantek Çelik) and causes less unexpected behavior in non-IE browsers. It is therefore a safer alternative to use as a box model hack (thus its name) even though it can also be used to successfully target older browsers (such as Opera 5).

modo

8:10 pm on Feb 21, 2004 (gmt 0)

10+ Year Member



So, you're saying that I should use it, right?

Strange thing is though, after having looked at so many other sites out there, I've never seen people use it...

Thanks for the info!

graphikjunkie

9:57 pm on Feb 21, 2004 (gmt 0)

10+ Year Member



Also, it's worth bearing in mind that you don't actually have to use any box-model hacks at all if you can avoid using horizontal padding and borders.

To avoid horizontal padding just use margins on child elements instead. (You might need to add a few spans)

To avoid using borders - try a graphical solution.

This is a good way to avoid headaches. :)

mipapage

12:20 am on Feb 22, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



To avoid using borders - try a graphical solution.

I was going to say something, then I saw your nick...
;-]

Khemikal

2:05 am on Feb 22, 2004 (gmt 0)

10+ Year Member



The * selector hack which is based on the box model hack...I posted about it here a number of months ago...is still the superior solution...at least in my humble opinion...due to its ability to degradate nicely, etc.

Khem

graphikjunkie

8:02 am on Feb 22, 2004 (gmt 0)

10+ Year Member



But seriously, if older browsers have a problem with horizontal padding, why not avoid it altogether and save all the hassle of implimenting box-model hacks? We don't even need it.

Khemikal

2:50 pm on Feb 22, 2004 (gmt 0)

10+ Year Member



This really isn't a problem with "older" browsers...it is the incorrect rendering of the box model by Internet Explorer. Unless you consider IE 6.0, the most widely used browser on the planet "old" I'm confused by what the complain is...unless it is just for M$ to get a clue. :)

Khem

graphikjunkie

4:55 pm on Feb 22, 2004 (gmt 0)

10+ Year Member



Actually, it's IE 5.5 and older which incorrectly interprets the box model. IE 6.0 conforms to spec.

So it <em>is</em> the older browsers that we're having problems with.

I just can't see why we need to use hacks at all. Is it just that the solutions is so simple that nobody can see it?

synotic

5:59 pm on Feb 22, 2004 (gmt 0)

10+ Year Member



Actually, it's IE 5.5 and older which incorrectly interprets the box model. IE 6.0 conforms to spec.

So it is the older browsers that we're having problems with.

I just can't see why we need to use hacks at all. Is it just that the solutions is so simple that nobody can see it?

Well the other solution to not using the the box model hack is to add an additional box within your box and use that to apply padding via margins and whatnot. It's just that people find adding meaningless boxes worse than using a hack. And some hacks, like Tantek's actually conform to CSS spec, they just rely on incorrect parsing of older browsers. I've found that sometimes I already have boxes within boxes just because it's how my site is divided and that I also try to avoid adding padding or borders to any box with an explicit width. That's why relying to much on absolute positioning is bad, it's easier imo to just let everything flow on the page and utilize floats, margins and all that.

DrDoc

8:14 pm on Feb 22, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It's a question of hack vs. code bloat.
You know the hack will always work... the code bloat may not.

And, you can't just dismiss it and say we should just do without margins and paddings. While that may be true, there are times when it is not applicable at all. We need padding and margins, but we don't want IE5 to fream out about it.

ergophobe

7:26 pm on Feb 23, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



There is a comprehensive list off most such hacks at

[dithered.com...]

You will find the two in question in the "single declarations" section.

There you will find that the \width declaration will be ignored by
- Win NS4.05 (it may crash the app in Win NS 4),
- Win Opera 3.5, 4, 5
- Unix Konqueror 3.0
- various Mac browsers
- handful of others

SuzyUK

12:30 am on Feb 24, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



or just run a search for the "underscore hack"....

hacks can be learnt from, then if you want to you can dismiss them, you can. Although personally I've found that they usually teach me something along the way whether I actually implement them or not.

So:

So, you're saying that I should use it, right?

It's up to you... if you need to then use it

Strange thing is though, after having looked at so many other sites out there, I've never seen people use it...
I'd be asking why not! not if...

Understand why you/your design needs a/any hack before you ask should I use one.

Suzy

modo

1:17 pm on Feb 24, 2004 (gmt 0)

10+ Year Member



thanks for the advice everyone. i went ahead and used the hack. and it works great!

i just still seem to be having a slight problem regarding layout. in netscape, when i go to a page with more content, my entire layout seems to move a couple of pixels. i suppose there is nothing i can do about this. it's the scrollerbar adding the few more pixels, isn't it?

ram_mac

12:24 pm on Feb 25, 2004 (gmt 0)

10+ Year Member



I believe the second hack is 2px wrong:

width: 180px;
\width: 188px;
w\idth: 180px;
margin: 0;
margin-left: 2px;
padding: 4px;

The false value should be 190px as there is padding of 4px set for all sides (8px total for left & right) as well as a margin-left of 2px, could this be it?

modo

1:17 pm on Feb 25, 2004 (gmt 0)

10+ Year Member



you know, i was wondering about that myself. in fact, i think you just pointed out something which might just fix my layout!

ciao
modo.