Skip to content

Instantly share code, notes, and snippets.

@blue2blond

blue2blond/critical-all.css Secret

Last active Jul 28, 2020
Embed
What would you like to do?
Magento 1 critical CSS

demo.php contains a code snippet that should be inserted into head.phtml to make it work within Magento 1. Don't forget to change the demo file_get_contents call by the actual $this->getCssJsHtml() call and set the right path as $skin_url.

CSS is for demo purposes only, use your own determined critical CSS by following my guide: CSS critical path in Magento 1.

Need help? Reach out via LinkedIn or my contactform

.no-critical-pos,
.no-critical {
opacity: 0!important;
transition: opacity 0.1s ease-in;
}
.no-critical-pos {
position: absolute!important;
}
* {
outline: none!important;
}
*, ::after, ::before {
box-sizing: inherit;
}
.clearer:after {
display: block;
content: ".";
clear: both;
font-size: 0;
line-height: 0;
height: 0;
overflow: hidden;
}
html {
-webkit-text-size-adjust: 100%;
-ms-text-size-adjust: 100%;
text-size-adjust: 100%;
}
html {
box-sizing: border-box;
-ms-overflow-style: scrollbar;
}
html, body {
background-color: #fff;
font: 13px/1 'Roboto', sans-serif;
color: #2d2d2d;
text-align: center;
margin: 0;
}
a {
color: #2d2d2d;
text-decoration: none;
}
.fa {
min-width: 10px;
display: inline-block;
font-style: normal;
font-weight: normal;
line-height: 1;
}
.wrapper {
text-align: left;
}
.page {
padding-top: 150px;
position: relative;
}
/* TOP LINKS */
.header-container.type3.header-newskin {
border-top-width: 3px;
border-top-style: solid;
border-color: #131a22;
margin-bottom: 50px;
background-color: #ffffff;
}
.header-container.type3.header-newskin {
position: fixed;
width: 100%;
z-index: 99;
top: 0;
}
.top-links-container {
background-color: #f0f0ed;
padding: 3px 0;
position: relative;
z-index: 200;
}
.header-container.type3 .top-links-container {
background-color: #fff;
min-height: 33px;
}
.header-container.type3.header-newskin .top-links-container {
border-bottom: #eeeeee solid 2px;
padding: 3px 0;
}
.container {
width: 100%;
padding-right: 15px;
padding-left: 15px;
margin-right: auto;
margin-left: auto;
}
@media (max-width: 1024px) {
.container {
max-width: 100%;
}
}
.header-container .form-language,
.header-container .form-currency {
display: block;
margin-right: 8px;
}
.form-language.top-select {
margin-top: 7px;
}
.form-language.top-select a {
color: #000;
font-weight: bold;
}
/* HEADER */
.header {
position: relative;
z-index: 101;
text-align: left;
}
.header-container.type3.header-newskin .header {
text-align: center;
}
@media (max-width: 767px) {
.header {
width: 100%;
}
}
.header>.logo {
display: inline-block;
}
.header-container.type3.header-newskin .header>.logo {
margin: 15px 0;
}
.header .logo strong {
position: absolute;
top: -999em;
left: -999em;
}
.header-container.type3.header-newskin .header .logo img {
height: 50px;
margin-top: -10px;
}
.header-container.type3.header-newskin .search-area {
position: absolute;
}
@media (max-width: 991px) {
.header-container.type3.header-newskin .search-area {
left: 0;
z-index: 2;
}
}
@media (max-width: 1024px) {
.header-container.type3.header-newskin .search-area {
margin: 10px 10px;
width: 94%;
top: 40px;
}
}
.search-icon {
display: none !important;
}
.header-container.type3.header-newskin .header #search_mini_form {
width: auto;
margin: 0;
}
.header .form-search {
border-radius: 20px;
border: 1px solid #ccc;
}
.header .form-search {
border-radius: 20px !important;
background-color: #eee9e6;
border-color: #ffffff;
}
@media (max-width: 991px) {
.header-container.type3.header-newskin .header .form-search {
position: absolute;
top: 0;
overflow: visible;
border-width: 5px;
width: 100%;
left: 0;
right: auto;
}
}
/* HEADER WRAPPER */
@media (max-width: 991px) {
.header-container .header-wrapper {
display: none;
}
}
.header .form-search #search {
border: 0;
height: 38px;
background-color: transparent;
padding: 10px 15px;
color: #999;
width: 100%;
margin: 0;
font-size: 13px;
line-height: 20px;
border-radius: 20px 0 0 20px;
}
.header-container.type3.header-newskin .header .form-search #search {
height: 40px;
}
.mobile-nav.side-block {
position: fixed;
top: 0;
left: -250px;
width: 250px;
}
.header .form-search button.button {
position: absolute;
border: 0;
background-color: transparent;
}
.header .form-search label,
.breadcrumbs {
display: none;
}
.product-image-carousel:not(.owl-loaded) {
display: block!important;
}
.product-image-carousel:not(.owl-loaded) .item {
display: none;
}
/* .owl-dots */
.product-image-carousel:not(.owl-loaded) .item {
padding-bottom: 13px;
}
.product-image-carousel .item:after {
content: '';
display: block;
width: 100%;
height: 100%;
position: absolute;
z-index: 1;
box-shadow: inset 20px 0 100px rgba(0,0,0,0.03);
background-color: rgba(0,0,0,0.02);
left: 0;
top: 0;
}
.product-image-carousel:not(.owl-loaded) .item:first-child {
display: block;
padding-top: 100%;
position: relative;
width: 100%;
background-color: rgba(0,0,0,0.02);
}
.product-image-carousel:not(.owl-loaded) .item img {
position: absolute;
display: block;
width: 100%;
top: 0;
}
#loading-mask {
position: fixed;
}
.product-name {
margin: 10px 0;
font-size: 1em;
font-weight: 400;
}
.product-view .product-shop > div {
text-align: left;
}
@media (max-width: 1024px) {
.product-view .product-name h1 {
font-size: 18px;
font-weight: 600;
letter-spacing: -0.01em;
color: #21293c;
margin: 0 0 25px;
line-height: 20px;
}
}
.ratings {
display: none;
}
table {
background-color: transparent;
}
table {
border: 0;
border-spacing: 0;
empty-cells: show;
font-size: 100%;
}
caption, th, td {
vertical-align: middle;
text-align: left;
font-weight: 400;
}
label {
margin: 0;
vertical-align: middle;
color: #777;
font-size: 13px;
font-weight: 400;
}
label {
color: #2d2d2d;
}
.data-table {
width: 100%;
border: 0;
}
.data-table.grouped-items-table {
margin-bottom: 20px;
border: 1px solid #dcdcdc;
}
.data-table tbody tr {
border-left: 1px solid #dcdcdc;
border-right: 1px solid #dcdcdc;
background-color: #fff;
}
.product-type-grouped .data-table tbody th,
.data-table tbody td {
border-bottom: 0px solid #dcdcdc;
border-right: 0px solid #dcdcdc;
padding: 2px 10px;
line-height: 1.3;
}
.data-table.grouped-items-table th,
.data-table.grouped-items-table td {
padding: 5px 0px;
}
.qty-holder {
display: inline-block;
vertical-align: middle;
margin-right: 7px;
width: 30px;
position: relative;
text-align: center;
}
.data-table .qty-holder,
.data-table .add-to-cart-alt {
position: relative;
text-align: left;
margin-right: 0;
}
input.input-text, select, textarea {
background: #fff;
border: 1px solid #ccc;
}
input.qty {
margin-right: 1px;
margin-left: 1px;
display: inline-block;
vertical-align: middle;
height: 43px;
font-size: 14px;
text-align: center;
color: #08c;
border-color: #e1e1e1;
}
.data-table input.qty {
color: #777;
width: 50px;
border-radius: 0;
border-color: #ddd;
font-size: 16px;
}
.price-box {
min-height: 19px;
}
.product-view .price-box {
margin: 0 !important;
}
.product-view .product-shop .price-box {
float: right;
}
.product-view .product-shop .price-box .regular-price .price {
font-size: 18px;
font-weight: 700;
font-family: sans-serif;
color: #c30524;
}
.lead-time-message .notice {
color: #74b436;
}
.notice {
color: #e26703;
}
<?php
function arrayToAttributeString( array $attr )
{
$string = null;
foreach ( $attr as $k => $v ) {
$string .= ' ' . $k . ( empty($v) ? null: '="' . $v . '"' );
}
return trim($string);
}
function doCriticalCss( $headHtml, array $criticalFiles=array() )
{
if ( empty($criticalFiles) ) {
return $headHtml;
}
$_noscript = array();
$_loop = array(
'link' => array('rel'=>'', 'type'=>'', 'href'=>'', 'media'=>''),
'script' => array('src'=>'')
);
$_attributes = array('rel'=>'', 'type'=>'', 'href'=>'', 'media'=>'', 'async'=>'', 'defer'=>'');
$_allowedPreloads = array('script','image','font','style');
// determine stylesheets and scripts within our $headHtml string
preg_match_all('|(<([a-z]+)[^>]+>)|', $headHtml, $matches);
foreach( $matches[1] as $j => $elm )
{
$tag = $matches[2][$j];
if ( !isset( $_loop[ $tag ] ) ) {
continue;
}
$_attributes = $attrs = $_loop[ $tag ];
if ( !empty($_attributes) ) {
preg_match_all('/(' . implode('|', array_keys( $_attributes ) ) . ')=("[^"]*")/i', $elm, $list, PREG_SET_ORDER);
foreach ( $list as $opts ) {
$attrs[ $opts[1] ] = trim($opts[2],'"');
}
}
// only adjust/mutate links if non-printable stylesheet (so, no canonical)
if ( $tag == 'link' && $attrs['rel'] == 'stylesheet' && $attrs['media'] !== 'print' ) {
$media = null;
if ( strpos($attrs['media'],'(') !== false ) {
$media = $attrs['media'];
}
$attrs['media'] = 'print';
$attrs['onload'] = 'this.media=\'' . ( $media ? $media: 'all' ) . '\';this.onload=null';
// add the stylesheet to noscript, as we wan't to fall back to direct/render blocking styling when JS has been disabled by visitors
$_noscript[] = $elm;
// and change original link-element to a lazyloading (onload) stylesheet-link
$attrs = array_filter( $attrs );
$newStylesheet = '<link ' . arrayToAttributeString( $attrs ) . ' />';
$headHtml = str_replace( $elm, $newStylesheet, $headHtml );
}
else if ( $tag == 'script' && !empty($attrs['src']) ) {
// add defer to script
$attrs['defer'] = '';
$newScript = '<script ' . arrayToAttributeString( $attrs ) . '>';
$headHtml = str_replace( $elm, $newScript, $headHtml );
}
}
// echo critical css and noscript
echo '
<style type="text/css">' . str_replace( array("\n","\r","\t"), null, implode( null, array_map('file_get_contents', $criticalFiles ) ) ) . '</style>
<noscript>' . implode( null, $_noscript) . '</noscript>';
// return adjusted $headHtml
return $headHtml;
}
$headHtml = file_get_contents('getCssJsHtml.txt'); // Magento 1 function call: $this->getCssJsHtml();
$criticalCss = array();
$criticalCss[] = 'critical-all.css';
$criticalCss[] = 'critical-pdp.css';
/*
// decide when to do critical magic in Magento 1
if ( Mage::registry('current_product') ) {
// if on product page
$criticalCss[] = $skin_url.'critical-general.css';
$criticalCss[] = $skin_url.'critical-product.css';
}
else if ( Mage::registry('current_category') ) {
// if on category page
$criticalCss[] = $skin_url.'critical-general.css';
$criticalCss[] = $skin_url.'critical-category.css';
}
else {
// on other pages, such as text-only or homepage, do nothing special
}
*/
// inline critical css and adjust $headHtml accordingly
$headHtml = doCriticalCss( $headHtml, $criticalCss );
// business as usual:
echo $headHtml;
?>
<link rel="stylesheet" type="text/css" href="/media/css_secure/b96948b8dd0ef4b7b53a3e3e231a3824.css" />
<link rel="stylesheet" media="(min-width:500px;)" type="text/css" href="/media/css_secure/42b7842a9e58e6b85ee88eb738e39602.css" />
<link rel="canonical" href="..."/>
<!--[if gt IE 7]>
<link rel="stylesheet" type="text/css" href="/media/css_secure/49b5e70767b8445de9741aa24f9b76b7.css"media="all" />
<![endif]-->
<script src="/media/js_secure/main.js"></script>
<script defer src="/media/js_secure/defer.js"></script>
@seansan

This comment has been minimized.

Copy link

@seansan seansan commented Jul 28, 2020

$skin_url = Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_SKIN);

Should work for all

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment