Activate your free membership today | Log-in

Monday, September 29th, 2008

CSS Systems: Maintaining your style

Category: CSS

Natalie Downe gave a talk at BarCamp London on CSS Systems as a wait to maintain your style. She goes into details on how she goes about setting up her CSS on a project:

A CSS System is a reusable set of content-oriented markup patterns and associated CSS created to express a site’s individual design. It is the end result of a process that emphasizes up-front planning, loose coupling between CSS and markup, pre-empting browser bugs and overall robustness. It also incorporates a shared vocabulary for developers to communicate the intent of the code.

Posted by Dion Almaer at 6:03 am
2 Comments

+++--
3.7 rating from 24 votes

Friday, September 26th, 2008

Maintaining Browser Specific CSS

Category: CSS, Tip

Nick Cairns saw our post on conditional CSS for browsers and followed up discussing how he handles maintaining IE specific CSS selectors:

We keep our IE related styling right below the common (standards-based) declarations. BUT, we DON’T use hacks. Underscore hacks, * hacks, and all of those things that we all gave up with the birth of IE7 should remain dead and buried. Instead, we’re going to use IE’s conditional commenting to create IE specific CSS selectors. We do this by adding a conditional comment block as the outer most wrapper in our html template (ie. the first tag inside the ).

HTML:
  1.  
  2.   </body><body>
  3.      <!--[if IE 7]><div id="body1" class="IE IE7 IE67"><![endif]-->
  4.      <!--[if IE 6]><div id="body1" class="IE IE6 IE56 IE67"><![endif]-->
  5.      <!--[if IE 5]><div id="body1" class="IE IE5 IE56"><![endif]-->
  6.      <!--[if !IE]>-->
  7.           <div id="body1" class="W3C">
  8.      <!--<![endif]-->
  9.                 /* THE REST OF YOUR HTML GOES HERE */
  10.           </div>   
  11.   </div></div></div></body> 
  12.  

Now, in this sample, we do have support for older legacy versions of IE, so you could always reduce the number of conditions if your project doesn’t need this level of support. And, you could also easily extend it to include IE8, or to do minus versioning such as IE8-.

With this conditional block in place, it becomes quite easy to place IE only style declarations right below their standards-based counterparts. As an example:

CSS:
  1.  
  2. #header { overflow: hidden; }
  3. .IE #header { zoom: 1; }
  4.  

Posted by Dion Almaer at 6:35 am
23 Comments

++---
2.3 rating from 32 votes

Monday, September 15th, 2008

CSS Transforms: First WebKit, now Gecko too!

Category: Browsers, CSS

We discussed the WebKit CSS transforms that allow you to scale, transform, skew, and do matrix work through simple CSS.

Mozilla has stepped up and Keith Schwarz posted on CSS transform support in Firefox thanks to the new -moz-transform:

CSS:
  1.  
  2. -moz-transform: translate(100px, 200px); /* Move right 100 pixels, down 200 pixels */
  3.  
  4. -moz-transform: translate(30px); /* Move down and right 30 pixels */
  5.  
  6. -moz-transform: translate(50%, 50%); /* Move down and right by 50% of the size of the element. */
  7.  

This function simply moves elements around by the specified offset. For example, a text element with -moz-transform: translate(100px); will appear 100 pixels downward and to the right of where it normally would have been displayed. Of course, there are more functions than just translate. For example, there’s rotate, which lets you rotate an element by a specified number of degrees; scale, which scales elements by arbitrary dimensions along each axis; skew, which performs skews along the X and Y axes; and matrix, for arbitrary matrix transformations. There are also simple versions of these like skewx and scaley which allow for simpler syntax in some cases.

It will be interesting to see what uses developers find for CSS transforms. Much of the functionality once reserved for plugins can now be directly integrated into CSS and Javascript, which hopefully will help web developers create more graphically exciting pages. We also look forward to rapid standardization of this property now that there are two implementations.

Posted by Dion Almaer at 9:55 am
7 Comments

++++-
4 rating from 23 votes

Tip: Using a background image on an image

Category: CSS, Tip

Pascal Opitz answered the question "Can you set an image background on an image element?" in simple fashion.

