Feed on
Posts
Comments

By accident the other day I came across the CSS3 target selector.  If you’re unfamiliar with what pseudo class selectors are, the W3C defines them thusly:

“The pseudo-class concept is introduced to permit selection based on information that lies outside of the document tree or that cannot be expressed using the other simple selectors. A pseudo-class always consists of a “colon” (:) followed by the name of the pseudo-class and optionally by a value between parentheses.”

There are several pseudo classes widely in use today, for example the :visited selector which is normally used like:

a.external:visited

to specify styling of links that have already been visited by the user.

The target pseudo class selector allows us to style parts of the web page that are targeted by bookmark anchor links. Again, I’ll use the W3C for an example:

“Some URIs refer to a location within a resource. This kind of URI ends with a “number sign” (#) followed by an anchor identifier (called the fragment identifier). URIs with fragment identifiers link to a certain element within the document, known as the target element. For instance, here is a URI pointing to an anchor named section_2 in an HTML document: http://example.com/html/top.html#section_2. A target element can be represented by the :target pseudo-class. If the document’s URI has no fragment identifier, then the document has no target element.”

You’re seen these links since the web began, however now we can apply styling based on the specific bookmark anchor. For example, using the URL in the previous paragraph, we could create a style that was only applied to the bookmark anchor section_2 if the URL contained #section_2. An example helps illustrate what I mean. Take a look at this page, but don’t click anything. Feel free to scroll up and down. Nothing special right? Now try clicking one of the links in the page, or go to the page again using this link /downloads/pseudo-target.html#two. Pretty cool. No JavaScript or anything else needed. What happens is that the bookmark anchor you follow gets styling applied to it only when the URL contains the bookmark anchor (e.g. #two, #six, #seven, etc.). The CSS that accomplishes this is:

dt:target {
	color:blue;
	background:#EFEFEF;
	border:1px solid #CCCCCC;
	padding:4px 2px;
	-moz-border-radius: 6px;
	-webkit-border-radius: 6px;
}

dt:target + dd {
	padding:4px;
	color:#336;
	background: #FDFDFD;
	border-bottom: 1px dotted #CCCCCC;
	border-right: 1px dotted #CCCCCC;
	border-left: 1px dotted #CCCCCC;
}

By applying the :target pseudo class selector, we can tell the browser to only style the elements on the page that match the bookmark anchor chosen. Among other things, this allows you to easily style part of a page like a FAQ and highlight the relevant link only when the user has clicked on a bookmark anchor or followed a URL that targets a specific anchor.  How many times have you followed a link that jumps you to a specific part of a long document only to not be sure that you’re seeing what you expected. This is especially true when bookmark anchor links take you to the bottom of a page when the browser can’t pull the content up to the very top of the window (i.e. link one versus link 10 in the demo).

Browser support

Since the :target pseudo class selector is a CSS3 feature, not all browsers understand it, or implement it fully. Firefox 3.5.1 and Chrome 2.x do a great job. Safari 4 almost gets it all right, and IE8 ignores it completely. Opera 9.64 seems to handle the :target selector alright, but ignores the sibling CSS (dt:target + dd).

I’m happy to announce that jQuery Infinite Carousel 1.0.1 has been released. This new version, which can be downloaded from the jQuery Infinite Carousel page, fixes a few bugs, adds a new feature, and comes in a reorganized zip file with a demo.

Changes:

  • Bug fixed for caption box height for large values
  • Default format for captions is now HTML instead of text
  • Images now in their own folder
  • Demo added
  • Documentation updated

Over the years I’ve needed a quick reference for CSS shorthand properties for things like font, border, and background. While there are a few decent cheat sheets out there for this, inevitably I end up having to pull them off my wall and read them because they aren’t clear enough. I finally decided to make my own and I’m making it available to anyone who wants it. It’s a one page PDF which should fit nicely in a wall or binder. It covers the three properties I mentioned above since I find myself using them the most. While it’s true that there are more CSS properties that can be written in shorthand, I find that they’re either a) easy to remember (e.g. color:  #123 versus color: #112233) or they’re infrequently used (e.g. outline?). If you notice any errors please feel free to let me know and I’ll update the sheet.

CSS Shorthand Cheat Sheet in PDF format, or as a PNG image.

CSS Shorthand Cheat Sheet

First of all, apologizes for the unplanned disappearance of this site (and a few others I run). Apparently a post or two received a lot of attention from reddit.com which caused our host to pitch a fit and shut our sites down. Lovely people. Knee-jerk reactions like this are always fun to fix. From what I’ve heard a lot of hosts have this type of shoot first ask questions later policy.

So what happened? On Thursday our site caused the server we’re on to get hammered and this must have set off some sort of alarms at our hosting provider. Now rather than look into the problem and do some basic troubleshooting, we were summarily moved from our old server to the stabilization server. However as our host now admits, they botched that up and didn’t move our databases over which caused the site to become dead. So for the past two days I’ve been going back and forth with them via email, their ticket system, and yes by phone. All that needed to be done was add a caching plugin to our site that got hammered and we should’ve been golden. However to do this, I needed web access to my site and functional databases, neither of which my host could seemingly provide.

For the time being we seem to be functional and hopefully we’ll be around for the foreseeable future. Thanks for your patience; mine are long gone.

About a month ago I was reading the New York Times online and they had an article which showed a road in Brooklyn that had been reconstructed to make it safer and more pleasing to the eye. To show the difference  in the reconstruction project, they showed a before and after picture using Flash that let the visitor drag a slider over the images, which were sandwiched with one on top of the other, so that you could easily see how dramatic the changes were. I immediately thought that this could be done in JavaScript using jQuery, so I set out to do it. Here’s the result:

Pretty slick no? The possibilities for this plugin are endless. Doctors can have before and after images of patients, Photoshop users can show the before and after differences between images, remodelers can show the before and after images of projects and on and on. This plugin weighs in at only 7K and can be used multiple times on a page.

Download

Download jquery.beforeafter.zip (version 1.1.0  – January 12, 2010)

What’s So Great About this Plugin?

  • Slick effect, no Flash needed
  • It’s just 7K (4K compressed)
  • Reusable on multiple containers
  • Degradable. If the visitor doesn’t have JavaScript they will still see both images.

How to Use

First, your before and after images must be the same size.  Each image must be inside its own div, and both of those within a containing div which must have an ID.  See this example.

<div id="container">
 <div><img alt="before" src="before.jpg" width="600" height="366" /></div>
 <div><img alt="after" src="after.jpg" width="600" height="366" /></div>
</div>

All images *MUST* have the width and height declared otherwise the plugin won’t work in Safari, Chrome, and any other webkit-based browsers.

To use the plugin you’ll need to have a copy of  jQuery and the jQuery UI, or point to jquery on Google and jqueryui on Google, and the plugin. Place the files on your site and link to them:

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.1/jquery-ui.min.js"></script>
<script type="text/javascript" src="jquery.beforeafter.js"></script>
<script type="text/javascript">
$(function(){
	$('#container').beforeAfter();
});
</script>

That’s it! You can apply the before/after plugin to any number of elements on a page.

Options

The following options are configurable:

  • animateIntro  – whether or not to have the drag bar start completely to the right of the container and gradually move by itself to the midpoint (default  false)
  • introDelay – if animateIntro is true, the number of milliseconds to wait before beginning the automatic drag bar move to the center of the image (default  1000)
  • introDuration – if animateIntro is true, the number of milliseconds it will take for the drag bar to go from the right side of the image to the middle of the image (default 1000)
  • showFullLinks – whether or not to display links below the image that a visitor can click on that will automatically show the full before or full after image (default true)
  • imagePath – the path (absolute or relative) to where you store the navigation images (default ‘/js/beforeAfter/’)

Options are added when calling the script:

$('#container').beforeAfter({
	animateIntro : true,
        introDelay : 2000,
        introDuration : 500,
        showFullLinks : false
});

Enjoy!

Demos

Note that the plugin uses several images which are kept in the same folder as the plugin. If you store the plugin in another folder on your server, update the path to these images.