PHP Classes

PHP CSS to Inline Styles: Convert CSS styles into HTML attribute values

Recommend this page to a friend!
     
  Info   Example   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
StarStarStarStar 69%Total: 380 All time: 6,733 This week: 89Up
Version License PHP version Categories
csstoinlinestyles 1.6.37BSD License5.3HTML, PHP 5, Parsers
Description 

Authors

Tijs Verkoyen
Lars Moelleken


Contributor

This package can convert CSS styles into HTML attribute values,

It can take a HTML document string and parses it to process CSS styles and apply them to the attributes of the document directly.

The class can optionally remove the original CSS in the HTML file or keep it, remove the original style attributes from the tags and exclude media queries from the original CSS.

Innovation Award
PHP Programming Innovation award nominee
February 2017
Number 11
When you want to send email messages formatted as HTML, usually the mail services will filter the CSS style sheets it may use, thus making the message be rendered in a different way than it was intended.

This package provides a solution for that. It consists in parsing the message HTML and apply the CSS stylesheets to the HTML attributes.

However, this may generate much larger HTML messages. Therefore it provides options to either remove the original CSS in the HTML file to make it small, as well remove the original style attributes from the tags and exclude media queries from the original CSS to minimize the final HTML size.

Manuel Lemos
Picture of Lars Moelleken
  Performance   Level  
Name: Lars Moelleken <contact>
Classes: 25 packages by
Country: Germany Germany
Age: 36
All time rank: 62140 in Germany Germany
Week rank: 44 Up2 in Germany Germany Up
Innovation award
Innovation award
Nominee: 11x

Winner: 1x

Example

<?php

require_once 'config.php';
require_once
__DIR__ . '/../vendor/autoload.php';

use
voku\CssToInlineStyles\CssToInlineStyles;

// create instance
$cssToInlineStyles = new CssToInlineStyles();

$html = file_get_contents(__DIR__ . '/examples/sumo/index.htm');
$css = file_get_contents(__DIR__ . '/examples/sumo/style.css');

$cssToInlineStyles->setHTML($html);
$cssToInlineStyles->setCSS($css);

// output
echo $cssToInlineStyles->convert();


Details

Build Status codecov.io Coverage Status Scrutinizer Code Quality Codacy Badge SensioLabsInsight Latest Stable Version Total Downloads Latest Unstable Version PHP 7 ready License

CssToInlineStyles class

WARNING: this is only a Extended-Fork of "https://github.com/tijsverkoyen/CssToInlineStyles"

> CssToInlineStyles is a class that enables you to convert HTML-pages/files into > HTML-pages/files with inline styles. This is very usefull when you're sending > emails.

About

PHP CssToInlineStyles is a class to convert HTML into HTML with inline styles.

Installation

The recommended installation way is through Composer.

$ composer require voku/css-to-inline-styles

Example

use voku\CssToInlineStyles\CssToInlineStyles;

// Convert HTML + CSS to HTML with inlined CSS
$cssToInlineStyles= new CssToInlineStyles();
$cssToInlineStyles->setHTML($html);
$cssToInlineStyles->setCSS($css);
$html = $cssToInlineStyles->convert();

// Or use inline-styles blocks from the HTML as CSS
$cssToInlineStyles = new CssToInlineStyles($html);
$cssToInlineStyles->setUseInlineStylesBlock(true);
$html = $cssToInlineStyles->convert();

// Or use linked files from the HTML as CSS
$cssToInlineStyles = new CssToInlineStyles($html);
$cssToInlineStyles->setLoadCSSFromHTML(true);
$html = $cssToInlineStyles->convert(false, 0, __DIR__ . '/../tests/');


Documentation

The following properties exists and have set methods available:

Property | Default | Description -------|---------|------------ cleanup|false|Should the generated HTML be cleaned? useInlineStylesBlock |false|Use inline-styles block as CSS. stripOriginalStyleTags |false|Strip original style tags. excludeMediaQueries |true|Exclude media queries from extra "css" and keep media queries for inline-styles blocks. excludeConditionalInlineStylesBlock |true|Exclude conditional inline-style blocks.

Warning

Also if the default is `cleanup === false`, you maybe need to use this feature, because Outlook has some special features where the inline-CSS will be ignored and it will fallback to use only the CSS-Class- or ID-properties.

$cssToInlineStyles->setCleanup(true);