All you have to do is make sure that the image is display: block and has a padding.

He put up a simple demo that uses a div with an image, and he applies backgrounds to both:

CSS:
  1.  
  2. div {
  3.         background: url('blur.jpg') no-repeat top left;
  4.         width: 232px;
  5.         height: 200px;            
  6. }
  7.                
  8. img {
  9.         display: block;
  10.         background: url('parallax.gif') no-repeat bottom left;
  11.         padding: 93px 100px 75px 100px;
  12. }
  13.  

You have the little chap running as the actual src of the image, the animated background as the img background, and then the sky background applied to the div.

Posted by Dion Almaer at 5:23 am
8 Comments

++++-
4.2 rating from 24 votes

Thursday, September 11th, 2008

You are not alone. None of the rest of us can fathom CSS either.

Category: CSS

Dave Minter is obviously frustrated, which lead him to write You are not alone. None of the rest of us can fathom CSS either.

He goes on a little rant that covers:

  1. Curvy corners
  2. Vertical floats
  3. Formatting for forms
  4. Floats within elements
  5. Graphical Buttons
  6. Column support
  7. Order Independence
  8. Widths on inline elements
  9. Addressing text within textareas
  10. A pony

Interestingly, do a view source on this bad boy to see nice corners that uses SVG, VML, -*-border-radius, depending on the browser. A lot of code for one feature huh?

On the other side we have 10 principles of the CSS masters by Glen Stansberry that covers:

  1. Keep CSS simple - Peter-Paul Koch
  2. Keep CSS declarations in one line - Jonathan Snook
  3. Use CSS shorthand - Roger Johansson
  4. Allow block elements to fill space naturally - Jonathan Snook
  5. Set a float to clear a float - Trevor Davis
  6. Use negative margins - Dan Cederholm
  7. Use CSS to center layouts - Dan Cederholm
  8. Use the right DOCTYPE - Jeffrey Zeldman
  9. Center Items with CSS - Wolfgang Bartelme
  10. Utilize text-transform commands - Trenton Moss
  11. And in other CSS news, Microsoft is getting nice, and is moving to -ms-* as vendor prefixes on CSS.

Posted by Dion Almaer at 8:51 am
30 Comments

+++--
3.5 rating from 28 votes

Tuesday, September 2nd, 2008

Firefox implements 3 CSS properties: text-shadow, -moz-box-shadow and -moz-column-rule

Category: CSS, Firefox

Michael Ventnor has blogged about the new support for text-shadow, -moz-box-shadow and -moz-column-rule which follows on with Safari and Opera. The subtle effects really add a great touch when NOT used gratuitously. The samples are both ;)

If you’ve been following the Gecko 1.9.1 development since it started then you’re probably already aware of, and possibly already taking advantage of, these CSS properties that I implemented but haven’t talked about yet (this web-tech blog really is a great idea!). They are in Alpha 1, but for maximum stability regarding them you should wait until Alpha 2 comes very shortly. I am, of course, talking about CSS3 text-shadow, box-shadow and column-rule.

(I would’ve liked to include an example in this post, but Wordpress doesn’t seem to like “strange” CSS properties)

Text shadows were in CSS2, but were dropped in CSS2.1 due to lack of implementations; but it’s slowly making a comeback! That’s why the -moz prefix is not necessary for text-shadow, but is needed for the other two (-moz-box-shadow and -moz-column-rule). To see some very creative uses of this property check out this web page. Another cool thing is that this property plays nicely with the “contenteditable” attribute, so you can make a text editor with flickering flames behind your text. Who said Silverlight was needed for pretty RIA’s?

Box shadow is really nice, you can apply a shadow to almost any arbitrary element in your web page, thus easily giving your web app an extra bit of beauty. We also support the multiple shadows feature for both text and box shadows. Column rule allows you to draw lines between the gaps that separate columns if you are using CSS3 columns. It takes exactly the same syntax as a border side (border-top, etc), and pretty much completes our support for CSS3 columns functionality-wise.

This post comes on the back of CSS word wrap support (e.g. word-wrap: break-word;), CSS 2.1 generated content (e.g. tr::before { display:table-cell; content:"First cell"; }), and more.

