在 TD 内添加控件时表项垂直对齐方式发生变化

发布于 2024-08-15 17:18:27 字数 691 浏览 3 评论 0原文

我一直想知道这个问题。为什么在将控件放入 td 内部时,td 的对齐会受到影响。

例如。

<tr>
  <td>Row 1</td>
  <td>
    <input type="text" />
    <input type="button" value="Select" />
  </td>
  <td>Selected Value 1</td>
</tr>
<tr>
  <td>Row 2</td>
  <td colspan="2">
    <input type="text" />
    <input type="button" value="Select" />
    Selected Value 2
  </td>
</tr>

第 1 行”文本与“选定值 1”对齐。但是,文本“选定值 2”稍微向下且未居中,且未与“行 2”对齐。

我已经尝试过垂直对齐中间甚至 valign 但它不起作用。 IE 和 Firefox 中都会发生这种情况。在IE中表现得更明显。我真的不明白这一点。

I have been wondering about this. Why does the alignment of the td gets affected when placing controls inside it.

For example.

<tr>
  <td>Row 1</td>
  <td>
    <input type="text" />
    <input type="button" value="Select" />
  </td>
  <td>Selected Value 1</td>
</tr>
<tr>
  <td>Row 2</td>
  <td colspan="2">
    <input type="text" />
    <input type="button" value="Select" />
    Selected Value 2
  </td>
</tr>

The "Row 1" text is aligned with "Selected Value 1". However, the text "Selected Value 2" is slightly down and not centered and not aligned with "Row 2".

I already tried vertical align middle or even valign and it does not work. It happens in both IE and Firefox. It is more obvious in IE. I really do not get this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

糖粟与秋泊 2024-08-22 17:18:27

这是因为第二行中的组件与文本“内联”显示,并且组件的高度大于文本的行高。

如果将所有文本的行高更改为更大的值,它们都会对齐。

td  {line-height:500%;}

it's because the components in the 2nd row are being displayed "inline" with the text and the heights of the components are larger than the line height of the text.

if you changed the line heights for all the pieces of text to a larger value, they would all line up.

td  {line-height:500%;}
暖阳 2024-08-22 17:18:27

输入控件的行高与标准文本不同。因此,当您将这些原生内联控件放置在文本旁边时,它们会强制基线/行高的行为与没有它的文本的行为不同。如果您将图像放置在文本旁边而不使其浮动(文本与图像底部对齐,直到换行到下一行),也会发生这种情况。

您应该能够通过将输入控件向左浮动或更改文本的行高来避免此问题。

编辑:这似乎对我来说效果很好..

<table>
    <tr>
        <td>Row 1</td>
        <td>
            <input type="text" />
            <input type="button" value="Select" />
        </td>
        <td>Selected Value 1</td>
    </tr>
    <tr>
        <td style="">Row 2</td>
        <td style="line-height: 120%" colspan="2">  
            <input type="text" />  
            <input type="button" value="Select" />  
            Selected Value 2
        </td>
    </tr>
</table>

The input controls have different line-heights from the standard text. So when you place these, natively inline, controls next to text they force the baseline/lineheight to behave differently than the text would behave without it. This also happens if you place an image next to text without floating it (the text aligns itself with the bottom of the image, until it wraps to the next line).

You should be able to circumvent this by floating your input controls to the left, or possibly by changing the line-height of the text.

Edit: This seemed to work fine for me..

<table>
    <tr>
        <td>Row 1</td>
        <td>
            <input type="text" />
            <input type="button" value="Select" />
        </td>
        <td>Selected Value 1</td>
    </tr>
    <tr>
        <td style="">Row 2</td>
        <td style="line-height: 120%" colspan="2">  
            <input type="text" />  
            <input type="button" value="Select" />  
            Selected Value 2
        </td>
    </tr>
</table>
回忆凄美了谁 2024-08-22 17:18:27

当我尝试将垂直对齐的中间添加到 td 内的另一个控件时,它似乎将所有内容设置在中间。这在 IE 中是一种奇怪的行为,但它确实解决了我的问题。

<tr>
  <td>Row 1</td>
  <td>
    <input type="text" />
    <input type="button" value="Select" />
  </td>
  <td>Selected Value 1</td>
</tr>
<tr>
  <td>Row 2</td>
  <td colspan="2">
    <input type="text" style="vertical-align: middle;" />
    <input type="button" value="Select" style="vertical-align: middle;" />
    Selected Value 2
  </td>
</tr>

When I tried adding vertical aligned middle to the other control inside the td, it seems to set all of the at the middle. It is a weird behavior in IE but it did fix my problem.

<tr>
  <td>Row 1</td>
  <td>
    <input type="text" />
    <input type="button" value="Select" />
  </td>
  <td>Selected Value 1</td>
</tr>
<tr>
  <td>Row 2</td>
  <td colspan="2">
    <input type="text" style="vertical-align: middle;" />
    <input type="button" value="Select" style="vertical-align: middle;" />
    Selected Value 2
  </td>
</tr>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文