<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>my tech blog &#187; CSS</title>
	<atom:link href="http://billauer.se/blog/tag/css/feed/" rel="self" type="application/rss+xml" />
	<link>https://billauer.se/blog</link>
	<description>Anything I found worthy to write down.</description>
	<lastBuildDate>Thu, 12 Mar 2026 11:36:00 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.2</generator>
		<item>
		<title>One little button for Firefox: XUL is cool</title>
		<link>https://billauer.se/blog/2009/05/firefox-extension-toolbar-button-xul/</link>
		<comments>https://billauer.se/blog/2009/05/firefox-extension-toolbar-button-xul/#comments</comments>
		<pubDate>Sun, 17 May 2009 20:28:22 +0000</pubDate>
		<dc:creator>eli</dc:creator>
				<category><![CDATA[Internet]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[CSS]]></category>
		<category><![CDATA[firefox]]></category>
		<category><![CDATA[overlay]]></category>
		<category><![CDATA[xul]]></category>

		<guid isPermaLink="false">https://billauer.se/blog/?p=211</guid>
		<description><![CDATA[Introduction This post summarizes some of my findings while attempting to make a descent single-button Firefox extension. I&#8217;ve tested the code shown below with Firefox 1.5 and 3.0 under Windows, and Firefox 3.0 under Linux, and there were zero portability issues. I would suggest reading a tutorial about toolbars and possibly Mozilla&#8217;s own tutorial about [...]]]></description>
			<content:encoded><![CDATA[<h3>Introduction</h3>
<p>This post summarizes some of my findings while attempting to make a descent single-button Firefox extension. I&#8217;ve tested the code shown below with Firefox 1.5 and 3.0 under Windows, and Firefox 3.0 under Linux, and there were zero portability issues.</p>
<p>I would suggest reading a <a href="http://www.borngeek.com/firefox/toolbar-tutorial/" target="_blank">tutorial about toolbars</a> and possibly <a href="https://developer.mozilla.org/En/XUL_Tutorial" target="_blank">Mozilla&#8217;s own tutorial about XUL</a> before the notes below.</p>
<h3>The magic</h3>
<p>All I wanted was to put a little button on my Firefox browser. At some point I realized that I won&#8217;t get things working like I want unless I understand what I&#8217;m doing. So I dived into the thing called XUL.</p>
<p>I have to admit, that it looks terrible at first glance. I started to change my mind when I realized, that XUL is a format for describing GUI items. That my case of toolbar extensions is a very narrowminded one. To get an idea what XUL can do,  just have a look on the so-called <a href="http://www.hevanet.com/acorbin/xul/top.xul" target="_blank">periodic table</a>. But that&#8217;s really nothing compared with the simple fact, the all of Firefox&#8217;  GUI (menus, buttons, dialogs) is implemented in XUL and JavaScript.</p>
<p>But my the real enlightment came to me at page 391-392 of <a href="http://books.google.co.nz/books?id=ryEKOKnHFa8C&amp;pg=PA392&amp;lpg=PA392&amp;dq=xul+chrome+programming+firefox&amp;source=web&amp;ots=k3AKxyj8QL&amp;sig=5YDd_01-dCL8QVQnkAzD1epfek0&amp;hl=en#PPP1,M1" target="_blank">Programming Firefox</a>. That&#8217;s where I realized that I should unzip the browser.jar file, which is exactly where one could expect to find it: In the Mozilla Firefox/chrome directory (where the application is installed, not where it puts its cache data etc.)</p>
<p>Also, unzipping classic.jar is useful to see the names of the image files used, so that the relevant place can be found easily in browser.jar&#8217;s files. It&#8217;s much easier to find the place at which the reload button is defined when its image file&#8217;s name is known.</p>
<h3>Reverse engineering</h3>
<p>Let&#8217;s investigate the reload button, then. It wasn&#8217;t too difficult to realize that its icon can be found in the classic.jar, in a file named Toolbar.png (find the directory yourself). Searching for that string in all of classic.jar&#8217;s files, the next clue was  in browser.css (same directory as Toolbar.png).</p>
<p>Now, I won&#8217;t pretend to really understand what&#8217;s going on in that CSS file, and neither did I make a real effort to do so. All I needed was to come across a CSS definition like:</p>
<pre>#reload-button {
  -moz-image-region: rect(0px 96px 24px 72px);
}</pre>
<p>If you&#8217;re not so good in CSS, that&#8217;s a definition for an object with ID &#8216;reload-button&#8217;. I wonder what that object could be&#8230;</p>
<p>So now I had my next clue: Look for reload-button in browser.jar&#8217;s files. Surprise, surprise, it&#8217;s in browser.xul. The button is declared as</p>
<pre>      &lt;toolbarbutton id="reload-button" class="toolbarbutton-1 chromeclass-toolbar-additional"
                     label="&amp;reloadCmd.label;"
                     command="Browser:Reload"
                     tooltiptext="&amp;reloadButton.tooltip;"/&gt;</pre>
<p>Which, to tell the truth, is not so interesting. What I&#8217;m really looking for, is the ID of the enclosing XML tag. Scrolling up a bit, I found it:</p>
<pre>&lt;toolbarpalette id="BrowserToolbarPalette"&gt;</pre>
<p>This is all I could ask for. But what is a &#8220;toolbarpalette&#8221;, I asked myself. Google took me straight to <a href="https://developer.mozilla.org/En/XUL:toolbarpalette" target="_blank">Mozilla&#8217;s documentation</a>, which spelled it out for me.</p>
<p>The aftermath is, of course, that there was no need for reverse engineering, since the documentation tells me exactly how to get the button at the desired position. On the other hand, I didn&#8217;t find that information before.</p>
<p>I see no point in telling the whole story of going back and forth from searching the jar files for strings, then trying Google and/or trying to squeeze in an element of my own. The bottom line is that once I understood how the XUL represents the GUI itself, it became fairly easy to find the right magic words.</p>
<p>So I&#8217;ll go on with the bottom line of how to put the button in some interesting places.</p>
<h3>The skeleton</h3>
<p>I&#8217;ll skip the framework, since it&#8217;s documented all over. You can always unzip any Firefox extension XPI file to get an idea of what should be in it.</p>
<p>I assume that we already have a Javascript file, testingbutton.js. In that file, we have a function called testing_doit(), which we want executed every time our button is pushed.</p>
<p>Basically, the XUL file has the following format:</p>
<pre>&lt;?xml version="1.0"?&gt;
&lt;?xml-stylesheet href="chrome://testingbutton/skin/testingbutton.css" type="text/css"?&gt;

&lt;overlay id="Testing-Overlay"
  xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"&gt;

  &lt;script type="application/x-javascript"
    src="chrome://testingbutton/content/testingbutton.js" /&gt;

... One or more element definitions come here ...

&lt;/overlay&gt;</pre>
<p>The important magic word here is <a href="https://developer.mozilla.org/en/xul/overlay" target="_blank">&lt;overlay&gt;</a>. What it means is that we can add elements to existing GUI elements by using their ID.  For example (shown in detail below), if I want to add an element to the toolbarpalette with ID &#8220;BrowserToolbarPalette&#8221;, I simply repeat its opening tag, and put my things there. This will not override the existing definition, but rather append new elements.</p>
<p>Note that any number of snippets from the examples below can be inserted to add several controls to the browser window.</p>
<p>And just before starting, here&#8217;s the CSS that comes along: Basically, it defines the image to use on the buttons, which are pinpointed through their IDs (using classes may have been more elegant?)</p>
<pre>#Testing-Toolbar-Button {
    list-style-image: url("chrome://testingbutton/skin/logo.png");
}
#Testing-Doit-Button {
    list-style-image: url("chrome://testingbutton/skin/logo.png");
}
#Testing-Doit-Button2 {
    list-style-image: url("chrome://testingbutton/skin/logo.png");
}</pre>
<h3>In the toolbar (not)</h3>
<p>I&#8217;ll start with a spoiler: The code below will add the button to the <strong>Toolbar Palette</strong> and not to the toolbar itself. In other words, after installing the extension, nothing will happen until the user manually puts the button in place. Not very impressive. I suppose there is a way to get the button there automatically, but given the alternatives, I didn&#8217;t bother to check how to do that.</p>
<p>Here&#8217;s the code:</p>
<pre>  &lt;toolbarpalette id="BrowserToolbarPalette"&gt;
    &lt;toolbarbutton
      id="Testing-Doit-Button"
      label="Testing button"
      tooltiptext="Do it!"
      oncommand="testing_doit();"
    /&gt;
  &lt;/toolbarpalette&gt;</pre>
