【问题标题】:Rowspan Images of Variable Height so Text is Always Evenly Spaced可变高度的行跨度图像,因此文本始终均匀分布
【发布时间】:2018-10-22 17:58:15
【问题描述】:

我正在为照片库创建一个显示页面。该页面将有 4 个照片属性 - 文件名、标题、拍摄日期和摄影师姓名,以及照片本身的预览缩略图。我试图实现一致的外观,但不幸的是图像有横向和纵向,所以虽然横向图像看起来一致,但纵向图像会拉长属性行之间的距离,因此会弄乱外观。

我的示例代码如下,JSFiddle 上的代码位于此处 - https://jsfiddle.net/tsmolskow/37hkszbq/61/

HTML 代码:

<style>

.right-Padding{
   padding-right: 10px; 
}
img.resize {
   width: 50%;
   height: 50%;
   max-height: 125px;
}

</style>
<table style='font-size:15px'>

<tr><td font-size='40px'>Image Name</td>
<td rowspan='5'><a href='" + href + "'><img class='resize' align='middle' src='https://p1.liveauctioneers.com/3283/129454/65998802_1_x.jpg?version=1540061833&format=pjpg&auto=webp&quality=50'></a></td></tr>
<tr><td width='50%' class="right-Padding">Image Title</td></tr>
<tr><td>Image Date Taken</td></tr>
<tr><td class="right-Padding">Image Taken By</td></tr>
<tr><td height='100%'></td></tr>

<tr><td font-size='40px'>Image Name</td>
<td rowspan='6'><a href='" + href + "'><img class='resize' align='middle' src='https://i.pinimg.com/474x/09/f6/83/09f683f4fde5a1e70c785ad1dedca58b--silver-belt-buckles-silver-belts.jpg'></a></td></tr>
<tr><td width='50%' class="right-Padding">Image Tile</td></tr>
<tr><td>Image Date Taken</td></tr>
<tr><td class="right-Padding">Image Taken By</td></tr>
<tr><td height='100%'></td></tr>

</table>

CSS 代码:

.right-Padding{
 padding-right: 10px; 
}
img.resize {
    width: 50%;
    height: 50%;
    max-height: 125px;
}

【问题讨论】:

  • 在 Google 上搜索“css 图像最大宽度比例”。解决方案将根据您的罚款设置最大值,而不是依赖于您发现的会扭曲的固定宽度。此外,您需要考虑将 CSS 与 HTML 分开,因为它更易于使用且更易于维护。
  • @VanquiishedWombat - 你的代码很好,只是它没有给我一个一致的外观。在第一张图片 (Kachina) 上,文本行之间的间距比在第二张图片 (Kokepelli) 中大得多。

标签: css html dynamic-html


【解决方案1】:

首先,您的第一张图片的行跨度为 6,应该是 4。为您的图片设置最大高度,我将其设置为 90 像素。然后从带有图像的&lt;td&gt; 单元格中删除 25% 的宽度和高度声明。最后,你必须使用表格吗?如果你需要表格布局,你真的应该使用 CSS Grid。

table{
   width:100%;
}
td{
   border:solid 1px #000;
}
img{
   max-height:90px;
   
}
<table style='font-size:15px'>
  <tr>
    <td font-size='40px'>
      <a href='" + href + "'>Image Name</a>
    </td>
    <td rowspan='4'>
      <a href='" + href + "'>
        <img  align='middle' src='https://p1.liveauctioneers.com/3283/129454/65998802_1_x.jpg?version=1540061833&format=pjpg&auto=webp&quality=50'>
      </a>
    </td>
  </tr>
  <tr>
    <td width='50%' style='padding-right: 10px;'>Image Tile</td>
  </tr>
  <tr>
    <td>Image Date Taken</td>
  </tr>
  <tr>
    <td style='padding-right: 10px;'>Image Taken By</td>
  </tr>
  <tr>
    <td font-size='40px'>
      <a href='" + href + "'>Image Name</a>
    </td>
<td rowspan='6'><a href='" + href + "'><img  align='middle' src='https://i.pinimg.com/474x/09/f6/83/09f683f4fde5a1e70c785ad1dedca58b--silver-belt-buckles-silver-belts.jpg'></a></td></tr>
<tr><td width='50%' style='padding-right: 10px;'>Image Tile</td></tr>
<tr><td>Image Date Taken</td></tr>
<tr><td style='padding-right: 10px;'>Image Taken By</td></tr>

</table>

【讨论】:

  • Rowspan: 好球!我已经更改了几次代码,所以以前我有六行。我发现图像的最大高度会扭曲超过 100 像素的图像。
  • @TomMolskow 除非明确设置宽度,否则它不会扭曲它们。 width:auto 会解决这个问题。
  • 我会在实际代码中尝试,但在 SO 页面上,它似乎仍然有更大的页面空间
  • 没有办法完全避免这种情况,高大的形象永远是高大的形象。您可以删除每个部分中的最后一行空并将max-height 减少到 90px(见我编辑)。要么是图像比你想要的小,要么是其中一些对于行来说太高了。
  • 好的,那么max-height 没有帮助。如果我使用max-height,那么文本看起来不错,但较大的图像会失真。如果我不使用它,那么图像看起来不错,但我仍然有文字问题。有没有办法根据图像高度动态设置最后一行的&lt;td height='100%'&gt;
猜你喜欢
  • 2012-08-29
  • 1970-01-01
  • 1970-01-01
  • 2011-07-04
  • 1970-01-01
  • 2017-06-01
  • 2015-03-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多