Posted by Dion Almaer at 8:36 am
5 Comments

++++-
4.7 rating from 35 votes

Monday, September 1st, 2008

CSS Sprites2: Return of the JS

Category: CSS, jQuery

In March 2004, Dave Shea wrote about CSS Sprites, and now he is back with CSS Sprites 2. He walks us through using JavaScript to make this all work nicely, and picks jQuery to get 'er done:

After putting this together piece by piece, we end up with:

JAVASCRIPT:
  1.  
  2. $(document).ready(function(){
  3.  
  4.         // remove link background images since we're re-doing the hover interaction below
  5.         // (doing it this way retains the CSS default hover states for non-javascript-enabled browsers)
  6.         // we also want to only remove the image on non-selected nav items, so this is a bit more complicated
  7.         $(".nav").children("li").each(function() {
  8.                 var current = "nav current-" + ($(this).attr("class"));
  9.                 var parentClass = $(".nav").attr("class");
  10.                 if (parentClass != current) {
  11.                         $(this).children("a").css({backgroundImage:"none"});
  12.                 }
  13.         });     
  14.  
  15.  
  16.         // create events for each nav item
  17.         attachNavEvents(".nav", "home");
  18.         attachNavEvents(".nav", "about");
  19.         attachNavEvents(".nav", "services");
  20.         attachNavEvents(".nav", "contact");
  21.  
  22.  
  23.         function attachNavEvents(parent, myClass) {
  24.                 $(parent + " ." + myClass).mouseover(function() {
  25.                         $(this).append('<div class="nav-' + myClass + '"></div>');
  26.                         $("div.nav-" + myClass).css({display:"none"}).fadeIn(200);
  27.                 }).mouseout(function() {
  28.                         $("div.nav-" + myClass).fadeOut(200, function() {
  29.                                 $(this).remove();
  30.                         });
  31.                 }).mousedown(function() {
  32.                         $("div.nav-" + myClass).attr("class", "nav-" + myClass + "-click");
  33.                 }).mouseup(function() {
  34.                         $("div.nav-" + myClass + "-click").attr("class", "nav-" + myClass);
  35.                 });
  36.         }
  37. });
  38.  

Posted by Dion Almaer at 6:41 am
3 Comments

+++--
3.6 rating from 17 votes

Wednesday, August 27th, 2008

Towards Using Custom Fonts

Category: CSS, Design

A little while ago, we talked about the two competing custom font technologies for the Web: linking and "embedding" (aka EOT). With Firefox about to implement support for linking à la Safari, John Allsopp has a summary of the state of font technologies and an illustration of just how easy it is to use these in stylesheets.

Once you define a font-face using both linking and EOT thusly:

CSS:
  1. @font-face {
  2.   font-family:"Fenwick";
  3.   src: url(fenwick.eot);
  4. }