<p>The only really interesting thing here is that the &#8216;toolbarpalette&#8217; tag shares its ID with the one defined in browser.xul. That&#8217;s the point, after all.</p>
<h3>In a toolbar of its own (ugly)</h3>
<p>I&#8217;m showing this one, only because it&#8217;s popular in examples I&#8217;ve seen, and it turns out ugly for a single button. The code below creates a full toolbar, from left to right, and puts one little button in it. Looks pathetic. Makes sense if you have a lot of buttons (and users who love your application a lot).</p>
<pre>  &lt;toolbox id="navigator-toolbox"&gt;
    &lt;toolbaritem flex="0"&gt;
      &lt;toolbarbutton id="Testing-Toolbar-Button"
       tooltiptext="Do it!"
       oncommand="testing_doit();" /&gt;
    &lt;/toolbaritem&gt;
  &lt;/toolbox&gt;</pre>
<p>Note that &#8216;toolbarbutton&#8217; has an ID which is mentioned in the CSS file. This is how the graphical icon appears.</p>
<h3>In the existing toolbar (bingo!)</h3>
<p>Well, I&#8217;m not sure if this is exactly where you would expect it, but the code below puts the button at the rightmost position next to the toolbar the user drags bookmarks to. The purely logic place might have been next to the address bar, but the actual position is excellent. Code follows:</p>
<pre>  &lt;toolbar id="PersonalToolbar"&gt;
    &lt;toolbarbutton id="Testing-Doit-Button2"
     tooltiptext="Do it!"
     oncommand="testing_doit();" /&gt;
  &lt;/toolbar&gt;</pre>
