I personally like the <fieldset>
tag because of how it draws a box and puts the <legend>
at the top of it, over the border. Like this.
However, the fieldset
element was made to organize forms, and using it for general design is no better than using tables for general design. So, my question is... how can I achieve the same result using another tag? The border has to be erased under the <legend>
(or whatever other tag will be used), and since there could be a "complex" body background image, I can't afford to just set the background-color
of the legend to match the one of the element under.
I'd like it to work without JavaScript, but CSS3 and XML-based formats (such as SVG or XHTML) are fine.
Here a possible solution with a focus on simplicity (if you can loose your requirement for transparent background a bit). No additional elements.
I believe you already know the answer.
You have 2 options, provide your own border (that's a lot of unnecessary work) or position an element with occludes the border (the problem with that is that you can only have a solid background color, but maybe that's fine).
To my knowledge there's not anything you can do to make this workout nicely in every browser out there. I like your style though, it's the right approach but probably not a problem you'll be able to solve in a satisfying manner.
My general opinion on these topics is that you should not try and do things with the web that requires A) excessive effort or B) a markup solution which is not entirely obvious to begin with. The web has limitations and you would do well to ad-her to them rather than trying to work around those limitations.
So I'm forced to ask, what's the problem with <legend/>?
Demo jsBin link
I got a reasonable result.
I update a little bit the propostion of Anonymous to that:
Use
::before
pseudoelement to generate the top border of the emulated<fieldset>
, then position the emulated<legend>
element over the::before
block withz-index
andposition
properties. Lastly, use gradient and a solid color-stop on the top of the emulated fieldset, setting the top color of the linear gradient to transparent so whatever background is behind the fake fieldset will be visible.