++ed by:

3 PAUSE users
1 non-PAUSE user.

Kevin Hatim Kamel
and 1 contributors


CSS::Inliner - Library for converting CSS <style> blocks to inline styles.


use Inliner;

my $inliner = new Inliner();

$inliner->read_file({ filename => 'myfile.html' });

print $inliner->inlinify();


Library for converting CSS style blocks into inline styles in an HTML document. Specifically this is intended for the ease of generating HTML emails. This is useful as even in 2013 Gmail and Hotmail don't support top level <style> declarations.



Instantiates the Inliner object. Sets up class variables that are used during file parsing/processing. Possible options are:

html_tree - (optional) Pass in a fresh unparsed instance of HTML::Treebuilder

NOTE: Any passed references to HTML::TreeBuilder will be substantially altered by passing it in here...

strip_attrs - (optional) Remove all "id" and "class" attributes during inlining

leave_style - (optional) Leave style/link tags alone within <head> during inlining

relaxed - (optional) Relaxed HTML parsing which will attempt to interpret non-HTML4 documents.

NOTE: This argument is not compatible with passing an html_tree.


Fetches a remote HTML file that supposedly contains both HTML and a style declaration, properly tags the data with the proper characterset as provided by the remote webserver (if any). Subsequently calls the read method automatically.

This method expands all relative urls, as well as fully expands the stylesheet reference within the document.

This method requires you to pass in a params hash that contains a url argument for the requested document. For example:

$self->fetch_file({ url => 'http://www.example.com' });


Opens and reads an HTML file that supposedly contains both HTML and a style declaration. It subsequently calls the read() method automatically.

This method requires you to pass in a params hash that contains a filename argument. For example:

$self->read_file({ filename => 'myfile.html' });

Additionally you can specify the character encoding within the file, for example:

$self->read_file({ filename => 'myfile.html', charset => 'utf8' });


Reads passed html data and parses it. The intermediate data is stored in class variables.

The <style> block is ripped out of the html here, and stored separately. Class/ID/Names used in the markup are left alone.

This method requires you to pass in a params hash that contains scalar html data. For example:

$self->read({ html => $html });

NOTE: You are required to pass a properly encoded perl reference to the html data. This method does *not* do the dirty work of encoding the html as utf8 - do that before calling this method.


Processes the html data that was entered through either 'read' or 'read_file', returns a scalar that contains a composite chunk of html that has inline styles instead of a top level <style> declaration.


Given a particular selector return back the applicable styles


Given a particular selector return back the associated selectivity


Return back any warnings thrown while inlining a given block of content.

Note: content warnings are initialized at inlining time, not at read time. In order to receive back content feedback you must perform inlinify first


This code has been developed under sponsorship of MailerMailer LLC, http://www.mailermailer.com/


Kevin Kamel <kamelkev@mailermailer.com>


Vivek Khera <vivek@khera.org>, Michael Peters <wonko@cpan.org>


This module is Copyright 2013 Khera Communications, Inc. It is licensed under the same terms as Perl itself.