Fit image to table cell [Pure HTML]

后端 未结 4 1386
一整个雨季
一整个雨季 2020-12-17 10:18

How do i fit image to table td cell? [Pure HTML]

I have tried below code to fit an image in table td cell.

HTML code is here :

相关标签:
4条回答
  • 2020-12-17 10:20

    Use your developer's tool of choice and check if the tr, td or img has any padding or margins.

    0 讨论(0)
  • 2020-12-17 10:28

    It's all about display: block :)

    Updated:

    Ok so you have the table, tr and td tags:

    <table>
      <tr>
        <td>
          <!-- your image goes here -->
        </td>
      </tr>
    </table>
    

    Lets say your table or td (whatever define your width) has property width: 360px;. Now, when you try to replace the html comment with the actual image and set that image property for example width: 100%; which should fully fill out the td cell you will face the problem.

    The problem is that your table cell (td) isn't properly filled with the image. You'll notice the space at the bottom of the cell which your image doesn't cover (it's like 5px of padding).

    How to solve this in a simpliest way?

    You are working with the tables, right? You just need to add the display property to your image so that it has the following:

    img {
      width: 100%;
      display: block;
    }
    
    0 讨论(0)
  • 2020-12-17 10:34

    Inline content leaves space at the bottom for characters that descend (j, y, q):

    https://developer.mozilla.org/en-US/docs/Images,_Tables,_and_Mysterious_Gaps

    There are a couple fixes:

    Use display: block;

    <img style="display:block;" width="100%" height="100%" src="http://dummyimage.com/68x68/000/fff" />
    

    or use vertical-align: bottom;

    <img style="vertical-align: bottom;" width="100%" height="100%" src="http://dummyimage.com/68x68/000/fff" />
    
    0 讨论(0)
  • 2020-12-17 10:44

    if you want to do it with pure HTML solution ,you can delete the border in the table if you want...or you can add align="center" attribute to your img tag like this:

    <img align="center" width="100%" height="100%" src="http://dummyimage.com/68x68/000/fff" />
    

    see the fiddle : http://jsfiddle.net/Lk2Rh/27/

    but still it better to handling this with CSS, i suggest you that.

    • I hope this help.
    0 讨论(0)
提交回复
热议问题