demosthenes.info

I’m Dudley Storey, the author of Pro CSS3 Animation. This is my blog, where I talk about web design and development with , and . To receive more information, including news, updates, and tips, you should follow me on Twitter or add me on Google+.

web developer guide

my books

Book cover of Pro CSS3 AnimationPro CSS3 Animation, Apress, 2013

my projects

A Sass color keyword system for designers. Replaces CSS defaults with improved hues and more memorable, relevant color names.

CSSslidy: an auto-generated #RWD image slider. 3.8K of JS, no JQuery. Drop in images, add a line of CSS. Done.

tipster.ioAutomatically provides local tipping customs and percentages for services anywhere.

Gentleman wearing a steampunk gasmask and top hatGentleman wearing a steampunk gasmask and top hat

Sepia-toning photographs with CSS

css / filters

Estimated reading time: 1 minute, 13 seconds

Previously I’ve discussed the concept of CSS filters, and stepped you through converting an image to black and white using CSS, without needing to create a new version. We can use the same approach to sepia-tone an image, for an old-timey photographic effect.

The HTML for this example is an image with an attached class, using a self-portrait photograph of Daniel Proulx, licensed under Creative Commons:

<img src="steampunk-man-in-gasmask.jpg" alt="Gentleman wearing a steampunk gasmask and top hat" title="Gentleman wearing a  steampunk gasmask and top hat" style="width:512px;height:512px" class="sepia">

Using the CSS3 Filters 1.0 Module is the easiest approach:

img.sepia { filter: sepia(100%);
-webkit-filter: sepia(100%); -moz-filter: sepia(100%);
-ms-filter: sepia(100%); -o-filter: sepia(100%); -webkit-filter(1); }

…but that only covers Chrome 18+. For Firefox 4+, we write a separate SVG filter:

<svg version="1.1" xmlns="http://www.w3.org/2000/svg">
<filter id="old-timey">
<feColorMatrix values="0.14 0.45 0.05 0 0
0.12 0.39 0.04 0 0
0.08 0.28 0.03 0 0
0 0 0 1 0" />
</filter>
</svg>

Saving this code in a file with the name sepia.svg allows us to reference it in our CSS:

img.sepia{ -webkit-filter: sepia(1);
-webkit-filter: sepia(100%); -moz-filter: sepia(100%);
-ms-filter: sepia(100%); -o-filter: sepia(100%);
filter: url(sepia.svg#old-timey); filter: sepia(100%);
}

Unfortunately there’s no explicit sepia filter for Internet Explorer, so we must fake it by placing a sepia color behind the image and making the image slightly transparent, restricting these changes to IE:

img.sepia{ -webkit-filter: sepia(1);
-webkit-filter: sepia(100%); -moz-filter: sepia(100%);
-ms-filter: sepia(100%); -o-filter: sepia(100%);
filter: url(sepia.svg#old-timey);
filter: sepia(100%);
background-color: #5E2612;
filter: alpha(opacity = 50);
zoom:1;
}

(The zoom: 1 line is a hack for IE; just know it has to be there and applied). Next, we’ll look at blurring images. Play with this code on CodePen.

comments powered by Disqus

This site helps millions of visitors while remaining ad-free. For less than the price of a cup of coffee, you can help pay for bandwidth and server costs while encouraging further articles.