英文:
I have a problem with CSS GRID on FORMs which have FIELDSETs in them - GRID ends up on both parent and child and then breaks
问题
基本上我在使用CSS网格来创建多列布局的表单,对于普通表单来说这很有效,但如果表单中包含了FIELDSETS,那么它就无法正常工作,因为FIELDSETS是表单元素的父元素,所以在FIELDSETS的情况下,我需要将GRID添加到FIELDSET而不是FORM元素上。
但是下面的CSS将其同时添加到FORM和FIELDSET(即两个嵌套的GRID),然后破坏了GRID结构 - 我只想要一个。
我不确定如何表达“如果表单没有FIELDSET子元素,则将GRID放在表单元素上,否则将其放在FIELDSET上”。
例如:
<form class="form-horizontal">
<fieldset GRID>
<div class="form-group"></div>
<div class="form-group"></div>
</fieldset>
</form>
或者
<form class="form-horizontal" GRID>
<div class="form-group"></div>
<div class="form-group"></div>
</form>
.form-horizontal, .form-horizontal FIELDSET {
display: grid;
/* grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); */
grid-gap: 0px;
grid-template-columns: repeat(auto-fill, minmax(min(350px, 100%), 1fr));
}
上述的CSS会同时添加到两者,就像下面一样:
<form class="form-horizontal" GRID>
<fieldset GRID>
<div class="form-group"></div>
<div class="form-group"></div>
</fieldset>
</form>
尝试使用CSS的:not
运算符,但在这种情况下不太确定如何做。也研究过display: contents
特性,但不确定如何在这里使用它……我不希望FIELDSET失去所有的块级样式,而且似乎也不起作用。但我确实希望在两种情况下只在FORM元素上使用GRID,并跳过FIELDSET元素,就像display: contents
应该做的那样。
英文:
Basically I am using CSS grid to make forms multi-column layout and this works fine for normal forms, but if the forms have FIELDSETS then it doesn't work as they are a parent of the form elements so in case of FIELDSETS I need the GRID to be on the FIELDSET instead of the FORM element.
But below CSS adds it to both FORM and FIELDSET (ie two nested GRIDS) and then breaks the GRID structure - only want one.
I'm not sure how to say "put the GRID on the form element if it's got NO FIELDSET child, otherwise put it on the FIELDSET instead"
ie
<form class="form-horizontal">
<fieldset GRID>
<div class="form-group">
</div>
<div class="form-group">
</div>
</fieldset>
</form>
or
<form class="form-horizontal" GRID>
<div class="form-group">
</div>
<div class="form-group">
</div>
</form>
.form-horizontal, .form-horizontal FIELDSET {
display: grid;
/* grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); */
grid-gap: 0px;
grid-template-columns: repeat(auto-fill, minmax(min(350px, 100%), 1fr));
}
Above CSS ends up adding to both like
<form class="form-horizontal" GRID>
<fieldset GRID>
<div class="form-group">
</div>
<div class="form-group">
</div>
</fieldset>
</form>
Tried using CSS :not operator but not really sure how to do it in this case.
Also looked at the display: contents feature but not sure how to use that here... i don't want the FIELDSET to lose all block styling and doesn't seem to work. But I do want the GRID on only the FORM element in both cases and skip over the FIELDSET element like display: contents is supposed to do.
答案1
得分: 1
以下是您早前评论中提到的示例的翻译:
.form-horizontal {
display: grid;
grid-gap: 0px;
grid-template-columns: repeat(auto-fill, minmax(min(150px, 100%), 1fr));
}
.form-horizontal fieldset {
display: inherit;
grid-gap: inherit;
grid-template-columns: inherit;
grid-column: 1/-1;
}
form div {
border: solid;
}
<form class="form-horizontal">
<fieldset>
<div class="form-group">
fieldset中的div
</div>
<div class="form-group">
fieldset中的div
</div>
</fieldset>
<fieldset>
<div class="form-group">
fieldset中的div
</div>
<div class="form-group">
fieldset中的div
</div>
</fieldset>
</form>
或
<form class="form-horizontal">
<div class="form-group">
直接子元素中的div
</div>
<div class="form-group">
直接子元素中的div
</div>
<fieldset>
<div class="form-group">
fieldset中的div
</div>
<div class="form-group">
fieldset中的div
</div>
</fieldset>
</form>
请注意,这是您提供的示例的翻译。如果您需要进一步的信息或帮助,请随时提出。
英文:
below an example that belongs to my earlier comment:
> You need to make the fieldset fill the whole row as well after it inherits display and grid-template-columns . add for the fieldset : grid-column:1/-1;
and it should lay through every columns of the form. (fieldset should be written with lowercase btw )
fieldset has a padding that offset its div from the div which are direct children.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-css -->
.form-horizontal {
display: grid;
grid-gap: 0px;
grid-template-columns: repeat(auto-fill, minmax(min(150px, 100%), 1fr));
}
.form-horizontal fieldset {
display: inherit;
grid-gap: inherit;
grid-template-columns: inherit;
grid-column: 1/-1;
}
form div {
border: solid;
}
<!-- language: lang-html -->
<form class="form-horizontal">
<fieldset>
<div class="form-group">
div in fieldset
</div>
<div class="form-group">
div in fieldset
</div>
</fieldset>
<fieldset>
<div class="form-group">
div in fieldset
</div>
<div class="form-group">
div in fieldset
</div>
</fieldset>
</form>
or
<form class="form-horizontal">
<div class="form-group">
div direct child
</div>
<div class="form-group">
div direct child
</div>
<fieldset>
<div class="form-group">
div in fieldset
</div>
<div class="form-group">
div in fieldset
</div>
</fieldset>
</form>
custom attributes are to be written via the data-attribute syntax, so GRID would better be data-grid ;)
<!-- end snippet -->
demo playing with spacing
<!-- begin snippet: js hide: true console: true babel: false -->
<!-- language: lang-css -->
* {
box-sizing: border-box;
}
.form-horizontal {
padding: 0.9em;
display: grid;
grid-gap: 0px;
grid-template-columns: repeat(auto-fill, minmax(min(150px, 100%), 1fr));
}
.form-horizontal fieldset {
display: inherit;
grid-gap: inherit;
grid-template-columns: inherit;
grid-column: 1/-1;
margin: 0 -.9em;
}
form div {
border: solid;
}
<!-- language: lang-html -->
<form class="form-horizontal">
<fieldset>
<div class="form-group">
div in fieldset
</div>
<div class="form-group">
div in fieldset
</div>
</fieldset>
<fieldset>
<div class="form-group">
div in fieldset
</div>
<div class="form-group">
div in fieldset
</div>
</fieldset>
</form>
or
<form class="form-horizontal">
<div class="form-group">
div direct child
</div>
<div class="form-group">
div direct child
</div>
<fieldset>
<div class="form-group">
div in fieldset
</div>
<div class="form-group">
div in fieldset
</div>
</fieldset>
</form>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论