Much like the other CSS preprocessors, CSS Crush offers a range of features designed to make your life easier. Although these including functions that let you do things like manipulate the opacity of a colour value, or generate a data uri from a relative file path, I’m going to talk about some of the other features that I have experience with.
The first, and possibly most important of these, is vendor prefixing. There has been a lot of talk in recent months about non-webkit browsers adopting the -webkit prefix, which has been spurred on by the fact that, to be honest, vendor prefixes are a pain in the arse.
CSS Crush helps solve issues like this by automatically adding all relevant prefixes to the properties that support them, meaning that this:
div { background: #fff linear-gradient( left, red, white ) no-repeat; }
will automatically be converted into this:
div { background: #fff -webkit-linear-gradient( left, red, white ) no-repeat; background: #fff -moz-linear-gradient( left, red, white ) no-repeat; background: #fff -ms-linear-gradient( left, red, white ) no-repeat; background: #fff -o-linear-gradient( left, red, white ) no-repeat; background: #fff linear-gradient( left, red, white ) no-repeat; }
This is something that (so far as I’m aware) none of the other CSS preprocessors offer out of the box – although some creative use of mixins does offer something similar – but by using CSS Crush as a git submodule, you’ll always have access to the latest prefixes available.
Another helpful feature is that of being able to define and use variables. This allows for uniformity across all your stylesheets, meaning you can define variables such as:
@define { /* fonts */ title: "Times New Roman", Georgia, serif; body: Verdana, Arial, Helvetica, sans-serif; /* colours */ primary-rgb: 89,168,15; primary: rgb($(primary-rgb)); primary-text: #FFFFFF; secondary-rgb: 158,213,76; secondary: rgb($(secondary-rgb)); secondary-text: #FFFFFF; background-rgb: 240,242,221; background: rgb($(background-rgb)); background-text: #222222; }
then if you need to change a value at a later date, rather than having to do a find/replace, and risk missing some, you simply update the value is one place, and CSS Crush does the rest for you.
One of the things I found particularly useful was the ability to use @import
to manage different CSS files for responsive sites. A practical example of this would be something like:
@import url( "mobile.css" ); /* Mobile first */ @import url( "tablet.css" ) screen and (min-width: 768px); /* Tablet and higher */ @import url( "desktop.css" ) screen and (min-width: 992px); /* desktop and higher */
which not only imports the files directly into a single stylesheet, would also put the correct @media
tags around the imported code, and then performs minification on it all.
I’m actually using CSS Crush in my limeBase WordPress starter theme, so that could be a good way for you to get started, but if you wanted to create your own implementation, it couldn’t be simpler:
cssCrush
require_once 'cssCrush/CssCrush.php';
<link>
code with
<?php echo CssCrush::tag( TEMPLATEPATH.DIRECTORY_SEPARATOR.'style.css' ); ?>
Upon viewing your page, CSS Crush will create two new files: .csscrush and style.crush.css, and output the relevant <link>
code linking to the new style.crush.css file.
If, before further page views, the original style.css gets updated, CSS Crush will detect the change, and generate a new crushed file for you, otherwise it will continue to serve the cached file.
If you’re already happily using LESS or SASS, then I’m not sure there is much that CSS Crush can offer you. If, however, you’re more interested in a simple way of automatically adding vendor prefixes and performing minification on your CSS, then you can’t go far wrong with CSS Crush.
]]>As I want to use this blog as a means for sharing knowledge and information that other developers might find useful, allow me to present my first such post, designed to solve the problem with how to display images within WordPress posts and pages on responsive Web sites.
For anyone unfamiliar with thm, responsive Web sites are sites that adapt and change their layout to fit the size of the device they’re viewed on.
This site, for example, is a responsive Web site, and employs responsive techniques which enables it to switch between three different layouts:
If you’re using a desktop computer, try resizing your browser and you’ll see the site switch between the three layouts as the window gets smaller.
As I’m sure you can imagine, due to the fact this requires designing and building multiple layouts, building a responsive site is hard work, especially once you come up against the issue of trying to display the same content on an ever decreasing screen size.
Anyway… One issue that I encountered while building this site was that of images embedded into posts and pages.
I had already designed both the desktop and tablet layouts to have the same width content area, meaning I can use the alignment options offered by WordPress, and know exactly how they’re going to look on two of my three layouts.
But what about the mobile layout? This is a bit more difficult to handle as the width of the device is variable. There had to be a simple way of handing this issue?
The first thing I wanted was to remove any left or right alignments of images in the mobile layout, and instead force them onto their own line with a centre alignment. This is because on a screen that is potentially no more than 480 pixels wide, the last thing you want is a 300 pixel image sat to the right of the content you’re trying to read.
I also wanted to make sure that the images on the mobile layout are displayed at an intelligent size. For example, images that are smaller than the width of the browser should display at their native resolution, but images larger than the width of the browser should resize down to fit.
Finally, I needs to use Mobile First techniques to make the site as accessible as possible.
Below is the code that I’ve produced to solve these issues. It is based on the required WordPress Generated Classes CSS, but modifies it to allow for the behaviour described in the requirements above.
I’ve tested it as much as I can, and so far as I can tell, it works exactly as intended, but if you can find any problems with it, or can offer any suggestions on how to improve it, please let me know.
/* =================================== WordPress Images =================================== */ /* * Mobile First */ a img.aligncenter { display: block; margin-left: auto; margin-right: auto; } .wp-caption, figure { background: #fff; border: 1px solid #f0f0f0; max-width: 96%; text-align: center; padding: 5px 5px 10px; } .wp-caption p.wp-caption-text, figcaption { font-size: 11px; line-height: 17px; margin: 0; padding: 10px 4px 0; } img.alignright, img.alignleft, img.aligncenter { float: none; max-width: 100%; height: auto; margin: 1em auto; display: block; } .wp-caption img, .wp-caption.alignright img, .wp-caption.alignleft img, .wp-caption.aligncenter img, figure img, figure.alignright img, figure.alignleft img, figure.aligncenter img { margin: 0 auto; max-width: 100%; height: auto; } .wp-caption.alignright, .wp-caption.alignleft, .wp-caption.aligncenter, figure.alignright, figure.alignleft, figure.aligncenter { float: none; max-width: 100% !important; height: auto; margin: 0 auto; } /* * Tablet and up */ @media only screen and (min-width: 768px) { .wp-caption.alignright, figure.alignright { margin: 5px 0 20px 20px; } .wp-caption img, figure img { border: 0 none; height: auto; max-width: 98.5%; width: auto; margin: 0; padding: 0; } .aligncenter, div.aligncenter, .aligncenter, figure.aligncenter { display: block; margin: 5px auto; } .alignright, a img.alignright { float: right !important; margin: 5px 0 17px 17px !important; } .alignleft, a img.alignleft { float: left !important; margin: 5px 17px 17px 0 !important; } }
You may have noticed the above code featured the new HTML5 figure
and figcaption
tags. Although WordPress doesn’t automatically support the figure
tag, it is possible to have WordPress use figure in place of its own caption solution by adding the following to your theme’s functions.php file (which was adapted from code in this article):
add_shortcode('wp_caption', 'twentyten_img_caption_shortcode'); add_shortcode('caption', 'twentyten_img_caption_shortcode'); function twentyten_img_caption_shortcode($attr, $content = null) { $options = array( 'id' => '', 'align' => 'alignnone', 'width' => '', 'caption' => '' ); extract(shortcode_atts($options, $attr)); if (1 > (int) $width || empty($caption)) { return $content; } if ($id) { $idtag = 'id="' . esc_attr($id) . '" '; } return '<figure ' . $idtag . 'aria-describedby="figcaption_' . $id . '" style="width: ' . (10 + (int) $width) . 'px" class="'.$align.'">'. do_shortcode($content) . '<figcaption id="figcaption_' . $id . '">' . $caption . '</figcaption></figure>'; }]]>