CSS:
  1. @font-face {
  2.   font-family: "Matrix";
  3.   src: url(http://www.westciv.com/CSS3Tests/matrix.ttf) ;
  4. }

You can use a single CSS attribute to reference whichever is supported on the currently browser and gracefully degrade on browsers that don't support either technology:

CSS:
  1. p {
  2.   font-family: Fenwick, Matrix, san-serif;
  3. }

Soon-to-be ubiquitous <canvas> (somehow, we'll get there), faster JavaScript, and custom fonts. Man, this is exciting...

Posted by Ben Galbraith at 6:00 am
3 Comments

++++-
4.8 rating from 10 votes

Using CSS to do the print watermark technique

Category: CSS, Tip

Andy Pemberton has put together a simple solution to get the watermark technique to work nicely with print CSS.

Check out the sample and pull up a print preview. He details the good, bad, and ugly:

The Good

The first step to getting a printable watermark is to use an inline tag, rather than background-images. In most browsers, background-images won’t be printed unless the user selects an additional browser option to enable it.

From here, we need to place the watermark image so that it is repeated on each page. It looks like the W3C thought of this ahead of time in the positioning module. The position property’s fixed value should ensure that a watermark is printed on each page. Some browsers, like FireFox and Internet Explorer 7, apply this rule correctly.

The Bad

Not all browsers play nicely with position:fixed and we haven’t yet considered using z-indexing to ensure the watermark displays behind all the regular page content. For IE and FireFox 3+, simply applying z-index:-1 to the watermark will do the trick.

Though, it turns out that earlier versions of FireFox (and other Gecko-based browsers) don’t play nicely with negative z-indexing. So, my current approach uses a few Gecko-specific hacks to degrade the watermark approach for FireFox 2. For FireFox 2, the watermark will display over the content, but still display on every page. Unfortunately, this means the CSS for my approach doesn’t validate.

The Ugly

But of course, position:fixed isn’t supported at all in IE6; it appears to ignore the rule altogether and display the image inline. So, I use conditional comments to filter just a *few* IE6-specific hacks:

Absolute positioning and an additional 100% height on the watermark and printable content are the keys to getting this working for IE6:

div.watermark{
position:absolute;
overflow:hidden;
}
div.content{
height:100%;

}

Posted by Dion Almaer at 2:14 am
Comment here

++---
2.5 rating from 15 votes

Monday, August 25th, 2008

Sizzle: John Resig has a new selector engine

Category: CSS, JavaScript

John Resig is working on a new selector engine called Sizzle:

This is a new selector engine that I'm working on.

It's a work in progress! Not ready for use yet!

It's definitely not ready yet (got some minor outlier bugs in the standards-compliant browsers - and a bunch of major bugs in IE still left to tackle) but the results are already promising.

4x faster in Firefox 3, 3x faster in Opera 9, 1.5x faster in Safari 3 than the other major JavaScript libraries. It's completely standalone (no library dependencies) and clocks in at under 4kb.

I'm just committing my code before moving on to work on IE - so beware. And yes, I expect this engine to become the new default selector engine in jQuery.

Just 594 lines of code so far, check it out.

Posted by Dion Almaer at 12:48 am
19 Comments

++++-
4.8 rating from 24 votes

Thursday, August 21st, 2008

querySelectorAll is coming fast

Category: Browsers, CSS

We have all been talking about querySelectAll for awhile, but John Resig gives us a wrap-up that covers the state of play.

He talks about the browsers, and the libraries that wrap them and clean up shop via code like:

JAVASCRIPT:
  1.  
  2.  
  3. function querySelectorAll(selector){
  4.   try {
  5.     return Array.prototype.slice.call(
  6.       document.querySelectorAll( selector ) );
  7.   } catch(e){}
  8.  
  9.   return myOtherLibrary( selector );
  10. }
  11.  

Less code. More speed.

Posted by Dion Almaer at 8:30 am
2 Comments

++++-
4 rating from 22 votes

Wednesday, August 20th, 2008

The lessons of CSS frameworks

Category: CSS

Jeremy Keith has been doing a great job blogging An Event Apart, and his writeup of The Lessons of CSS Frameworks by Eric Meyer caught my eye.

Eric took a look at the most popular CSS frameworks (960, Blueprint, Content With Style, That Standards Guy, YAML, YUI, Elements, Tripoli, WYMStyle) and talks about choosing one...

Let’s get one question out of the way, the question "which one is right for you?" Answer… none of the above. It’s like templates. There’s nothing wrong with templates but you don’t put together your client’s site based on a template, right? They can be a good starting point for ideas but you do your own designs. If you’re going to use a framework, it should be yours; one that you’ve created. You can look at existing frameworks for ideas and hack at it. But the professionals in this room are not well served by picking up a framework and using it as-is.

Eric put together a grid of features and which frameworks support those features. Every framework does reset, colours, and fonts. The fact that every framework has a reset is evidence of the frustration we all feel with the inconsistencies between browsers. The rules for colour tend to be much more minimal. Font styling, on the other hand, is more fully-featured generally. Whereas the colour might just be set for the body element, font sizes and faces are specified throughout. Usually that font face is Helvetica. Most frameworks steer away from trying to style form elements. Almost all of them do layout, usually combinations of columns. Four of the nine frameworks included print styles. Three of the nine included hacks.

After using a framework on Google Code, I can definitely say that they add a lot, and can take some of the pain of out CSS.

Posted by Dion Almaer at 10:56 am
3 Comments