tag: css3

Useful CSS snippets

A quick round-up of some CSS tricks I use on my sites.

Reset input type search

Safari ren­ders search inputs with curved cor­ners and a spy glass. If this doesn’t fit into your design, but you want to retain cor­rect seman­tics, this reset is help­ful. The first line removes the curved cor­ners, but leaves a white space where the spy glass was, this is removed using the sec­ond line.

input[type=search] {
-webkit-appearance: none;
}
input[type=search]::-webkit-search-decoration {
display: none;
}

Bor­der radius

Both Safari 5 and the lat­est Chrome sup­port border-radius, given the fast update curves for these browsers, I have stopped using -webkit-border-radius, and almost always default to the use­ful short­hand (older ver­sions of iOS are a known caveat).

-moz-border-radius: 10px 0 0 10px;
border-radius: 10px 0 0 10px;

Tweaked clearfix

I still see a lot of sites using the old clearfix, includ­ing IE for Mac hacks. I’ve cleaned this up a bit and moved the IE spe­cific parts into con­di­tional IE stylesheets. For lists, I found I often need to clear each li, to save lit­ter­ing the HTML with class names, I added clearfixItems li, now I only need one class on the ul or ol.

.clearfix:after,
.clearfixItems li:after {
content: ".";
display: block;
height: 0;
clear: both;
visibility: hidden;
}

Then in IE6 and IE7:

.clearfix, .clearfixItems li {
zoom: 1;
}

Equal height columns

For when you want your con­tain­ers to have the same height. If you are unfa­mil­iar with this tech­nique, I rec­om­mend read­ing Ed Eliot’s arti­cle.

.col {
margin-bottom: -1000px;
padding-bottom: 1000px;
}

Lig­a­tures and kern­ing pairs

Where fonts sup­port it, text ren­der­ing can be improved by enabling kern­ing pairs and lig­a­ture sup­port. Add the fol­low­ing to your body to enable it on all text:

text-rendering: optimizeLegibility;

Firefox’s default state (text-rendering: auto) par­tially enables this, opti­miz­ing leg­i­bil­ity on font sizes above 20px (surely leg­i­bil­ity is most impor­tant on the small­est text?). Using the above code will opti­mize all font sizes, more details are avail­able at MDC, as pointed out in the com­ments by rdela.

Gra­di­ents

An invalu­able tool for avoid­ing images and extra HTTP requests. For the most part I avoid pro­vid­ing fall­back images, instead see­ing gra­di­ents as a pro­gres­sive enhance­ment. This usu­ally means Opera and FF3.5 or less will see a solid colour, I’m fine with that.

background: #990000;
background: -webkit-gradient(linear, 0% 0%, 0% 100%, from(#cc0000), to(#990000));
background: -moz-linear-gradient(#cc0000,#990000);

And in IE con­di­tional stylesheets:

filter: progid:DXImageTransform.Microsoft.gradient(startColorstr=#cc0000, endColorstr=#990000);

Animated CSS3 cube using 3D transforms

3D cube interface using new WebKit transforms

Last week WebKit included the much antic­i­pated (at least on my part) 3D trans­forms in its lat­est nightly build, announced prac­ti­cally along­side the awe­some Snow Stack demo that pro­vides a 3D inter­face for brows­ing Flickr images (use left, right and space-bar). Today the Surfin Safari blog has updated with some more excit­ing demos, includ­ing “Mor­phin Power Cubes” and “Poster Cir­cle”. It is now pos­si­ble to cre­ate all sorts of crazy three-dimensional and ani­mated user inter­faces; the power comes largely in -webkit-perspective and a num­ber of updated transforms–adapted to incor­po­rate the Z axis.

Read More »

Accordion using only CSS

An accor­dion effect can be achieved using CSS3’s :target pseudo-class, with­out requir­ing JavaScript. Using the pro­pri­etary -webkit-transition prop­erty this accor­dion can also be ani­mated.
Read More »

3D Cube using 2D CSS transformations

Multiple cubes created using CSS

Update (July 21st): Using newly released (cur­rently only in Webkit Nightly releases) 3D trans­forms, a three dimen­sional, rotat­ing cube with cor­rect per­spec­tive is pos­si­ble, and as a bonus–the cube can be nav­i­gated using arrow keys. Check out the lat­est demo, “Ani­mated CSS3 cube using 3D trans­forms”.

The impres­sion of a three dimen­sional cube can be cre­ated using mod­ern CSS tech­niques, with­out the need for JavaScript, imagery, can­vas or SVG. Using the pro­pri­etary trans­form prop­erty to skew and rotate shaded rec­tan­gles, indi­vid­ual cube faces can com­bine to form a 3D object. Cur­rently only sup­ported in recent WebKit and Gecko based browsers, most impor­tantly Fire­fox 3.5+ -moz-transform (doc­u­men­ta­tion) and Safari 3.2+ -webkit-transform (doc­u­men­ta­tion).
Read More »

Auto-scrolling Parallax Effect without JavaScript

Correctly rendered background images for parallax effect

Here’s another quick CSS3/WebKit tran­si­tions project in the con­tro­ver­sial realm of CSS ani­ma­tion. This time I have opted to recre­ate the pop­u­lar par­al­lax effect using mul­ti­ple back­ground images on a sin­gle ele­ment and the -webkit-transition prop­erty (doc­u­men­ta­tion). I have based this on Chris Coyier’s par­al­lax tuto­r­ial, reusing the star images with per­mis­sion, the tech­nique itself was coined by Paul Annett (expla­na­tion on Think Vit­a­min). If you’re not quite sure what par­al­lax is, then Chris and Paul both go into some depth to explain it and Wikipedia is always help­ful.
Read More »

Time for some CSS debate

That’s the best pun I could think of, pretty lame to be hon­est. My first foray into shared exper­i­men­tal CSS (pro­pri­etary WebKit prop­er­ties used to cre­ate a clock ani­ma­tion) has fired up an inter­est­ing debate; where should the realm of cas­caded style sheets end?
Read More »

Track outbound links using Google Analytics

Track every­thing”, lest vital vis­i­tor trends fall through the cracks — that’s my newly endorsed web ana­lyt­ics doc­trine. As a pre­cur­sor to the quan­ti­ta­tive ‘what’ and the qual­i­ta­tive ‘why’ we need that cold hard data before analy­sis can begin; Google Ana­lyt­ics is the pop­u­lar har­vester of choice and out of the box it grabs a lot. Vis­its, Pageviews, Screen res­o­lu­tion, et al — GA seem­ingly has all your con­ven­tional data needs cov­ered. But one sig­nif­i­cant trend is lack­ing — how vis­i­tors leave your site, specif­i­cally through out­bound links on a page, data that inevitably leads to a what and an avenue for inves­ti­gat­ing the why. For instance, “Which part­ner sites are attract­ing the high­est click throughs?” or more gen­er­ally “Why do vis­i­tors leave my site?”.
Read More »