I want to style form fieldsets with a margin, but remove margin-top for the first non-hidden fieldset and margin-bottom for the last non-hidden fieldset (any fieldset may be set hidden by the script dynamically). I tried the following CSS with no luck (fs2 is expected to have no margin-top but actually has; fs3 is expected to have no margin-bottom but actually has). Is there a way to do something like 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>