Known issues

  • no support for pseudo selectors

  Files folder image Files (43)  
File Role Description
Files folder imageexample (2 files, 1 directory)
Files folder imagesrc (3 files)
Files folder imagetests (3 files, 1 directory)
Accessible without login Plain text file .editorconfig Data Auxiliary data
Accessible without login Plain text file .scrutinizer.yml Data Auxiliary data
Accessible without login Plain text file .styleci.yml Data Auxiliary data
Accessible without login Plain text file .travis.yml Data Auxiliary data
Accessible without login Plain text file CHANGELOG.md Data Auxiliary data
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file LICENSE.md Lic. License text
Accessible without login Plain text file phpcs.php_cs Example Example script
Accessible without login Plain text file phpstan.neon Data Auxiliary data
Accessible without login Plain text file phpunit.xml Data Auxiliary data
Accessible without login Plain text file README.md Doc. Documentation

  Files folder image Files (43)  /  example  
File Role Description
Files folder imageexamples (1 directory)
  Accessible without login Plain text file config.php Aux. Auxiliary script
  Accessible without login Plain text file index.php Example Example script

  Files folder image Files (43)  /  example  /  examples  
File Role Description
Files folder imagesumo (2 files)

  Files folder image Files (43)  /  example  /  examples  /  sumo  
File Role Description
  Accessible without login Plain text file index.htm Data Documentation
  Accessible without login Plain text file style.css Data Auxiliary data

  Files folder image Files (43)  /  src  
File Role Description
  Plain text file CssToInlineStyles.php Class Class source
  Plain text file Exception.php Class Class source
  Plain text file Specificity.php Class Class source

  Files folder image Files (43)  /  tests  
File Role Description
Files folder imagefixtures (22 files)
  Accessible without login Plain text file bootstrap.php Aux. Auxiliary script
  Plain text file CssToInlineStylesTest.php Class Class source
  Plain text file SpecificityTest.php Class Class source

  Files folder image Files (43)  /  tests  /  fixtures  
File Role Description
  Accessible without login Plain text file style.css Data Auxiliary data
  Accessible without login Plain text file style_big.css Data Auxiliary data
  Accessible without login HTML file test1Html.html Doc. Documentation
  Accessible without login HTML file test1Html_result.html Doc. Documentation
  Accessible without login Plain text file test1Latin.txt Doc. Documentation
  Accessible without login Plain text file test1Utf8.txt Doc. Documentation
  Accessible without login Plain text file test2Css.css Data Auxiliary data
  Accessible without login HTML file test2Html.html Doc. Documentation
  Accessible without login HTML file test2Html_result.html Doc. Documentation
  Accessible without login HTML file test3Html.html Doc. Documentation
  Accessible without login HTML file test3Html_result.html Doc. Documentation
  Accessible without login HTML file test4Html.html Doc. Documentation
  Accessible without login HTML file test4Html_result.html Doc. Documentation
  Accessible without login HTML file test5Html.html Doc. Documentation
  Accessible without login HTML file test5Html_result.html Doc. Documentation
  Accessible without login HTML file test6Html.html Doc. Documentation
  Accessible without login HTML file test6Html_result.html Doc. Documentation
  Accessible without login Plain text file test6Style.css Data Auxiliary data
  Accessible without login HTML file test7Html.html Doc. Documentation
  Accessible without login HTML file test7Html_result.html Doc. Documentation
  Accessible without login HTML file test_big.html Doc. Documentation
  Accessible without login HTML file test_big_result.html Doc. Documentation

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
Downloadcsstoinlinestyles-2019-04-26.zip 95KB
Downloadcsstoinlinestyles-2019-04-26.tar.gz 68KB
Install with ComposerInstall with Composer
Needed packages  
Class DownloadWhy it is needed Dependency
Simple HTML DOM Download .zip .tar.gz Dom-Manipulation Required
 Version Control Unique User Downloads Download Rankings  
 100%
Total:380
This week:0
All time:6,733
This week:89Up
User Ratings User Comments (1)
 All time
Utility:87%StarStarStarStarStar
Consistency:87%StarStarStarStarStar
Documentation:81%StarStarStarStarStar
Examples:87%StarStarStarStarStar
Tests:-
Videos:-
Overall:69%StarStarStarStar
Rank:339
 
Thats a very good class ;-)
6 years ago (José Filipe Lopes Santos)
80%StarStarStarStarStar