- Transform CSS into line style attributes with lxml.html

11 July 2009   9 comments   Python

Powered by Fusion×

By blogging about it I can pretty much guarantee that someone will comment and say "Hey, why didn't you use the pypi/alreadyexists package which does the same thing but better". I couldn't find one after a quick search and I felt the hacker mood creeping up on my begging me to (re)invent it. takes a HTML page, finds all CSS blocks and transforms these into style attributes. For example, from this:

    h1, h2 { color:red; }
    strong {

You get this:

    <h1 style="color:red">Hi!</h1>
    <p><strong style="text-decoration:none">Yes!</strong></p>

Why is this useful? When you're writing HTML emails. Like this newsletter app that I'm working on.

I just wrote it late yesterday and it needs lots of work to impress but for the moment it works for me. If I take the time to tidy it up properly I'll turn it into a package. Assuming there isn't one already :)


No available on and as a PyPi package


Two new copy-cats have been released:

Might be worth poking around at these if my premailer isn't good enough.


Walter Dörwald
XIST includes a function for doing this. An online version of the CSS inliner is available from
Marius Gedminas
I've been looking for something like this to use as a PyBlosxom plugin to inline styles in the RSS feed. And when I say "looking", I mean "posting a question and waiting for lazyweb to come up with an answer". So, thanks ;-)

What is the licence of your code?
Peter Bengtsson
No licence yet but I'm guessing something very forgiving. I've made a much improved version but haven't uploaded it yet.
I guess I'll stick it on pypi
Lionel Barret
funny I have done the same a few weeks ago. I'll compare with your module if there is anything I can add.
This seems to be exactly what am I looking for. I installed it, but how can I use it? Can you post some instructions or tell us where to find them?
Very nice, thank you.
Alex Hayes
I've created a django template tag that can be used with premailer - see
Peter Bengtsson
That is great!

Your email will never ever be published

Related posts

My first iPhone web app - Crosstips iPhone interface 01 July 2009
setuptools usability - not good, what can be done? 15 July 2009
Related by keywords:
mincss "Clears the junk out of your CSS" 21 January 2013
setAttribute('style', ...) workaround for IE 08 January 2007
The awesomest way possible to serve your static stuff in Django with Nginx 24 March 2010
COPYFILE_DISABLE and python distutils in python 2.6 12 April 2014
Premailer on Python 3 08 October 2014
premailer now excludes pseudo selectors by default 27 May 2013
mincss in action - sample report from the wild 22 January 2013
premailer now with 100% test coverage 22 August 2014
This site is now 100% inline CSS and no bytes are wasted 05 March 2013
XHTML, HTML and CSS compressor 07 April 2004
How to use premailer as a command line script 13 July 2012
premailer now honours specificity 21 March 2013