CSS: The All-Expandable Box 152
In HTML, if you don't specify a specific width, block-level elements are vertically expandable by their nature. Think of an unordered list. That list will grow be be as big as it needs to be to fit all of it's list elements. If a user increases the font size in their browser, the list will expand vertically, growing to fit the larger content. Sometimes it feels like vertical-only expansion is limiting and it would be nice if the element could grow horizontally as well as vertically with a font size increase by the user.
Abstract
If you have been using the Firefox 3 beta much, you might notice that it handles this automatically. Increasing the size in Firefox 3 doesn't just increase the font size, it increases everything in size, which actually feel really natural and nice. But despite it's growing market share, we can't count on Firefox for the resizing needs of our users.
I am going to attempt to explain how to make an All-Expandable box, with the following features:
- Works in all major browsers
- Expands both vertically and horizontally
- Uses a single background image

This is a bit of a tall order, especially the use of the background image. This will end up using kind of a combination of the CSS sprites technique since different areas of the image will be used in different places and the Sliding Doors technique, since different amounts of those images will be visible depending on the current size.
Make the box horizontally expandable
There is one way simple way to make a box horizontally expandable: specify your width in em's. For example:
.box {
width: 35em;
margin: 50px auto;
}
The margin is there for example purposes, to keep it centered and away from the top edge of the browser window.
Thinking about image placement
In this example, the box has rounded corners, a bit of a drop shadow, and a bit of an inner shadow. This means that all of the four corners of the box are distinctly different. This is uniquely challenging since images are not expandable. We will need a way to apply the four different corner images to the four corners of the box separately.
Also, we will need to overlap them in such a way that the transitions are seamless. And also, we are trying to do this with only a single background image, to make it as efficient as possible.
Below is an image of how you might think of what we need to do. The boxes would be overlapping, I nudged them apart so you can see the whole boxes.

When creating the background image, think big. The bigger your background image, the larger you will be able to expand without borking the layout. The example background is 700px wide which gets you about 4 or 5 different text sizings it works at, but it does eventually break apart above that.
Coding the box
Of course we always like to be as semantic as possible with our XTHML. That means not using extra markup for things that aren't really content but are purely design. Unfortunately, with all this craziness of needing four boxes for our single box, it ain't gonna happen.
This is how it's done:
<div class="box">
<div class="topleft">
<div class="topright">
<div>
CONTENT GOES HERE
</div>
</div>
</div>
<div class="bottomleft">
<div class="bottomright">
</div>
</div>
</div>
Styling the box
Here is the CSS for the four areas within the box:
.box div.topleft {
display: block;
background: url("images/box-bg.png") top left no-repeat white;
padding: 2.0em 0em 0em 2.0em;
}
.box div.topright {
display: block;
background: url("images/box-bg.png") top right no-repeat white;
padding: 2.0em;
margin: -2.0em 0 0 2.0em;
}
.box div.bottomleft {
display: block;
height: 45px;
margin-top: -2.0em;
background: url("images/box-bg.png") bottom left no-repeat white;
}
.box div.bottomright {
display: block;
background: url("images/box-bg.png") bottom right no-repeat white;
height: 45px;
margin-left: 3.0em;
}
Note the negative margins are necessary to pull back from the padding applied from the parent spans. It just works out good that way with the padding, keeping text inside the box. Also note the height of the bottom spans are set in pixels. That is on purpose as they need to be kept short and not be expandable.
This has been tested in Firefox, Safari, Opera, and IE 6 and is working in all of them, so I'm fairly satisfed it's a solid technique.
Credits
This tutorial is contributed by Chris Coyier. Visit CSS-Tricks to learn more CSS tricks from Chris.
Update:
The code in this example was updated to fix the div within a span issue and now validates.
Nothing new, but good nonetheless
yeah, rounded corners, good illustrated!
the last three days i fight with the IE6 PNG Problems… :) I’ll habby that works now on my new page… puh…
This is nice though, I gonna apply this on my XHTML and CSS website projects.
Thanks for this tutorial Nick! Keep up the good work :)
Good techinque!
But it’s not good to put block “div” into inline “span”, it’s not valid. Perhaps, will be better to use “div”, if we want to use block elements inside “topright”.
Bad example, you can’t put a block level element (a
div) inside of an inline level element – aspan. Also it is not good having two completely empty non-semantic elements. Check out this article I wrote some time ago (it’s in Serbian, but the example is what you’ll be interested in), I am using a definition list to mark up a comment (just as an illustration) that is as semantic as possible and uses as little markup as possible.“But it’s not good to put block “div” into inline “span”, it’s not valid.” +1 but I prefer sliding doors !
Good example although I’m not a fan of using none semantic markup I can understand that achieving this any other way is near impossible without CSS3 or other technologies.
OK – up to a point. If the text size gets to large then the whole thing ends up looking like a dogs breakfast. Tested on Windows in Firefox, IE7
Firefox 3 is not the only one to scale everything when increasing the text size, ie7 and opera 9 do the same. I think the zoom thing must be achieved by browsers, this code is not semantic at all. Interesting experiment anyway.
Very nice, but it breaks in Safari 3 at a certain point. (keep enlarging the text..)
interesting. thank you.
I have to try this one.. this is fantastic. great tutorial out there. I love the site also. Thanks for sharing this with us.
goog enjoy!`iam chinese.^_^
Hi, that’s very useful and interesting! Thanks a lot for this tutorial.
Yeah I kind of agree with the rest of the crowd about the placing of a block level element into an inline level element. That isn’t valid HTML/XHTML. Interesting idea though.
why bother with the spans? with that content, you’ve got four hooks, which is all you need for an expandy box with unique corners
I agree with the comments about the valid code and empty spans. I’ve utilized similar techniques using all divs but found the excessive code to be messy.
The best solution is to write javascript to add in the extra divs, keeping the html code simple and just living with the fact that if js is off then it won’t be as cool…
It does not work when zooming too large or too small in FF, or when using a large image in the content.
nice to see another tutorial with a different slant on an old problem…
The example page broke at three expansions, using Firefox 2 on XP.
Screenshot sent on request.