代码之家  ›  专栏  ›  技术社区  ›  James Skidmore

简单的HTML看起来很糟糕?

  •  2
  • James Skidmore  · 技术社区  · 16 年前

    我无论如何也弄不明白为什么我的桌子会变成这样。图像和文本应排列整齐:

    alt text

    HTML:

    <div id="items">
      <div class="category"><h1>Apparel</h1></div>
      <div class="list-product">
      <table border="0" cellpadding="0" cellspacing="0" width="100%">
        <tr>
          <td width="50" align="center" style="padding-right: 5px;">
            <img height="30" src="/images/products/none.jpg" />
          </td>
          <td>
            <img style="padding: 0 3px 0 0;" src="/images/icons/flag_red.png" />
          </td>
          <td valign="middle">
            <strong>Test Product</strong><br />
            <span style="color: #777;">This has a short description.</span>
          </td>
        </tr>
      </table>
      </div>
      ... and the other table here
    </div>
    

    div#items
    {
    width: 400px;
    }
    
    div.category
    {
    padding: 7px 13px;
    background: #f0f0f0;
    color: #101010;
    font-size: 20px;
    border-bottom: 1px solid #d0d0d0;
    }
    
    .list-product
    {
    border-bottom: 1px solid #ddd;
    font-size: 12px;
    padding: 10px;
    }
    
    6 回复  |  直到 7 年前
        1
  •  4
  •   David Thomas    16 年前

    为了补充Pete的答案,并使用CSS而不是内联表示标记,您可能想尝试添加 vertical-align: middle 为表格单元格添加css。

    line-height: 100px line-height: 50px vertical-align

        2
  •  1
  •   Pete    16 年前

        3
  •  1
  •   abramN    16 年前

    试着在表格中临时添加一个边框,这样你就可以看到图像和文本在各自的td中对齐的位置。此外,您是否需要为描述文本使用span?

        4
  •  1
  •   DreadPirateShawn    16 年前

    什么浏览器?我复制粘贴了你的HTML(添加了HTML和body标签),复制粘贴了CSS(添加了head和style标签),垂直对齐是准确的——与图片中的问题不匹配。

    尝试为各种元素添加边框,看看这是否能揭示疯狂之处。确保你的代码和粘贴在这里的代码之间没有拼写错误。

        5
  •  1
  •   Nosredna    16 年前

    我不确定这是否重要,但我会从使用break和span切换到使用标题和段落标签,然后对它们进行样式设置。

      <td valign="middle">
        <h2>Test Product</h2>
        <p>This has a short description.</p>
      </td>
    
        6
  •  0
  •   JasonS    16 年前

    为您的表格和每个td标签使用固定宽度。将表的宽度设置为100%,并在第一个标记上设置固定宽度。第二和第三td标签根据td标签内的内容进行调整,以填充表格标签上的100%宽度。