<p>(By the way, I found the magic &#8216;PersonalToolbar&#8217; ID simply by looking at browser.xul and trying it out. The name sounded promising, after all)</p>
<h3>In the status bar (So cool!)</h3>
<p>At the bottom-right corner, with minimal distraction:</p>
<pre>  &lt;statusbar id="status-bar"&gt;
    &lt;statusbarpanel class="statusbarpanel-iconic"
     id="Testing-status-button"
     onclick="testing_doit();"
     tooltiptext="Do it!"
     src="chrome://testingbutton/skin/logo.png" /&gt;
  &lt;/statusbar&gt;</pre>
<h3>An entry in the context menu</h3>
<p>For users who don&#8217;t like to travel much with their mouse (laptops?), we have the menu which pops up when you right-click. How to add an entry to the context menu is described <a href="https://developer.mozilla.org/En/XUL/PopupGuide/Extensions" target="_blank">here</a>. Note that the executed script can get some info about what was right-clicked (it&#8217;s called a context menu, after all).</p>
<p>Anyhow, this is what I made of it:</p>
<pre>  &lt;popup id="contentAreaContextMenu"&gt;
    &lt;menuitem id="Testing-menu-entry"
     label="Do it!"
     oncommand="testing_doit();"/&gt;
  &lt;/popup&gt;</pre>
<h3>Conclusion</h3>
<p>This XUL framework takes some time to get used to, but it&#8217;s a fantastic demonstration of how an open-source project can expose its internals without needing to compile anything. Despite some difficulty, working with Firefox&#8217; internals is far more enjoyable than its well-known counterpart. Which shouldn&#8217;t surprise anyone.</p>
]]></content:encoded>
			<wfw:commentRss>https://billauer.se/blog/2009/05/firefox-extension-toolbar-button-xul/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>CSS and DIV layout: The main pitfalls</title>
		<link>https://billauer.se/blog/2009/04/css-div-layout-main-pitfalls-cascaded-style-sheets/</link>
		<comments>https://billauer.se/blog/2009/04/css-div-layout-main-pitfalls-cascaded-style-sheets/#comments</comments>
		<pubDate>Thu, 16 Apr 2009 14:49:00 +0000</pubDate>
		<dc:creator>eli</dc:creator>
				<category><![CDATA[Internet]]></category>
		<category><![CDATA[absolute]]></category>
		<category><![CDATA[CSS]]></category>
		<category><![CDATA[div]]></category>
		<category><![CDATA[floats]]></category>
		<category><![CDATA[overflow]]></category>
		<category><![CDATA[overlap]]></category>
		<category><![CDATA[pitfalls]]></category>
		<category><![CDATA[position]]></category>
		<category><![CDATA[relative]]></category>
		<category><![CDATA[rtfm]]></category>
		<category><![CDATA[table]]></category>
		<category><![CDATA[w3c]]></category>

		<guid isPermaLink="false">https://billauer.se/blog/?p=108</guid>
		<description><![CDATA[In the beginning web pages were simple and innocent. Then people started to abuse tables to achieve some layout. And then came CSS and DIV, the winning combination for making a web page oh-so-beautiful but not resizable. Web designers started telling the browser where to put what, and the browser responded with blind obedience, even [...]]]></description>
			<content:encoded><![CDATA[<p>In the beginning web pages were simple and innocent. Then people started to abuse tables to achieve some layout. And then came CSS and DIV, the winning combination for making a web page oh-so-beautiful but not resizable. Web designers started telling the browser where to put what, and the browser responded with blind obedience, even if the result was a rubble of text and graphics. That&#8217;s the deal: Use absolute positioning to get full control, but lose the browser&#8217;s capability of avoiding nasty accidents.</p>
<p>So here is my short list of the main problems you may run into, while attempting to layout a web page using CSS-backed DIVs. If you really have to, that is.</p>
<h2>Pitfall #0: Resize</h2>
<p>If your page is made with CSS, don&#8217;t let the width change. Period. Play with CSS/DIV layout only when you&#8217;re inside a box, whose width is known and fixed to the pixel. This is not very browser tolerant, I know. It may not work well on a tiny screen. Or a very big one.</p>
<p>But trust me on this: If you don&#8217;t set the overall width, you will sooner or later find yourself with spillovers and overlapping graphics. Maybe you can get it to survive a page resize on your browser. You have no chance with the IE version still not out.</p>
<p>If you want to allow a resize, use tables.</p>
<h2>Pitfall #1: Huge CSS file (reference by ID)</h2>
<p>This one is so common, that sometimes I wonder if I missed something.</p>
<p>Anyhow, the original idea of CSS, if I got it right, was to define the format in one place and put the content in another. I suppose someone hoped that people would go on using simple tags such as &lt;h1&gt; and &lt;p&gt; and get beautiful pages with clean HTML.</p>
<p>And to make things nicer, they allowed classes. So if you wanted a special paragraph, you just went &#8216;p.special&#8217; in the style definition, and then &lt;p class=&#8221;special&#8221;&gt; in the HTML, and there you were, the HTML still readable and the format consistent and depending on the content.</p>
<p>But then came style-by-ID, which turned everything into a joke. This made it possible to define the style for a very certain element, pin-pointing it by its ID tag. So if I wanted to format one single paragraph, I&#8217;d go &#8216;#theID&#8217; in the CSS file, and then &lt;p id=&#8221;theID&#8221;&gt; in the HTML. And that makes me wonder: If the styling is going to be used once anyhow, why not put it directly in the HTML? Why not go &lt;p style=&#8221;&#8230;&#8221;&gt; instead? Don&#8217;t tell me that you separate formatting from content, when you can&#8217;t change anything in the CSS file without checking the specific place where it&#8217;s used. If you remember where it was, that is.</p>
<h2>Pitfall #2: The IE &#8216;width&#8217; bug</h2>
<p>The <a href="http://www.w3.org/TR/2008/REC-CSS2-20080411/" target="_blank">CSS2 spec</a> paragraph 10.2 clearly defines the &#8216;width&#8217; property as &#8220;that of the rendered content within&#8221;. Microsoft probably said &#8220;we are the standard&#8221; and decided to include the padding and the border in the &#8216;width&#8217; property. This is said to be fixed in IE6, so maybe it isn&#8217;t an issue today, after all.</p>
<p>Below is drawing of the box model, and the difference between the two definitions of &#8216;width&#8217;. The sad bottom line is that if you need a consistent width of some element, padding and border have to be zero. If this is not an option, the only safe solution is to put your element in another element (a DIV, I suppose) and define the &#8216;width&#8217; for the enclosing element.</p>
<div id="attachment_127" class="wp-caption alignnone" style="width: 490px"><img class="size-full wp-image-127" title="boxmodel" src="https://billauer.se/blog/wp-content/uploads/2009/04/boxmodel.png" alt="Box model" width="480" height="360" /><p class="wp-caption-text">Box model</p></div>
<h2>Pitfall #3: Getting absolute positioning wrong</h2>
<p>So let&#8217;s go through a few quick facts:</p>
<ul>
<li>Absolute positioning puts your element where it would anyhow, unless you specify otherwise for the X axis (with &#8220;left&#8221; and &#8220;right&#8221; style properties) and/or the Y axis (with &#8220;top&#8221; or  &#8220;bottom&#8221; style properties). The only difference is that no space is allocated on the rendered page. The responsibility to keep the underlying area clean (or intentionally overlapping) is all yours.  Usually, if you don&#8217;t know exactly what you&#8217;re doing, things will get wrong sooner or later.</li>
<li>If you want the element on a fixed position on the screen, no matter how the page is scrolled (logo?), used the &#8220;fixed&#8221; position.</li>
<li>&#8220;relative&#8221; position is not what it appears to be. It will move the block from its original position, but allocate  space for it, as if it was there. Makes sense if you want chemistry-style subscripting, but otherwise I can see only one use: The zero-offset relative positioning, which is a way to set the &#8216;position&#8217; property to something else than static (the default) without actually doing anything, and hence create a &#8220;containing block&#8221; in its natural place (more about that next).</li>
<li>The &#8220;left&#8221;, &#8220;right&#8221;, &#8220;top&#8221; and &#8220;bottom&#8221; properties are related to the &#8220;containing block&#8221;, whose definition is a pitfall in itself. (See the <a href="http://www.w3.org/TR/2008/REC-CSS2-20080411/" target="_blank">CSS2 spec</a>, paragraph 10.1, item 4). To make a long story short, the browser will climb up the hierarchy of blocks until it finds one, which isn&#8217;t statically positioned. It will then respect that block&#8217;s padding, and there is the zero point.<br />
Now, &#8220;statically positioned&#8221; is the default setting of the &#8220;position&#8221; property, which means that the browser should put the element following the &#8220;old-fashioned&#8221; rules. See the example below for how to use the &#8220;relative&#8221; position to make the enclosing DIV effective.<br />
If the containing block isn&#8217;t explicitly defined, hell breaks loose. The browser will most likely pick the root block, which is the top left corner of the page. Or not, depending on other junk you have on your page. Everything happens.</li>
</ul>
<h2>Pitfall #4: Absolute positioning know-it-all</h2>
<p>I think that the main reason why &#8220;professional&#8221; web designers started talking about replacing &lt;TABLE&gt; formatting with &lt;DIV&gt; and CSS was that they first painted their web page with some graphics tool, and then wanted to copy their graphic design exactly into the web page. Which is a nice concept, if you can assure that the page will be viewed exactly like you expect: The same fonts, the same sizes, the same screen size, and so on. Which you can&#8217;t, but that&#8217;s another story.</p>
<p>No wonder they hated tables. It&#8217;s really a headache to control how the browser displays tables down to the pixel. Tables were, after all, not meant to be used that way. Getting the graphics from one table cell aligned with the next one is not easy. Not to mention more complex relations between graphic elements. And then came CSS and the DIVs.</p>
<p>The real power with DIVs is absolute positioning. Just make yourself a box, decide how large it will be (down to the pixel) and put whatever you want inside, exactly where you want it. Something like this:</p>
<pre>&lt;div style="position: relative; height: 200px; width: 200px; border: dotted red 1px;"&gt;
This is my canvas!
&lt;div style="position: absolute; top: 60px; left: 20px"&gt;And&lt;/div&gt;
&lt;div style="position: absolute; top: 50px; left: 70px"&gt;I&lt;/div&gt;
&lt;div style="position: absolute; top: 55px; left: 90px"&gt;can&lt;/div&gt;
&lt;div style="position: absolute; top: 60px; left: 130px"&gt;put&lt;/div&gt;
&lt;div style="position: absolute; top: 110px; left: 40px"&gt;the&lt;/div&gt;
&lt;div style="position: absolute; top: 105px; left: 70px"&gt;elements&lt;/div&gt;
&lt;div style="position: absolute; top: 112px; left: 140px"&gt;where&lt;/div&gt;
&lt;div style="position: absolute; top: 150px; left: 80px"&gt;I&lt;/div&gt;
&lt;div style="position: absolute; top: 155px; left: 90px"&gt;want!&lt;/div&gt;
&lt;/div&gt;</pre>
<p>And you get this:</p>
<div style="border: 1px dotted red; position: relative; height: 200px; width: 200px;">This is my canvas!&nbsp;</p>
<div style="position: absolute; top: 60px; left: 20px;">And</div>
<div style="position: absolute; top: 50px; left: 70px;">I</div>
<div style="position: absolute; top: 55px; left: 90px;">can</div>
<div style="position: absolute; top: 60px; left: 130px;">put</div>
<div style="position: absolute; top: 110px; left: 40px;">the</div>
<div style="position: absolute; top: 105px; left: 70px;">elements</div>
<div style="position: absolute; top: 112px; left: 140px;">where</div>
<div style="position: absolute; top: 150px; left: 80px;">I</div>
<div style="position: absolute; top: 155px; left: 90px;">want!</div>
</div>
<p>Now I&#8217;m asking, isn&#8217;t this heaven? I say: It is, on your browser, on your computer. But since you&#8217;ve told the browser exactly what to do, you&#8217;ve also crippled its ability to fix things when the page is shown differently from how you expect it. A simple example follows.</p>
<h2>Pitfall #5: Overflow</h2>
<p>The page got resized? The fonts are shown larger than you defined? There was more text than expected? In the old days, the browser used to fix this. With CSS and DIVs you get rubbish.</p>
<p>The CSS spec says, that any block can overflow. What to do with the excessive graphic pulp is up to the browser (or it can be defined) but no matter how it&#8217;s handled, the result is ugly.</p>
<p>Let&#8217;s take a silly menu layout for example:</p>
<pre>&lt;div style="position: relative; width: 120px; height: 70px; border: solid 1px black"&gt;
&lt;div style="position: absolute; top: 10px; left: 20px; background-color: #f8f;"&gt;Take me!&lt;/div&gt;
&lt;div style="position: absolute; top: 40px; left: 20px; background-color: #ff8;"&gt;Or take me!&lt;/div&gt;
&lt;/div&gt;</pre>
<p>That looks like this:</p>
<div style="border: 1px solid black; position: relative; width: 120px; height: 70px;">
<div style="position: absolute; top: 10px; left: 20px; background-color: #ff88ff; font-size: 14px;">Take me!</div>
<div style="position: absolute; top: 40px; left: 20px; background-color: #ffff88; font-size: 14px;">Or take me!</div>
</div>
<p>The truth is, I can&#8217;t be sure that you see this as I do. But I&#8217;ll assume that it looks idiotic, but OK. But what happens if a script puts more text than expected? Or if the user forces a larger font? I&#8217;ll try to simulate this by choosing a larger font here:</p>
<div style="border: 1px solid black; position: relative; width: 120px; height: 70px;">
<div style="position: absolute; top: 10px; left: 20px; background-color: #ff88ff; font-size: 30px;">Take me!</div>
<div style="position: absolute; top: 40px; left: 20px; background-color: #ffff88; font-size: 30px;">Or take me!</div>
</div>
<p>But the truth is that I can&#8217;t know if this example went through OK, because I don&#8217;t know how your browser responded. But that&#8217;s the point, isn&#8217;t it?</p>
<p>If you want to be on the safe side, use the &#8216;overflow&#8217; property. Below is shown what happens when you set &#8216;overflow: hidden;&#8217; (to the left) or &#8216;overflow: scroll;&#8217; (to the right) for the toplevel &lt;div&gt;. At least nothing spills over.</p>
<div style="position: relative; width: 260px; height: 100px;">
<div style="border: 1px solid black; overflow: hidden; position: absolute; width: 120px; height: 70px; top: 0px; left: 0px;">
<div style="position: absolute; top: 10px; left: 20px; background-color: #ff88ff; font-size: 30px;">Take me!</div>
<div style="position: absolute; top: 40px; left: 20px; background-color: #ffff88; font-size: 30px;">Or take me!</div>
</div>
<div style="border: 1px solid black; overflow: scroll; position: absolute; width: 120px; height: 70px; top: 0px; left: 140px;">
<div style="position: absolute; top: 10px; left: 20px; background-color: #ff88ff; font-size: 30px;">Take me!</div>
<div style="position: absolute; top: 40px; left: 20px; background-color: #ffff88; font-size: 30px;">Or take me!</div>
</div>
</div>
<h2>Pitfall #6: Floats</h2>
<p>For some reason, which is beyond me, floats are commonly suggested as a solution for organizing web pages (except for horizontal layout, as shown below). There is only one problem with floats: They were intended to push text, and nothing else.</p>
<div style="border: 1px solid black; margin: 6px; padding: 5px; float: left; width: 4em; height: 4em; background-color: #ffdddd; text-align: center;">Nice image</div>
<p>The whole idea was to allow an image to float to the left or right, and let the text surround it, just like as they do in newspapers. This is why the <a href="http://www.w3.org/TR/2008/REC-CSS2-20080411/" target="_blank">CSS2 spec </a>says, in paragraph 9.4.1:</p>
<blockquote><p>In a block formatting context, boxes are laid out one after the other, vertically, beginning at the top of a containing block. (&#8230;) In a block formatting context, each box’s left outer edge touches the left edge of the containing block (for right-to-left formatting, right edges touch). <strong>This is true even in the presence of floats</strong> (although a box’s content area may shrink due to the floats).</p></blockquote>
<p>Let me spell this out: Paragraphs, DIVs or whatever, <strong>shouldn&#8217;t</strong> get pushed aside by floats. What should get pushed is <strong>the text they contain</strong> (or more precisely, inline elements). What happens in reality seems to depend on the browser. Firefox and Google Chrome follow the spec, IE plays its own game.</p>
<p>So let&#8217;s look at the following example:</p>
<pre>&lt;div style="border: 1px solid black; margin: 6px; padding: 5px; float: left; width: 4em; height: 4em; background-color: #ffdddd; text-align: center;"&gt;
Nice image&lt;/div&gt;
&lt;div style="background-color: #ff8; width: 12em; border: solid 1px black;"&gt;
This is my DIV, which has melted into the float.
Only the text is pushed aside.
&lt;/div&gt;</pre>
<p>Which looks like this on <strong>your browser</strong>:</p>
<div style="border: 1px solid black; margin: 6px; padding: 5px; float: left; width: 4em; height: 4em; background-color: #ffdddd; text-align: center;">Nice image</div>
<div style="border: 1px solid black; background-color: #ffff88; width: 12em;">This is my DIV, which has melted into the float. Only the text is pushed aside.</div>
<p>Note that the non-floating DIV (in yellow) should surround the float. It should be packed as if the float wasn&#8217;t there. This is what the spec says should happen. In real life I got:</p>
<div id="attachment_144" class="wp-caption alignnone" style="width: 450px"><img class="size-full wp-image-144" title="Float behavior on Firefox and IE" src="https://billauer.se/blog/wp-content/uploads/2009/04/floats-pitfall.png" alt="Float behavior on Firefox and IE" width="440" height="192" /><p class="wp-caption-text">Float behavior on Firefox and IE</p></div>
<p>Not very impressive, is it?</p>
<p>By the way, if you want a rectangle which doesn&#8217;t mix with floats, put a table there. Tables are inline elements, so they won&#8217;t collide with floats. But if you&#8217;re playing with floats, you must be thinking that tables are obsolete&#8230;</p>
<p>Alternatively, you could go for a floats-only region. People who test with IE only are likely to get this wrong, because of the problem mentioned above. Anyhow, the idea is to pack DIVs horizontally to the right and to the left using floats, and avoid collision from top and bottom using the &#8220;clear&#8221; directive. For example:</p>
<pre>&lt;div style="background-color: #ffd; border: solid 1px black; text-align: center;"&gt;Above all&lt;/div&gt;
&lt;div style="float: left; background-color: #fdd; width: 4em; height: 3em; border: solid 1px black; text-align: center;"&gt;
One&lt;/div&gt;
&lt;div style="float: left; background-color: #fdd; width: 4em; height: 2em; border: solid 1px black; text-align: center;"&gt;
Two&lt;/div&gt;
&lt;div style="float: right; background-color: #fdd; width: 4em; height: 4em; border: solid 1px black; text-align: center;"&gt;
Four&lt;/div&gt;
&lt;div style="float: right; background-color: #fdd; width: 4em; height: 3em; border: solid 1px black; text-align: center;"&gt;
Three&lt;/div&gt;
&lt;div style="clear: both; background-color: #ffd; border: solid 1px black; text-align: center;"&gt;And this is below all!&lt;/div&gt;</pre>
<p>Which looks like this:</p>
<div style="background-color: #ffd; border: solid 1px black; text-align: center;">Above all</div>
<div style="border: 1px solid black; float: left; background-color: #ffdddd; width: 4em; height: 3em; text-align: center;">One</div>
<div style="border: 1px solid black; float: left; background-color: #ffdddd; width: 4em; height: 2em; text-align: center;">Two</div>
<div style="border: 1px solid black; float: right; background-color: #ffdddd; width: 4em; height: 4em; text-align: center;">Four</div>
<div style="border: 1px solid black; float: right; background-color: #ffdddd; width: 4em; height: 3em; text-align: center;">Three</div>
<div style="clear: both; background-color: #ffd; border: solid 1px black; text-align: center; margin-bottom: 2em;">And this is below all!</div>
<p>This is great for multiple columns, whose heights are unknown. We want the bottom DIV below all of them, and this is how it&#8217;s done. Note that if we put a non-floating DIV in the middle, we would get different behavior on IE and W3C-compliant browsers.</p>
<p>Note that none of the DIVs in this example contains the other. What makes them avoid each other is the &#8220;clear: both&#8221; attribute at the bottom which tells the browser that floats are not allowed in either side. So we get a clean line, immediately below.</p>
<p>One problem (or feature) of this method, is that if there is not enough horizontal space, floats will be pushed vertically, a bit like inline flow. Paragraph 9.5 of the <a href="http://www.w3.org/TR/2008/REC-CSS2-20080411/" target="_blank">W3C specification</a> says:</p>
<blockquote><p>If there isn’t enough horizontal room on the current line for the float, it is shifted downward, line by line, until a line has room for it.</p></blockquote>
<p>This can&#8217;t be relied on too much, though. It looks like Firefox prefers to let some of the page go out of view rather than to mess the page up, when the page is resized to very narrow (below 400 pixels?). I don&#8217;t know exactly how it works, but the fact is that at some point the floats don&#8217;t tile vertically (in IE they do). I guess it&#8217;s an interpretation of &#8220;not enough horizontal space&#8221;.</p>
<p>And just a final remark about floats: The <a href="http://www.w3.org/TR/2008/REC-CSS2-20080411/" target="_blank">W3C specification</a> requires that the width is known, and must therefore be set unless it has an intrinsic value (e.g. an image). But nothing stops us from setting the width in percents. With this simple trick, we can have columns that grow and shrink according to the limiting DIVs width (resizing&#8230;), while controlling the proportions between the columns.</p>
<p>Just a warning, though: Be careful with letting the overall width come near 100%, since the browser may round each float&#8217;s pixel width slightly. And of course, keep in mind that the width doesn&#8217;t include the border, margins nor padding, so these can cause a mess when things get tight. Not to mention mixing percentage width floats with constant width floats.</p>
<h2>Pitfall #7: Getting the selection wrong</h2>
<p>The grammar is in principle simple, but knowing the formalities is important. For example, div.theclass { } means a DIV with class &#8220;theclass&#8221;, but not necessarily a DIV within it. For a DIV within a certain class, the selection is e.g. &#8220;.theclass DIV { }&#8221;. This also goes for IDs, e.g. &#8220;#theID DIV { }&#8221;. The list can be nested, so a link within a list item within a certain ID could look like &#8220;#theID li a:visited { }&#8221;. The same trick goes with classes instead of IDs.</p>
<p>If several cases are desired, start the nesting from the beginning. Exactly like &#8220;a.banner:link, a.banner:visited, a.banner:active { }&#8221; one should also go &#8220;banner a:link, banner a:visited, banner a:active { }&#8221; when a &#8220;within&#8221; relation is desired rather than a direct one.</p>
<h2>Conclusion</h2>
<p>If you want to control your page&#8217;s layout down to the pixel, DIVs and CSS are your screws and screwdriver. Just remember that things may look very different when someone views your page with a cellular phone or some other gadget you wouldn&#8217;t think about.</p>
<p>As they always say, when everything else fails, RTFM (Read the Fine Manual). In the case of CSS, that manual is the <a href="http://www.w3.org/TR/2008/REC-CSS2-20080411/" target="_blank">W3C specification</a>. This is not to say that browsers really follow it. Firefox does most of the time. At least they consider it a bug when it doesn&#8217;t. Microsoft and their IE live in a world of their own.</p>
<p>Anyhow, if you wonder why your &lt;div&gt;&lt;div&gt;&lt;div&gt;-page is a mess, or why everything goes wrong with patches of page fragments being where they shouldn&#8217;t be, the ultimate answer is to understand how the fine machine works. Or at least, how it was meant to. And that is written in the spec. Boring, but a man has to do what a man has to do.</p>
]]></content:encoded>
			<wfw:commentRss>https://billauer.se/blog/2009/04/css-div-layout-main-pitfalls-cascaded-style-sheets/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
	</channel>
</rss>
