【问题标题】:Overwrite img vertical align inside table cell (Chrome)覆盖表格单元格内的 img 垂直对齐 (Chrome)
【发布时间】:2015-04-08 18:41:01
【问题描述】:

我需要能够覆盖已定义垂直对齐的 td 标记内的图像的垂直对齐。 我尝试给图像垂直对齐:中间,虽然这在 ff 中有效,但图像仍然低于 chrome 中的线。 html 是通过 bbcode 编辑器提供的。

<table>
<tr>
    <td style="vertical-align: top">
        The text in table cell needs to align to top
        <br/>
        <br/>            

        image should vertically center relative to line <img src="https://jsfiddle.net/img/logo.png"/>
    </td>
</tr>

td {
    border: 1px solid blue;
    height: 200px;
}
td img {
    vertical-align: middle;
}

fiddle

适用于 Firefox,但不适用于 chrome。

【问题讨论】:

    标签: html css google-chrome


    【解决方案1】:

    td 的中间垂直对齐将解决您的问题:

    <td style="vertical-align: middle">
    

    https://jsfiddle.net/6okr7ayy/7/


    "vertical-align: top" 不能更改或删除(它被 bbcode 编辑器用于各种目的)

    然后尝试在 CSS 中使用 !important 覆盖它

    td {
        vertical-align: middle !important;
    }
    

    【讨论】:

    • "vertical-align: top" 无法更改或删除(bbcode 编辑器将其用于各种目的)
    • td 需要保持顶部垂直对齐。我更新了小提琴以更好地显示问题。
    【解决方案2】:

    将 td 中的文本和图像包装成一个段落。现在您的代码也可以在 Chrome 上运行:

    <table>
     <tr>
      <td>
        <p>The text in table cell needs to align to top
        <br/>
        <br/>image should vertically center relative to line 
        <img src="https://jsfiddle.net/img/logo.png"/></p>
      </td>
     </tr>
    </table>
    

    CSS 保持不变:

    td {
    border: 1px solid blue;
    height: 200px;
    }
    td img {
    vertical-align: middle;
    }
    

    See the fiddle.

    【讨论】:

      【解决方案3】:

      在你的 CSS 中用 !important 覆盖标准的垂直对齐属性:

      更新

      在您的文本中添加一个元素并垂直对齐您的图像

      td img {
          background: red;
          height: 40px;
          vertical-align: middle;
      }
      td {
          border: 1px solid blue;
          height: 200px;
      }
      td img{vertical-align:middle}
      <table>
          <tr>
              <td style="vertical-align: top">
                  The text in table cell needs to align to top The text in table cell needs to align to top The text in table cell needs to align to top
                  <br/>
                  <br/>            
                  
                  <span>image should vertically center relative to line</span> <img src="https://jsfiddle.net/img/logo.png"/>
              </td>
          </tr>
      </table>

      【讨论】:

      • "vertical-align: top" 无法更改或删除(bbcode 编辑器将其用于各种目的)
      • 我修改了我的代码!现在它适用于垂直对齐:顶部
      • td 需要保持顶部垂直对齐。我更新了小提琴以更好地显示问题。
      • 我能想到的唯一解决方案是在你的文本中添加一个jsfiddle.net/6okr7ayy/13,我也再次修改了代码sn-p。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-06-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-03
      • 1970-01-01
      • 2014-08-18
      相关资源
      最近更新 更多