HTML-CSS: How to put a form/table with fieldset to the center but keeping the fieldset as the same size as the form/table?

huangapple go评论58阅读模式
英文:

HTML-CSS: How to put a form/table with fieldset to the center but keeping the fieldset as the same size as the form/table?

问题

在HTML中,与<form>一起使用的是<table>。它如下所示:

<form ...>
  <table>
    <tr>
      <td></td>
      <td></td>
    </tr>
    ...
    <tr>
      <td></td>
      <td></td>
    </tr>
  </table>
</form>

要将此“表单/表格”置于页面中间,可以使用以下CSS:

table {
  margin: 0px auto;
}

来源:Center-align a HTML table

直到一切都正常。

情况是,当向<form>添加<fieldset><legend>时,实际上是添加到了<table>。因此,现在的结构如下:

<form ...>
  <fieldset>
    <legend>一些描述 ...</legend>
    <table>
      <tr>
        <td></td>
        <td></td>
      </tr>
      ...
      <tr>
        <td></td>
        <td></td>
      </tr>
    </table>
  </fieldset>
</form>

这里出现了两种情况:

  1. 如何使<fieldset>的边框大小与表单/表格本身相同?

在这里应用了以下CSS:

fieldset {
  display: inline-block;
}

来源:有没有办法让fieldset的宽度仅与其中的控件一样宽?

即使它按预期工作,<table>仍然位于左侧。

问题

  • 如何将带有<fieldset>的表单/表格置于中间,但保持<fieldset>与表单/表格的大小相同?
英文:

In HTML for a &lt;form&gt; used together with a &lt;table&gt;. It such as:

&lt;form ...&gt;
 &lt;table&gt;
   &lt;tr&gt;
    &lt;td&gt;&lt;/td&gt;
    &lt;td&gt;&lt;/td&gt;
   &lt;/tr&gt;
   ...
   &lt;tr&gt;
    &lt;td&gt;&lt;/td&gt;
    &lt;td&gt;&lt;/td&gt;
   &lt;/tr&gt;
 &lt;/table&gt;
&lt;/form&gt;

To put this form/table in the middle of the page is used CSS as follows:

table {
   margin: 0px auto;
}

Credits from:

Until all is ok.

The situation is when is added &lt;fieldset&gt; and legend to the &lt;form&gt;, well really to the &lt;table&gt;. Therefore the structure is now as follows

&lt;form ...&gt;
&lt;fieldset&gt;
&lt;legend&gt;Some Description ...&lt;/legend&gt;
 &lt;table&gt;
   &lt;tr&gt;
    &lt;td&gt;&lt;/td&gt;
    &lt;td&gt;&lt;/td&gt;
   &lt;/tr&gt;
   ...
   &lt;tr&gt;
    &lt;td&gt;&lt;/td&gt;
    &lt;td&gt;&lt;/td&gt;
   &lt;/tr&gt;
 &lt;/table&gt;
&lt;/fieldset&gt;
&lt;/form&gt;

Here arises two situations:

  1. How to put the &lt;fieldset&gt;'s border as the same size as the form/table itself?

Here as applied

fieldset {
	 display: inline-block;
}

Credits from:

Even when it works as expected, the &lt;table&gt; returns to the left.

Question

  • How to put a form/table with fieldset to the center but keeping the fieldset as the same size as the form/table?

答案1

得分: 1

像这样吗?

<!-- 开始代码段:js 隐藏: false 控制台: true Babel: false -->

<!-- 语言:lang-css -->
form {
    display: table;
    margin: 0 auto;
}
table {
    margin: 0 auto;
}
fieldset {
    display: inline-block;
}

<!-- 语言:lang-html -->
<form>
    <fieldset>
        <legend>一些描述...</legend>
        <table>
            <tr>
                <td></td>
                <td></td>
            </tr>
            <tr>
                <td></td>
                <td></td>
            </tr>
        </table>
    </fieldset>
</form>

<!-- 结束代码段 -->

请注意,我已经保留了原始代码的格式和标记。

英文:

Something like this?

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

form {
    display: table;
    margin: 0 auto;
}
table {
   margin: 0 auto;
}
fieldset {
   display: inline-block;
}

<!-- language: lang-html -->

&lt;form&gt;
  &lt;fieldset&gt;
    &lt;legend&gt;Some Description ...&lt;/legend&gt;
    &lt;table&gt;
      &lt;tr&gt;
        &lt;td&gt;&lt;/td&gt;
        &lt;td&gt;&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
        &lt;td&gt;&lt;/td&gt;
        &lt;td&gt;&lt;/td&gt;
      &lt;/tr&gt;
    &lt;/table&gt;
  &lt;/fieldset&gt;
&lt;/form&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年7月14日 00:36:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/76681598.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定