Lime Blast » CSS http://limeblast.co.uk The virtual home of Web developer Daniel Hollands, the place to be if you're looking for articles and tutorials (and rants) on all aspects of the World Wide Web. Wed, 14 Oct 2015 13:13:21 +0000 en-US hourly 1 http://wordpress.org/?v=3.9 CSS Crush /2012/04/css-crush/ /2012/04/css-crush/#comments Thu, 05 Apr 2012 14:00:57 +0000 /?p=312

Related posts:

  1. Facebook Timeline Photoshop TemplateSeeing as Facebook’s Timeline feature is making the move to Pages, I figured that now was a good time to share the Facebook Timeline Hack Template that I used to style up my Timeline Page. The download includes a Photoshop......
  2. Images in responsive WordPress Web sitesFor the past couple of weeks I’ve been learning how to build responsive Web sites, a process that is simple in principle, but complex in practice once you get down to the nitty-gritty. As I want to use this blog as......
  3. Running a server is hard workI was hoping, when I first had the idea for this post, that it would be a run-through of all the steps I has taken in setting up my dedicated server. It was going to include links to the resources......
]]>
Although there is a lot of love for CSS preprocessors such as LESS and SASS, both of which seem to work very well, I wanted to take a moment to talk about a slightly different alternative, the PHP-based CSS Crush.

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.

Vendor prefixing

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.

Variables

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.

Direct @import and minification

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.

Implementing CSS Crush in WordPress

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:

  1. Download/clone the files into your theme folder in a folder called cssCrush
  2. in your functions.php file, add the following line
    require_once 'cssCrush/CssCrush.php';
  3. in your header.php, replace your existing stylesheet <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.

Conclusion

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.

]]>
/2012/04/css-crush/feed/ 0
Images in responsive WordPress Web sites /2012/02/images-in-responsive-wordpress-web-sites/ /2012/02/images-in-responsive-wordpress-web-sites/#comments Tue, 28 Feb 2012 13:35:35 +0000 /?p=183

Related posts:

  1. Scrap it and start againAfter spending almost all of my spare time during the last four weeks working on designing and building the Lime Blast site, I had gotten to a point on Saturday morning where all I had to do was fix the......
  2. I love you Jose Diaz-GonzalezThis is a very quick post to thank Jose Diaz-Gonzalez for the Upload behaviour he wrote for CakePHP. I needed a way of uploading multiple images in the admin area of the Chameleon Photography site. During my Cake 1.3 days,......
  3. Rotten to the CoreI don’t know if there is a term for what I am – Applephobic maybe – but I hate Apple. I’m about as PC as PC can get (and I’m not talking about being Politically Correct). In my opinion, the......
]]>
For the past couple of weeks I’ve been learning how to build responsive Web sites, a process that is simple in principle, but complex in practice once you get down to the nitty-gritty.

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.

What is a responsive Web site?

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:

  • Desktop – which has two main columns.
  • Tablet – where the right column drops below the main content, and the width of the site drops.
  • Mobile – everything is in a single column which spans the entire width of the device.

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.

Responsive images

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?

Requirements

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.

Solution

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;
    }

}

Figures

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>';
}
]]>
/2012/02/images-in-responsive-wordpress-web-sites/feed/ 0