Skip to Site Navigation | Skip to Content

Blog

A technique for rounded transparent boxes using PNG images and CSS

In my last post, I described some design changes to the content management system’s element control bars used to edit content. The new UI design called for control bars with some clean, rounded edges. The problem is that the control bars appear inside the site’s visual design template so we have no idea, on a system-level, what color or image the control bars will appear on top of. This presented some contrast issues as described in the aforementioned post, but also required us to use actual transparency.

Sliding Doors

Typically, when you need to create rounded containers with CSS, Doug Bowman’s Sliding Doors technique is the way to go. Unfortunately, sliding doors is best for containers that are only rounded on two edges (think tabs instead of buttons), or have one fixed dimension (buttons that get wider but not taller), and are in front of a regular, known background. In our case, the the boxes must scale to most any width and height, are round on all four sides, and must be actually transparent — including some subtle drop-shadows. Phew.

For those of you who know your way around CSS and are unfamiliar with the technique, sliding doors uses two HTML elements, two images, and some clever CSS to create a rounded tab that will expand to hold its contents in some limited ways. The solution is elegant (no extra markup in most cases) and reliable in modern browsers. The two images consist of a narrow left edge graphic, and a wider right edge image. The left edge covers the right, completing the illusion. However, the rounded images are GIF or JPG graphics that are square and match the background color. A true transparent image would break the effect. So, I realized that while I could use what I new about sliding doors to find a solution, I would have to try something new.

The Solution

I knew right off that elegance would by and large go out the window. The technique is pretty simple, but requires extra, non-semantic mark-up. But here is the general idea for truly transparent sliding doors. Traditional sliding doors pad the outer container so that the nested element will show the other piece behind it. It covers the wider image. My technique places them side-by-side so that the transparency can work. We’ll use two nested DIV elements, the outer having a negative margin that is exactly as wide as the "corner" image, and a give the nested DIV a negative margin to match, pulling it outside the outer element. It works surprisingly well in all of the modern browsers. Here are a few details.

The markup:


<div class="actions">

<div class="roundedTop"><div class="rLeft"></div></div>

</div>


The CSS:


.roundedTop {

height: 6px;

background: url(actionsBar_RT.png) no-repeat right top;

margin-left: 6px;

}


.roundedTop .rLeft {

height: 6px;

background: url(actionsBar_LT.png) no-repeat left top;

margin-left: -6px;

}


That’s the code for the two top pieces Here’s a graphic that shows the individual graphics and DIV boundaries. The final piece is a top, middle, and bottom repeating the technique above all wrapped in a containing DIV:

diagram

That’s it. Feel free to use this technique in your website designs. I have attached a ZIP file that contains all of the HTML and CSS in a working example for you to experiment with. Let us know in the comments if you find any bugs or improvements to this idea.

5 comments (Add your own)

1. Chuck Norris wrote:
This technique does not work in IE6. Does Element Fusion support IE6?

Tue, September 16, 2008 @ 11:01 AM

2. Jason Zimdars wrote:
Chuck, I had no idea you were so active in web standards and development. I'll tread lightly as I answer this. Nobody likes a round-house kick to the head ;) I do think that technically it is possible to make this effect work with Internet Explorer 6 and here at Element Fusion all of our professionally designed websites and web applications support IE6. What we take is the progressive enhancement approach to IE 6. Everything is fully functional, but may not have all of the advanced visual effects or fully design as you might get with a more modern web browser. Typically we can still make our work nearly identical when viewed with IE6. However in this case there were other considerations to take into account. Since all of the more modern browsers, including the current version of IE - 7, supported this without necessitating hacks or external scripts we chose to degrade the experience for IE 6 users. Partly because IE6 is a very small portion of our user base and partly because fully supporting IE 6 would have meant resorting to the typical script additions that are used to enable PNG transparency support along with probably some conditional CSS. Those hacks were deemed not worth the visual payoff when balanced with the performance trade-offs. You see, our CMS admin tools run on top of the user's design theme, inline with the content. So any scripts required to render the UI have to avoid conflicting with scripts running in the site user's templates. So it could have caused conflicts for designers who implemented their own IE 6 compatibility scripts or did not want them at all. Eventually I might get to revising this technique to include IE 6 support - I think it would be trivial to do so - but for now it is very representative of how we are using it in the real world. Thanks for your feedback!

Tue, September 16, 2008 @ 12:46 PM

3. mhadz wrote:
nice one.

Mon, October 20, 2008 @ 6:57 AM

4. Matthew Blackmon wrote:
When I implemented your example, at certain heights the background image (actionsBar) does not fill the area surrounded by the div.

Sun, July 19, 2009 @ 6:45 PM

5. tomexx wrote:
Does not work for IE6. That means it is not usable today. :(

Thu, April 1, 2010 @ 10:28 AM

Add a New Comment

Enter the code you see below:
code
 

Comment Guidelines: No HTML is allowed. Off-topic or inappropriate comments will be edited or deleted. Thanks.