html - 为什么我的图像将我的 <td> 推到右边那么远?

标签 html css

我正在尝试复制 Facebook 页面帖子的设计和结构。

body {
    font-family: Helvetica Neue, Helvetica, Arial, sans-serif;
}
a {
    color: #365899;
    cursor: pointer;
    text-decoration: none;
}
.post td {
    vertical-align: top;
}
.timestamp {
    font-size: 13px;
    color: #999;
}
<div class="post">
    <table>
        <tbody>
            <tr>
                <td>
                    <img src="https://scontent-lhr3-1.xx.fbcdn.net/v/t1.0-1/c34.34.431.431/s50x50/229746_457468357668297_1899772142_n.png?oh=a6c0ddb505a2485280d1661c1ee087df&oe=5916C9DF">
                </td>
                <td>
                    <table>
                        <tr>
                            <td>
                                <strong><a>Toomblr</a></strong>
                            </td>
                        </tr>
                        <tr class="timestamp">
                            <td>
                                20/01/2017 - 11:43
                            </td>
                        </tr>
                    </table>
                </td>
            </tr>
            <tr>
                <td>
                    Some content.
                </td>
            </tr>
        </tbody>
    </table>
</div>

这是我拥有的 JSFiddle:https://jsfiddle.net/6nodfbdj/

如您所见,图像只有 50x50,但它无缘无故地将 td 一直推到外面。

不知何故,它与包含内容的 td 冲突。所以基本上发生了什么,无论内容 td 的宽度是多少,也会为包含图像的 td 设置。

有什么想法吗?

最佳答案

如评论之一所述,表格不用于格式化。它们用于表格数据。

来自 w3.org 网站

Tables should not be used purely as a means to layout document content as this may present problems when rendering to non-visual media. Additionally, when used with graphics, these tables may force users to scroll horizontally to view a table designed on a system with a larger display. To minimize these problems, authors should use style sheets to control layout rather than tables.

Note. This specification includes more detailed information about tables in sections on https://www.w3.org/TR/html401/appendix/notes.html#notes-tables

您应该改用 CSS。虽然布局和大小与您的 fiddle 不完全相同,但请查看 https://jsfiddle.net/ringhidb/jeeu2yrt/

<!DOCTYPE html>
  <html>
    <head>
      <meta http-equiv="content-type" content="text/html; charset=UTF-8">
      <style type="text/css">
        body {
          font-family: Helvetica Neue, Helvetica, Arial, sans-serif;
          font-size: 16px;
        }

a {
  color: #365899;
  cursor: pointer;
  text-decoration: none;
}

.post {
  position: absolute;
}

.header {}

.logo {
  float: left;
  padding: .1em;
}

.info {
  float: left;
  padding: .25em;
}

.site {
  font-size: 1.02em;
  font-weight: bold;
}

.timestamp {
  font-size: .75em;
  color: #999;
}

.content {
  clear: both;
}

  </style>

 <title>CSS Layout</title>
 
</head>

<body>
  <div class="post">
    <div class="header">
      <div class="logo">
        <img src="https://scontent-lhr3-1.xx.fbcdn.net/v/t1.0-1/c34.34.431.431/s50x50/229746_457468357668297_1899772142_n.png?oh=a6c0ddb505a2485280d1661c1ee087df&oe=5916C9DF">
      </div>
      <div class="info">
        <a class="site">Toomblr</a>

        <div class="timestamp">20/01/2017 - 11:43</div>
      </div>
    </div>

    <div class="content">
      Some content.
    </div>
  </div>
</body>

</html>

关于html - 为什么我的图像将我的 <td> 推到右边那么远?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41766225/

相关文章:

html - 图片在响应式设计中没有正确对齐

javascript - 在鼠标悬停和鼠标移出时显示隐藏 div

css - 你能在背景图片上叠加透明的 css3 渐变吗?

php - 如何更改 WooCommerce 上产品的外观?

javascript - 如何在断开连接的环境中使用 font-awesome?

html - Bootstrap 水平表单,在文本框上附加输入

javascript - 如何访问(通过javascript) Angular Material 中的当前主题调色板颜色?

html - 使用伪元素在特定类之后添加 clear both

c# - 如何通过单击链接滚动到页面中的某个位置

javascript - 粘性 header 仅适用于桌面设备,不适用于移动设备