I have been trying to send emails using html layout but there are lots of html elements and styles that email clients don't support.
So I am working on this library to convert almost html layout to email-compatible table layout.
Note: I don't guarantee that this library can convert any html element to your desired layout all the time. Feel free to tune the result as you want.
Choose one of the following methods.
- NPM
npm i html-to-table
- Github ESM Module
import HTML2Table from 'https://github.com/w99910/html-to-table/blob/master/dist/html-to-table.js'
- Github CJS
const HTML2Table = require('https://github.com/w99910/html-to-table/blob/master/dist/html-to-table.cjs')It is pretty straight-forward to use.
- Initialise the class
let html2table = new HTML2Table();- (Optional) - You can filter the elements you don't want to include in the output using Regex Expression
This checks the
classname andid.
html2table.excludeElementByPattern('toolbar')- Convert the element you want by calling
convertand passing the element as parameter.
html2table.convert(document.querySelector('your-element-to-convert'));- I suggest you target mobile device layout first so that the layout will work both on mobile and desktop.
Base64image data does not work. So host the image on cloud and use it.- Don't use
linear-gradientcss function in inline css. Instead, create the gradient as an image and load it usingurl.style="background: url('link-to-your-image')"
- Use
background-coloras a fall-back background color if image would not be working in some email clients. - Skip using
heightproperty if possible since it can prevent uninteneded padding or margin.
- Converting
remdoesn't render exactly as it is. - even if only
border-topis provided, all border is being rendered. - Change the logic for
v-alignas it is giving unnecessary vertical space.
- 0.1.4
- Drop setting
heightcss property for the parent. - Remove
v-alignin the output. Please tweak the output as you want. Heightproperty is converted toautofor child nodes unless you unexplictly set it as inlined css.- Append the bounding rect width as the
widthstyle when tranversing the child nodes. - Consider the styles of the embedding classes before get the window's computed styles.
- Drop setting
- 0.1.2
- Add
bgcolorattribute for fall-backbackgroundcss style. alignandvalignof table now consider the css properties of parent element. if there is no parent element, default value will be used.- Deprecated feature that convert SVG to Base64 PNG.
- Add
- 0.1.1
- Add
altandtitleto allowed attribute when it is cloned. - Fix
widthbeing0in some cases.
- Add
- 0.1.0 - Rewrite the logic by using bounding client rect to determine the layout
