Simple Rounded Corner CSS Boxes
Part One: Historical Info, then the Goods
In November 2003, I invented an interesting CSS technique for semantic, rounded corner boxes using CSS. What that translates into non-geek-speak is that you get pretty boxes with no extra bulky XHTML markup. David Shea liked it and blogged it. Jeffrey Zeldman refused to publish an article because I had asked for some feedback from the folks on the CSS-Discuss list (from whence Dave got the blog reference) ... he considered it "already published". This was just as they were working on their earliest rounded corner box articles and before they were published. As I recall, even Eric Meyers commented on CSS-D that he liked it and might even use the technique ... the highest praise possible from an aspiring CSSer!
There were challenges though, as became quickly evident from the (even to this day) weekly inquiries from around the planet
- It requires two images to come up with the appearance of one ... not instantly intuititive. Why not one for each corner, for example?
- It used some pretty tricky CSS chock full of padding, negative margins and IE hacks-a-plenty. Even I have a tough time following and have to futz to get right when I deviate from the "recipe" ... probably not what you should call a "good sign".
- My original demo images were only really meant for no more than a few hundred pixels wide. So I got lots of questions about huge gaps in the boxes when used full screen on an 2484x1652 resolution page (or maybe it was just 1024x768...).
- Worse, I hadn't done much Photoshop teaching since doing the technical edit for Random House on The Official Guide for Adobe Photoshop 2.5 and visiting Kai Krause at the HSC offices in Santa Monica. Yes, version 2.5 ... and yes the guy of KPT filter fame.
- Did I mention a lot of folks asked me how to make images?
(Speaking of HSC and Kai, anyone ever know what happened to Sydney Stein and the rest of the Live Picture product team? Shawn Steiner's at Autodesk (via Discreet's acquisition) now and I still keep up with him via Linked-In; Ben Weiss is still doing amazing stuff in the graphics world. And Kai Krause opened Byteburg, an R&D lab in a freaking castle in Germany!)
But I digress ... I devised the hypothesis that if I could do it with only one really big sample image and a max of five CSS rules for the basic structure, then I would be able to stop sending folks email along the lines of, "I'd really like to help out but I don't really do Photoshop tutorials and Google can probably help you figure it out... and yes I'm aware there's a massive gap on your monitor that's large enough to show two HD-res movies side-by-side".
Then I promptly did the one-image version of it (in early 2004, mind you), never finished documenting it, continued to get emails from folks asking when, and eventually started working on a little CMS project that's kept me pretty busy since then. This morning, I finally managed to get down to my five-rule goal! (Actually, I made the part up about the five-rule goal ... but it's pretty cool that much got cut out nonetheless! Jason Fried is right: sometime "less is more".)
Notwithstanding the meandering trivia above, today I finally present the long-promised update to the original "ThrashBox" technique, with a more semantic name: Simple Rounded Corner CSS Boxes. Give "credit" (ahem) to "Big John" Gallant for the original name, as he insisted it be "ThrashBox". Since he is the man when it comes to figuring out sticky CSS situations, and helped work around some tricky IE shenanigans in the original, I reluctanly acquiesced. I promise to be stronger next time! :p
For those of you that haven't seen it, you can make boxes that look like the following (or better/worse) with this technique. The neat thing is that they expand and shrink automatically to fit your content and the XHTML markup is really light:
So let's just start right in... Imagine, if you will, the following markup:
<div class="cssbox">
<div class="cssbox_head"><h2>This is a header</h2></div>
<div class="cssbox_body">
<p>This is for your content.</p>
</div>
</div>
Now, for the CSS ... much more svelt and the bare minimum of hacks in hopes it pleases the anathema that is the future IE7:
/* set the image to use and establish the lower-right position */
.cssbox, .cssbox_body, .cssbox_head, .cssbox_head h2 {
background: transparent url(YourImageHere.png) no-repeat bottom right;
}
.cssbox {
width: 335px !important; /* intended total box width - padding-right(next) */
width: 320px; /* IE Win = width - padding */
padding-right: 15px; /* the gap on the right edge of the image (not content padding) */
margin: 20px auto; /* use to position the box */
}
/* set the top-right image */
.cssbox_head {
background-position: top right; margin-right: -15px; /* pull the right image over on top of border */
padding-right: 40px; /* right-image-gap + right-inside padding */
}
/* set the top-left image */
.cssbox_head h2 {
background-position: top left;
margin: 0; /* reset main site styles*/
border: 0; /* ditto */
padding: 25px 0 15px 40px; /* padding-left = image gap + interior padding ... no padding-right */
height: auto !important; height: 1%; /* IE Holly Hack */
}
/* set the lower-left corner image */
.cssbox_body {
background-position: bottom left;
margin-right: 25px; /* interior-padding right */
padding: 15px 0 15px 40px; /* mirror .cssbox_head right/left */
}
Now, for a live example, using the relevant CSS above.
- You have a large box with a drop shadow you want to use as your maximum sized box (800px by 800px @ a whopping 5Kb for a 24-bit .png). Make sure this image is taller and wider than the largest box you anticipate you'll need.
- The side margin is 15px on the outside of the file up to the "real" box edge (not including the drop shadow).
- You want 25px of interior padding on the box.
- You want the edge-to-edge box dimensions (omiting the outside padding) to be 320px.
These three critical measurements are indicated by the blue (max image), red (box) and green (inside content live area) bars below above the actual box:
Simple CSS Rounded Corners Rock
This is a short sample paragraph.
And another one, only it's not as short, as you can see. We'll see how much you really like this when it's all said and done.
And if you're reading this far, don't you think you have much better things to do than reading inane commentary? Like feeding your pet Beta. Speaking of Betas, I had one once and failed to come home when pulling an all-nighter at the office. Arriving home to discover a dried, purple fish, no longer flopping on the floor. True story...
Show me a smaller box and hide the drivel...
So there you have it! Part 2 will cover the math behind the madness, come with a fancy 3D-box model type Illustration made popular by über-designer Mr. Jon Hicks himself, and might even offer a few other examples. Part 3 will attempt a fluid-version of the box, with an even bigger image! And Part 4 will sum it all up, with bonus material of a discussion of some cool things that MODx lets you do with CSS like making dynamic CSS and registering CSS to the head of a page (used on this demo itself in fact). Somewhere along the line, I'll include sample .PSD files to get you started making box images. And based on historical precedent, you can expect the series to be complete no later than mid-2008.
In the meantime, I look forward to your feedback and questions, below! (logged in forum account required)
Comments:
Thanks!
@kmurray24: what do you mean without the header part? the header and H2 are needed for the top left/right images... if only IE supported generated content...
Thanks.
@Simply: all you need to do is change the selector from .cssbox_head h2 to .cssbox_head h1 and it should work perfectly.
@singpolyma: just follow the notes about the dimensions in the comments and it should work just fine.
@Bryn: I'm working on that issue exactly for Part 3 or Part 4. I'm almost there, too, although the positioning/dimensions are not nearly as straightforward.
Not sure if my test IE computer has some weird setup but it does not display correctly for me. It is IE6 on XP service pack 2. This is what the box on this page looks like http://www.wireless18.com/uploads/css/part2.jpg
If anyone knows of a particular problem with IE6 SP2 that is causing this I would be mighty grateful
cheers
Jonathan
http://www.maxdesign.com.au/presentation/pullquote/
@wireless18: not seeing the problems with my SP2 IE6 install. Do you by chance have some extensions installed that change it's default behavior? I've heard that can cause issue sometimes.
That is all.
We all love teh ThrashBox.
Also, with Big John's assistance (he is too freaking talented at prescribing fixes for the worlds IE-odditied and CSS maladies, did I mention that?) we finally nailed the technique for px widths using 24-bit PNGs with alpha transparency... now to work out the IE hacks for the bg images not scaling or squishing, if it's possible...
Part 2 should be cool, I hope!
http://www.ci.longmont.co.us/indexbox1.htm
Thanks
And the name mentions worked as I had suspected they would, so it wasn't just pointless name dropping after all folks. Now helping someone track down one of the foks mentioned in the article! ;)
http://img.photobucket.com/albums/0803/stuckinafridge/brokedScreen.png
my css is identical to yours with the exception the name for the original box.
I tried to do this with a sweet Table... it does not work in IE6. Have you any ideas?
http://test.my-taxi.com/#c_akt-fahrten
@Simply: have a link to share?
@heini: which part are you having trouble with? sweet Table?
One thing which is keeping me busy though, is that i can't figure out how you managed to get that 5Kb png image.
I played for hours with The Gimp & pngcrush, but it always stays around 100Kb .. that's faaaaaaar from the 5Kb you got :(.
Can you please tell us (or point us to) the magic behind the whopping 5Kb for a 24-bit .png?
One thing which is keeping me busy though, is that i can't figure out how you managed to get that 5Kb png image.
I played for hours with The Gimp & pngcrush, but it always stays around 100Kb .. that's faaaaaaar from the 5Kb you got :(.
Can you please tell us (or point us to) the magic behind the whopping 5Kb for a 24-bit .png?
One thing which is keeping me busy though, is that i can't figure out how you managed to get that 5Kb png image.
I played for hours with The Gimp & pngcrush, but it always stays around 100Kb .. that's faaaaaaar from the 5Kb you got :(.
Can you please tell us (or point us to) the magic behind the whopping 5Kb for a 24-bit .png?
Still there is something i'd like to ask you. I'm trying to create a website with a container div with an all grey rounded box, and put some other (container) divs in there, also with rounded boxes as background images. Sort of nesting rounded boxes. Is that possible? Thanx, Dzjien.
Still there is something i'd like to ask you. I'm trying to create a website with a container div with an all grey rounded box, and put some other (container) divs in there, also with rounded boxes as background images. Sort of nesting rounded boxes. Is that possible? Thanx, Dzjien.
perhalps i should browse positioniseverything for this.
any tips?
perhalps i should browse positioniseverything for this.
any tips?
if anyone finds this problem interesting, please have a look at http://www.cilla.com/boxtest/
any help, suggestions or ideas would be greatly appreciated. cheers!
Now it is made from 4 pictures one for red, one for green, one for left middle and one for right middle. It works in all browsers and it's your code with 2 other divs.
@ Dzjien: Sure, you can nest as many boxes as you'd like, only I'd probably not use this technique for the containing box as you'll probably need a header and footer section anyway and you could do that styling there.
@heini: sweet table???
@MaBu: nice work :)
Also is anyone using this for rounded buttons. I'm trying to make my links into buttons with a small round rectangle like this - but messing with the paddings gets it all out of whack.
http://server21.dedicateduk.com/~ntrl/events/test/demo.html
I need to be able to put a table in one of these boxes. I've tried different variations (table width in percent, nested divs etc) but none have worked. Any suggestions?
http://server21.dedicateduk.com/~ntrl/events/test/demo.html
I need to be able to put a table in one of these boxes. I've tried different variations (table width in percent, nested divs etc) but none have worked. Any suggestions?
Also, Photoshop apparently has better png compression than other programs. I tried exporting a simple 1000x1000 box with a drop shadow in Illustrator, and the png format was 24K. In Photoshop, the exact same box came to 6.9K. Go figure...
Still, it seems that for an image with >256 colors, gif is still the way to go...
Two lines of code to the .cssbox properties for example...
-------------------
.cssbox {
-moz-opacity: 0.5; /* line one added */
opacity: 0.5; /* line two added */
--------------------
Worked like a charm in firefox!
This is going to be fun playing around with rounded corner boxes placed over gradient image background for example.
This is also a very "nifty" way to achieve CSS corners sans any graphics at all. Relies on a bit of Javascript for dynamic CSS generation. Well worth investigating...
However, it seems the main column - which has a flexible width using min/max - is having problems in I.E.6/Win (and most likely not on some others, either): the right side, body & bottom isn't appearing. The right column (column2) has a fixed width, so those are showing up fine (I will be using the colored boxes in other place at different sizes so I didn't want to put a fixed width on the actual box styles), but I'd like to keep the main column flexible if possible.
Can anyone help me figure out what might fix this?
Just thought I'd thank you for inventing this very useful technique - I've used it on many occasions since you first wrote about it back in 2003.
Just thought I'd point you and any other poeple who may be interested to an article I've written on merging Thrash Box with the Towering Inferno technique from Position is everything. This means you can now have bulletproof, fluid columns using Thrash Box, whist also making all columns equal height. I'm sure this has probably been done already, although more references and onformation on this can't hurt!
Thanks again,
Chris Williams
Doh!
http://www.chriswilliamsdesign.com/article/19/Towering-Thrash-Box-Inferno
I also link to the method I ended up going with instead - the Schillmania Rounded Corners, which I found to be really easy code to follow and eliminates the worry of not having an image large enough for if/when my client creates a really long page full of copy.
I've learned a TON! All the best to everyone in your CSS adventures. :)
I was not however able to set a fixed-height heading. I am not so worried if I need to add a couple of extra tags (although I would of course prefer not to) but can anyone find a way to make this work?
If, for example, there were two columns of these boxes on a page - it might be preferable to have the tops and bottoms of each header / box in-line with each other.
Or, you might have several rounded corner boxes on a page - some with single line headings and others with 2-line headings - and you might want the headings to be the same height irrespective of how many lines are used.
Thanks,
Alex.
1. When using transparent images at the corners, its opacity will be doubled, since images will be layered on top of each other; you can't use real transparent drop shadows (FF only, since IE6 doesn't support transparent 24-bit PNGs)
2. When the image has an odd width or height, resizing them produce artifacts, so always create the box image as having an even width and height (FF only, it seems?)
3. As the text article mentions, you need the image to be 'at least as big' as the div that will contain it, so if you want to use it to be around your whole content/page, you're screwed... because you will need a huge image.
Can somebody come up with solutions to these problems? There's some layout I'm trying and I can't get it to work because I'd need that for the thing to be really adaptive.
And geez, why can't CSS layout be as simple and as straightforward as doing it with tables? I know tables are so 90's, and CSS is so the future omg, but I can't stand how it'd be easier to do this with tables, while I'm spending way too much of my time to come up with a CSS solution that doesn't even work as much as the table one would... and it still has a lot of hacks I have to deal with. Whew. I mean, really.
P.S. This is my first try at web design using all CSS, and making it fluid. So the entire site is kinda rough.
I haven't yet had the time to look at it in more detail but of course some of my questions would be answered in the articles parts 2-4. So if there there already, I really would love to find them. But since it's not even 2007 yet I don't exept them to be anywhere yet ;-).
But thanks for this article and the nice concept.
Thanks Rob.
.boxbody {
background: url(img/sbbody-l.gif) no-repeat bottom left;
margin: 0;
padding: 5px 30px 31px;
}
Try this instead for your padding line:
padding: 1px 5px 30px 31px;
.boxbody {
background: url(img/sbbody-l.gif) no-repeat bottom left;
margin: 0;
padding: 5px 30px 31px;
}
Try this instead for your padding line:
padding: 1px 5px 30px 31px;
I saw that a year before on a web page..
but you improved it...maybe without knowing that something like that existed before..
however great work
thanx!
thanx!
Does anyone know how to have 2 boxes in a single row as when I use the code the 2 boxes are aligned vertically ?
We used the code to form the box on the right and for some reason can't get that little nub poking out to disappear, any suggestions?
http://dbmarketingminer.com/
Is there something I can do to remedy this? It's not a big deal at all but fixing it would satisfy my perfectionist tendencies.
Many Thanks,
Aaron
http://dbmarketingminer.com/
Is there something I can do to remedy this? It's not a big deal at all but fixing it would satisfy my perfectionist tendencies.
Many Thanks,
Aaron
http://dbmarketingminer.com/
Is there something I can do to remedy this? It's not a big deal at all but fixing it would satisfy my perfectionist tendencies.
Many Thanks,
Aaron
http://dbmarketingminer.com/
Is there something I can do to remedy this? It's not a big deal at all but fixing it would satisfy my perfectionist tendencies.
Many Thanks,
Aaron
www.codepal.co.nr
how can I place a/two float containers into cssbox_body? I tring it for hours - but I have no idea :(
I'm trying to get the box to function as a 'target area' where the background changes on rollover (eg. the download button http://www.mozilla.com/en-US/firefox/). Any ideas on how to do this by modifying the above code?
(I would use the same code as the mozilla page, but need the box to be able to stretch both horizontally and vertically)
Secondly - on the referring page to this blog, you have something about the widthless boxes, and they work - but they use two different images, not just the one .png... is there a way to do this with the current thrashbox implementation with the .png? Or was that abandoned?
Thirdly - I am using the thrashbox code, but I'm finding that it gets a bit horked when I put a lot of data into it - as if the image doesn't grow with the text being entered into the div field - is there a way to get around this? Do I need to do something to my margins to get the image to grow with the text or other content I enter into it?
Thanks again.
Thanks
You must be logged into the forums to comment. Please login
http://beta.blissdev.com - an example