Looking to style form fieldsets with margins, while removing the margin-top for the first non-hidden fieldset and margin-bottom for the last non-hidden fieldset (any fieldset can be dynamically set as hidden by a script). I attempted the CSS below without success (fs2 is supposed to have no margin-top but does; fs3 is supposed to have no margin-bottom but does). Is there a way to achieve this using CSS3?
form > fieldset {
margin: .5em;
}
/* this doesn't work */
form > fieldset:not([hidden]):first-of-type {
margin-top: 0;
}
/* this doesn't work */
form > fieldset:not([hidden]):last-of-type {
margin-bottom: 0;
}
<form>
<fieldset hidden>fs1</fieldset>
<fieldset>fs2</fieldset>
<fieldset>fs3</fieldset>
<fieldset hidden>fs4</fieldset>
</form>