{"id":345,"date":"2005-12-13T10:55:17","date_gmt":"2005-12-13T10:55:17","guid":{"rendered":"http:\/\/www.stevepugh.net\/VTT\/?p=345"},"modified":"2016-01-26T15:17:57","modified_gmt":"2016-01-26T15:17:57","slug":"cross-browser-unintrusive-fieldsets-and-legends","status":"publish","type":"post","link":"https:\/\/stevepugh.net\/VTT\/2005\/12\/cross-browser-unintrusive-fieldsets-and-legends\/","title":{"rendered":"Cross-browser unobtrusive fieldsets and legends"},"content":{"rendered":"<p>I was tidying up some web pages (Goodbye single column table, hello unordered list; goodbye multiple level one headings, hello heading hierarchy) when I came across a heading followed by a group of checkboxes. Looks like a <code>fieldset<\/code> with a <code>legend<\/code>, I thought, but the page design really wouldn&#8217;t be helped by the default presentation of these elements in most browsers. So how make to make these elements completely unobtrusive?<br \/>\n<!--more--><\/p>\n<p>Here&#8217;s the default styling (ignore the dotted box, that&#8217;s just to separate each example on this page):<\/p>\n<div style=\"border: 1px dotted; margin: 0.5em; padding: 0.5em;\">Text before the <code>fieldset<\/code><\/p>\n<fieldset>\n<legend>Text inside the <code>legend<\/code><\/legend>\n<p>Text inside the <code>fieldset<\/code><\/fieldset>\n<\/div>\n<p>My mission is to get the start of all three sentences to align.<\/p>\n<p>First thing I tried was setting the margins, padding and borders to zero.<\/p>\n<div style=\"border: 1px dotted; margin: 0.5em; padding: 0.5em;\">Text before the <code>fieldset<\/code><\/p>\n<fieldset style=\"border: none; margin: 0; padding: 0;\">\n<legend style=\"border: none; margin: 0; padding: 0;\">Text inside the <code>legend<\/code><\/legend>\n<p>Text inside the <code>fieldset<\/code><br \/>\n<\/fieldset>\n<\/div>\n<p>Works great in Gecko based browsers but the <code>legend<\/code> is still indented in Opera and IE.<\/p>\n<p>IE can be fixed by applying a negative left margin. This has no effect in Gecko and Opera.<\/p>\n<div style=\"border: 1px dotted; margin: 0.5em; padding: 0.5em;\">Text before the <code>fieldset<\/code><\/p>\n<fieldset style=\"border: none; margin: 0; padding: 0;\">\n<legend style=\"border: none; margin: 0 0 0 -7px; padding: 0;\">Text inside the <code>legend<\/code><\/legend>\n<p>Text inside the <code>fieldset<\/code><\/fieldset>\n<\/div>\n<p>The margin needs to be -7px regardless of the font size set in IE (no need for ems).<\/p>\n<p>Opera can be fixed by making the <code>legend<\/code> a relatively positioned element and moving it 10 pixels to the left.<\/p>\n<div style=\"border: 1px dotted; margin: 0.5em; padding: 0.5em;\">Text before the <code>fieldset<\/code><\/p>\n<fieldset style=\"border: none; margin: 0; padding: 0;\">\n<legend style=\"border: none; margin: 0; padding: 0; position: relative; left: -10px;\">Text inside the <code>legend<\/code><\/legend>\n<p>Text inside the <code>fieldset<\/code><\/fieldset>\n<\/div>\n<p>This also affects IE, but as the difference is 7px vs 10px we need to apply different styles to different browsers. Conditional Comments are handy here, or any number of CSS hacks.<\/p>\n<p>As I&#8217;m using inline styles in this example the <a href=\"http:\/\/www.dithered.com\/css_filters\/css_only\/important_property.html\">!important hack<\/a> works. With IE7 around the corner we need to take care with CSS hacks that distinguish between IE6 and better browsers (hacks that distinguish between IE5.x on the one hand and IE6\/better browsers on the other are still safe) so in a real example Conditional Comments would be preferable.<\/p>\n<div style=\"border: 1px dotted; margin: 0.5em; padding: 0.5em;\">Text before the <code>fieldset<\/code><\/p>\n<fieldset style=\"border: none; margin: 0; padding: 0;\">\n<legend style=\"border: none; margin: 0; padding: 0; position: relative; left: -10px !important; left: -7px;\">Text inside the <code>legend<\/code><\/legend>\n<p>Text inside the <code>fieldset<\/code><\/fieldset>\n<\/div>\n<p>The code:<\/p>\n<pre><code>Text before the fieldset\n&lt;fieldset style=&quot;border: none; margin: 0; padding: 0;&quot;&gt;\n&lt;legend style=&quot;border: none; margin: 0; padding: 0;\nposition: relative; left: -10px !important; left: -7px;&quot;&gt;\nText inside the legend\n&lt;\/legend&gt;\nText inside the fieldset\n&lt;\/fieldset&gt;<\/code><\/pre>\n<p>Or in more generic terms<\/p>\n<pre><code>fieldset, legend {border: none; margin: 0; padding: 0;}\nlegend {position: relative; left: -10px;}<\/code><\/pre>\n<p>and then in an IE only stylesheet:<\/p>\n<pre><code>legend {left: -7px;}<\/code><\/pre>\n<p>(Testing in Safari\/Konquerer still required, anyone with any other browsers is free to experiment and leave a comment with feedback.)<\/p>\n<p>There is an issue with Gecko browsers. If the text size is changed on the fly then the relatively positioned legend is misplaced on the page. A reload fixes things but the best solution is the make sure that your text doesn&#8217;t need resizing by always using the users preferred text size anyway.<\/p>\n<p>Finally, IE makes the text of the <code>legend<\/code> blue by default. You don&#8217;t need me to tell you how to fix that.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I was tidying up some web pages (Goodbye single column table, hello unordered list; goodbye multiple level one headings, hello heading hierarchy) when I came across a heading followed by a group of checkboxes. Looks like a fieldset with a legend, I thought, but the page design really wouldn&#8217;t be helped by the default presentation of these elements in most browsers. So how make to make these elements completely unobtrusive?<\/p>\n<p><a href=\"http:\/\/www.stevepugh.net\/VTT\/?p=345\">Continue reading this very true thing<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[15],"tags":[74],"class_list":["post-345","post","type-post","status-publish","format-standard","hentry","category-www","tag-css"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p1fxq5-5z","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/stevepugh.net\/VTT\/wp-json\/wp\/v2\/posts\/345","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/stevepugh.net\/VTT\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/stevepugh.net\/VTT\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/stevepugh.net\/VTT\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/stevepugh.net\/VTT\/wp-json\/wp\/v2\/comments?post=345"}],"version-history":[{"count":1,"href":"https:\/\/stevepugh.net\/VTT\/wp-json\/wp\/v2\/posts\/345\/revisions"}],"predecessor-version":[{"id":3030,"href":"https:\/\/stevepugh.net\/VTT\/wp-json\/wp\/v2\/posts\/345\/revisions\/3030"}],"wp:attachment":[{"href":"https:\/\/stevepugh.net\/VTT\/wp-json\/wp\/v2\/media?parent=345"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/stevepugh.net\/VTT\/wp-json\/wp\/v2\/categories?post=345"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/stevepugh.net\/VTT\/wp-json\/wp\/v2\/tags?post=345"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}