Creating Custom Corners & Borders
Written by jon on 12:08 AMRounded corners are one of the most frequently requested CSS techniques. As with many things in CSS, there are various ways in which this problem can be approached. In this article, I'll look at the pros and cons of some common techniques and introduce a new technique that utilises both CSS and JavaScript. Before we dive in to the CSS, let's remind ourselves of the old fashioned approach to this problem, which uses layout tables: | |
| |
A few years ago this would have been an acceptable solution. Today, it's an ugly hack: that's an awful lot of redundant markup for a relatively unimportant visual decoration. In fact, the above code won't even function as intended in documents served using a strict doctype -- small gaps will appear beneath the corner images, caused by the fact that images are inline elements and, hence, leave space beneath the image for the "tails" on letters such as 'y' and 'j'. The solution, as explained by Eric Meyer in Images, Tables and Mysterious Gaps, is to add the following rule to your stylesheet: td img { display: block; } | |
But, now we're using CSS hacks to fix ugly table hacks! Let's look at ways to implement the same effect using only CSS. As a general rule, any decorative image should be implemented as a CSS background image on an existing page element, rather than being dropped in to the page proper using an <img> tag. It's easy to determine whether an image is decorative or contains actual content: ask yourself if the absence of the image would have any effect on the overall content of the page. In the case of rounded corners, the answer is obviously not. CSS background images are remarkably powerful things. You need only look at the many wonderful designs on display at the CSS Zen Garden for evidence of that. Using CSS, a background image can be applied to any element on a page. Furthermore, it can be repeated horizontally, vertically or not at all; it can be positioned within the background area of the image using absolute measurements, or relative to one of the four corners; it can even be made to stay fixed in place when the element's content scrolls. Unfortunately, CSS 2 imposes one small but significant limitation: you can only apply a single background image to each element on the page. To properly render rounded corners on a <div> we need to apply four background images, one in each corner. Fixed Width BoxesIf the width of the box to which we're applying decorative corners is fixed, half of the problem is solved already. If we know that the box will always be 200 pixels wide, instead of creating four background images (one for each corner), we can create two: one for the top of the box and one for the bottom. The challenge is now reduced to applying two background images to our <div>. It's time to take advantage of our markup. A box with rounded corners wouldn't be much fun if it didn't contain any content. Consider the following: | |
| |
Nested ElementsApplying four backgrounds to a single div is still out of our reach. But what if we nested four divs, one for each background? Doing so solves our problem, but comes at the expense of additional markup with no structural value: <div class="rounded"><div><div><div> And, in the CSS: div.rounded { | |
It should be clear what's going on here. Each of the four divs is assigned a rounded corner background image, positioned in the top-right, top-left, bottom-right and bottom-left respectively. While the width of the containing div is set to 200px, it could just as easily be set to something more flexible for use with liquid designs -- the corners would still work, no matter how large or small the containing div was. We now have a solution to the problem, which uses far less markup than the original tables example. But, it's still not perfect: it uses three extra divs, which add nothing of value to the overall document structure. Can we do any better? It's time to look to JavaScript. Using theUsing JavaScript and the DOM, it's possible to manipulate the structure of a document after it has been loaded by the browser. Rounded corners are a presentational effect that can be hidden from non-JavaScript user agents without any significant reduction in their overall experience of the site, so there are no ethical problems with using JavaScript for this kind of transformation. Our final solution will require only a single <div> in the source document. We will use JavaScript to dynamically append the three extraneous divs needed for the rounded corner effect. | |
|
0 comments: Responses to “ Creating Custom Corners & Borders ”