html2canvas icon indicating copy to clipboard operation
html2canvas copied to clipboard

Extra padding when set img display:block styles

Open dohooo opened this issue 3 years ago • 8 comments
trafficstars

Please make sure you are testing with the latest release of html2canvas. Old versions are not supported and issues reported for them will be closed.

Please follow the general troubleshooting steps first:

  • [x] You are using the latest version
  • [x] You are testing using the non-minified version of html2canvas and checked any potential issues reported in the console

Bug reports:

Extra padding when i set img display:block styles.

image

jsfiddle.

Specifications:

  • html2canvas version tested with: 1.4.1
  • Browser & version: any
  • Operating system: any

dohooo avatar Feb 14 '22 14:02 dohooo

I hit this issue as well - I've got a Pull Request in for a fix, but you may be able to work around it by adding this CSS to your page stylesheet:

    body > div:last-child > span + img {
      display: inline !important;
    }

In brief, the library measures text size information by appending a span containing the text with all the CSS applied and then a blank image, and then measuring how much the text displaces the image to determine the height of the text. The <img> element that gets appended doesn't set its own display property, so if you're setting the display to block for every image in the document it will add padding to the test image and incorrectly measure the height of the text, which results in the extra padding in the output.

The workaround is a little flimsy since it may cause unintended issues if you have images you're cloning that also match that CSS selector, but it works correctly in your jsfiddle.

mccahan avatar Feb 18 '22 02:02 mccahan

I hit this issue as well - I've got a Pull Request in for a fix, but you may be able to work around it by adding this CSS to your page stylesheet:

    body > div:last-child > span + img {
      display: inline !important;
    }

In brief, the library measures text size information by appending a span containing the text with all the CSS applied and then a blank image, and then measuring how much the text displaces the image to determine the height of the text. The <img> element that gets appended doesn't set its own display property, so if you're setting the display to block for every image in the document it will add padding to the test image and incorrectly measure the height of the text, which results in the extra padding in the output.

The workaround is a little flimsy since it may cause unintended issues if you have images you're cloning that also match that CSS selector, but it works correctly in your jsfiddle.

uh, yes in fact I found a work around that similar to yours.

img {
    display: initial !important;
}

I think it’s safer in any browsers.

dohooo avatar Feb 18 '22 03:02 dohooo

I hit this issue as well - I've got a Pull Request in for a fix, but you may be able to work around it by adding this CSS to your page stylesheet:

    body > div:last-child > span + img {
      display: inline !important;
    }

In brief, the library measures text size information by appending a span containing the text with all the CSS applied and then a blank image, and then measuring how much the text displaces the image to determine the height of the text. The <img> element that gets appended doesn't set its own display property, so if you're setting the display to block for every image in the document it will add padding to the test image and incorrectly measure the height of the text, which results in the extra padding in the output.

The workaround is a little flimsy since it may cause unintended issues if you have images you're cloning that also match that CSS selector, but it works correctly in your jsfiddle.

they should merge your pull request already

XiDanko avatar Aug 02 '22 20:08 XiDanko

I hit this issue as well - I've got a Pull Request in for a fix, but you may be able to work around it by adding this CSS to your page stylesheet:

    body > div:last-child > span + img {
      display: inline !important;
    }

In brief, the library measures text size information by appending a span containing the text with all the CSS applied and then a blank image, and then measuring how much the text displaces the image to determine the height of the text. The <img> element that gets appended doesn't set its own display property, so if you're setting the display to block for every image in the document it will add padding to the test image and incorrectly measure the height of the text, which results in the extra padding in the output.

The workaround is a little flimsy since it may cause unintended issues if you have images you're cloning that also match that CSS selector, but it works correctly in your jsfiddle.

Legend! You have no idea how many hours I spent, how many solutions that I tried and I also tried out basically every single one of those export image packages in the market, and each of them has some other issues that I just ended up come back to html2canvas.

ASCSam avatar Mar 01 '23 09:03 ASCSam

It would be very helpful if this could be addressed within the program(html2canvas).

arayutw avatar Mar 24 '23 13:03 arayutw

I hit this issue as well - I've got a Pull Request in for a fix, but you may be able to work around it by adding this CSS to your page stylesheet:

    body > div:last-child > span + img {
      display: inline !important;
    }

In brief, the library measures text size information by appending a span containing the text with all the CSS applied and then a blank image, and then measuring how much the text displaces the image to determine the height of the text. The <img> element that gets appended doesn't set its own display property, so if you're setting the display to block for every image in the document it will add padding to the test image and incorrectly measure the height of the text, which results in the extra padding in the output.

The workaround is a little flimsy since it may cause unintended issues if you have images you're cloning that also match that CSS selector, but it works correctly in your jsfiddle.

Legend!!!

Dude, you saved my life. Someone has to accept your pr ASAP

Edu-hs avatar Dec 29 '23 20:12 Edu-hs