{"id":516,"date":"2008-08-27T09:20:37","date_gmt":"2008-08-27T16:20:37","guid":{"rendered":"http:\/\/www.bspcn.com\/2008\/08\/27\/10-principles-of-the-css-masters\/"},"modified":"2011-01-11T08:22:48","modified_gmt":"2011-01-11T15:22:48","slug":"10-principles-of-the-css-masters","status":"publish","type":"post","link":"http:\/\/localhost\/wordpress\/2008\/08\/27\/10-principles-of-the-css-masters\/","title":{"rendered":"10 Principles of the CSS Masters"},"content":{"rendered":"
Written by Glen Stansberry<\/a><\/p>\n When it comes to CSS, there are lots of resources and supposed “expert tips” on the web. These are from unproven, self-proclaimed “gurus” who have no street cred in the design world. While they may have valid points, how is one to know whether a CSS tip is a valid resource or just an untested hack?<\/p>\n Instead of relying on unknown sources for advice, let’s look deeply into designers who have excellent design backgrounds and have walked the walk. These CSS tips are gathered from some of the most respected designers on the planet. They have the portfolios to back their advice up, so you’ll know that each tidbit of advice is of the highest quality.<\/p>\n Below are 10 excellent principles that any web developer or designer can find useful, meaningful, or challenging. Consider this sage advice from journeymen (and women) who have walked the long, hard road of design excellence. These are the true<\/em> masters of CSS. Drink deep from their knowledge and take their wisdom on your next designing adventure.<\/p>\n What bothers me most about the mindset of CSS hackers is that they are actively searching for complicated solutions. Seek and ye shall be found, if you want complexity it’ll take you by the throat. It’ll never let go of you, and it won’t help you, either.<\/p><\/blockquote>\n Peter-Paul Koch is a godfather of web development. While he’s an old-school developer and the bulk of his web portfolio was between 1998-2002, he’s worked with the likes of Apple and other heavyweights. He’s written a book on javascript<\/a>, but don’t think for a second he doesn’t have anything to say about CSS.<\/p>\n Koch has addressed something that every<\/em> designer and web developer should follow with zeal: Keep your CSS simple<\/a>. Simplicity is a hard thing to achieve, especially in CSS design. There are a myriad of CSS hacks that one can find for making all browsers look the same, regardless of version or type. Yet there’s a fundamental flaw with using many CSS hacks. As web browsers evolve, it’s much harder to keep up with the changes.<\/strong><\/p>\n Koch makes an interesting point about developing for the web. The Internet as as whole is a very unpredictable place, and trying to second-guess the way it will work in the future is a very bad strategy.<\/p>\n The Web is an uncertain place. You’ll never be sure that your Web sites will work in the exact way you want them to work, not even when you apply all modern insights from CSS, accessibility and usability. Instead of seeking false comfort in hacks that seem all the more comfortable because of their complexity, you should accept uncertainty as a basic principle.<\/p>\n Browsers don’t have perfect CSS support; especially for people who’ve just started learning CSS, that can be infuriating. Nonetheless CSS hacks are not the solution. Acceptance of the way the Web currently works is the best way to go because it’ll keep your sites simple.<\/p><\/blockquote>\n Peter-Paul has hit on something that rings true for not only CSS, but for web development as a whole. Simplicity is key for efficiency in coding<\/strong>.<\/p>\n <\/p>\n Jonathan Snook<\/a> is an incredibly popular designer from Ottawa, Canada who’s made his name in web standards and design. He’s spoken at prestigious conferences like SXSW<\/a> and has published quite a few technical resources on design through Sitepoint<\/a>.<\/p>\n One of Jonathan’s tenants to coding CSS<\/a> is to keep declarations in one line.<\/p>\n The second one may look prettier but it sure doesn’t help me find anything. When looking for something in a style sheet, the most important thing is the ruleset (that’s the part before the { and } ). I’m looking for an element, an id or a class. Having everything on one line makes scanning the document much quicker as you simply see more on a page. Once I’ve found the ruleset I was looking for, find the property I want is usually straightforward enough as there are rarely that many.<\/p><\/blockquote>\n Jonathan goes on to give an example for single line declarations that looks like this:<\/p>\n Good<\/strong><\/p>\n {font-size:18px; border:1px solid blue; color:#000; background-color:#FFF;}<\/p>\n Bad<\/strong><\/p>\n h2 { Not only does this approach help with quickly scanning your CSS, it also helps in keeping your CSS file smaller by removing unneeded spaces and characters.<\/p>\n <\/p>\n Most people know about and use some shorthand, but many don’t make full use of these space saving properties.<\/p><\/blockquote>\n Roger Johansson knows a thing or two about designing for the web. The Swedish web designer has been working on the Internet since 1994, and has a popular web design blog<\/a>. When it comes to simple and elegant solutions, Roger is one of the most knowledgeable in his field.<\/p>\n Johansson has a very in-depth article on the importance of CSS shorthand<\/a>, and gives quite a few examples of how to use it while coding CSS. Here’s an example:<\/p>\n Using shorthand for these properties can save a lot of space. For example, to specify different margins for all sides of a box, you could use this: margin-top:1em; margin-right:0; margin-bottom:2em; margin-left:0.5em; But this is much more efficient: margin:1em 0 2em 0.5em; The same syntax is used for the padding property.<\/p><\/blockquote>\n While CSS shorthand reduces the size of the stylesheet, it also helps organize and keep the code simple. Beautiful CSS is simple CSS.<\/p>\n <\/p>\n Mr. Snook has another piece of crucial advice that every web developer should live by: allow block elements to fill space organically<\/a>. If there’s one recurring theme in CSS development, it’s to not force the code to do things it isn’t meant for. This means avoiding CSS hacks and finding the simplest solution possible.<\/p>\n My rule of thumb is, if I set a width, I don’t set margin or padding. Likewise, if I’m setting a margin or padding, I don’t set a width. Dealing with the box model can be such a pain, especially if you’re dealing with percentages. Therefore, I set the width on the containers and then set margin and padding on the elements within them. Everything usually turns out swimmingly.<\/p><\/blockquote>\n Jonathan’s rule of thumb is great for ensuring that your layouts won’t break and that the simplest approach is used when creating layouts with block elements.<\/p>\n <\/p>\n Floating is probably one of the most important things to understand with CSS, but knowing how to clear floats is necessary too.<\/p><\/blockquote>\n Trevor Davis may not be as big of a name as Zeldman or Snook in the design world, he surely deserves some mention just based on his excellent portfolio<\/a> of web layouts. His blog<\/a> is an excellent resource for any web developer wanting to brush up on his design chops.<\/p>\n In Trevor’s flagship article The 6 Most Important CSS Techniques You Need To Know<\/a>, he’s added a nugget that can save many headaches when using columns in your layouts.<\/p>\n I have created a simple page with two floating columns next to each other<\/a>. You will notice in the example that the grey background does not contain the floating columns. So, the easiest thing to do is to set the containing element to float<\/a>. But now, you will see that the container background doesn’t contain the content area.<\/p>\n Since the container has margin: 0 auto, we do not want to float it because it will move it to whichever side we float it. So another way to clear the float, is to insert a clearing element<\/a>. In this case, I just use an empty p set to clear: both. Now, there are other ways to clear a float without markup<\/a>, but I have noticed some inconsistencies with that technique, so I just sacrifice an empty p.<\/p><\/blockquote>\n <\/p>\n Sometimes it’s easier to deal with the exception to the rule, rather than add declarations for all other elements around it.<\/p><\/blockquote>\n Dan Cederholm’s company SimpleBits<\/a> is a powerhouse of a design company. Dan’s worked with the likes of:<\/p>\n … and many other high-profile web companies. Fortunately, Dan passes on some of the knowledge he’s learned working with these massive names on his blog at SimpleBits<\/a>. Here’s a rule of thumb for you web designers and developers: If Dan Cederholm says anything, you listen.<\/strong> Think of him as a digital sherpa, guiding you to the crest of your design mountain.<\/p>\n While it may seem counterintuitive to put a negative in front of any declaration (like margin-left: -5px), it’s actually quite a good idea. Mr. Cedarholm explains that using negative margins<\/a> on elements are sometimes easier than having to change every other aspect of the design to make it align they way you want.<\/p>\n There are situations when using negative margins on an element can be the easiest way to “nudge” it out from the rest, treating the exception to the rule in order to simplifiy code.<\/p><\/blockquote>\n You can see his example of proper negative margin usage here<\/a>.<\/p>\n <\/p>\n “How do I center a fixed-width layout using CSS?” For those that know, it’s simple. For those that don’t, finding the two necessary rules to complete the job can be frustrating.<\/p><\/blockquote>\n It’s no surprise that Dan is going to make this list twice. Centered layouts are on the surface a very simple idea, but for some reason they don’t always work as easily as advertised. Centering layouts with CSS can be a frustrating endeavor for a beginner if they’ve never tried it before.<\/p>\n Dan’s got a tried-and-true method that he uses frequently to achieve centered-layout nirvana.<\/p>\n #container { margin: 0 auto; width: xxxpx; text-align: left; }<\/p>\n Many modern designs rely on centered layouts, so using this method will at some point come in handy for web developers and designers.<\/p>\n <\/p>\n You’ve written valid XHTML and CSS. You’ve used the W3C standard Document Object Model (DOM) to manipulate dynamic page elements. Yet, in browsers designed to support these very standards, your site is failing. A faulty DOCTYPE is likely to blame.<\/p><\/blockquote>\n Jeffrey Zeldman is one of the co-founders of the excellent resource site A List Apart<\/a>, co-founded and ran The Web Standards Project<\/a>, runs the Happy Cog<\/a> design studio, and even wrote the<\/em> book on designing for web standards<\/a>. In short, Zeldman is in the upper-echelon of web designers.<\/p>\n The DOCTYPE of a web page is one of the most overlooked aspects of design. Using the right DOCTYPE is crucial, and Zeldman explains why.<\/p>\n Using an incomplete or outdated DOCTYPE-or no DOCTYPE at all-throws these same browsers into “Quirks” mode, where the browser assumes you’ve written old-fashioned, invalid markup and code per the depressing industry norms of the late 1990s.<\/p><\/blockquote>\n Zeldman stresses the importance of a) actually using a DOCTYPE, and points out that you have to add an url in the declaration like so:<\/p>\n If you’re finding unexplained problems with your layouts, odds are the DOCTYPE could be the problem.<\/p>\n <\/p>\n Centering items is a frequent task when designing websites. But for people that are new to CSS it’s mostly kind of enigma how to center for example a whole website in browsers other than IE.<\/p><\/blockquote>\n Wolfgang Bartelme is a web designer with Bartelme design<\/a>, a web design firm. Bartelme has one of the most elegantly-designed blogs, and continually creates excellent icon and design work<\/a>. He’s done design work for the blogging platform Squarespace<\/a>, as well as the popular software event MacHeist<\/a>.<\/p>\n Wolfgang has created a tutorial that helps with the complicated task of centering elements with CSS<\/a>. Centered elements are insanely useful, but are sometimes hard to achieve given the design. Bartelme’s tutorial ensures centered alignment by choosing the right DOCTYPE and making adding his CSS voodoo. The code nothing fancy and gets the job done, and falls directly in line with striving for simplicity in CSS.<\/p>\n <\/p>\n Trenton Moss knows web usability. He has his own web usability company<\/a> that trains people in usability training<\/a> and web writing<\/a>. He also writes for sites like Sitepoint<\/a>. Trenton gives excellent tips based on his experience as a web usability expert.<\/p>\n It’s a simple fact that designs change over time, especially in the way text is displayed on websites. The best thing a web designer can do is plan for the future to make sure that instead of having to manually change the way text is displayed, it’s best to use CSS to change the appearance of the text. Trenton Moss shows us how to achieve this through the use of a simple, underused CSS command called text-transfom<\/a>.<\/p>\n One of the lesser known, but really useful CSS commands is the text-transform command. Three of the more common values for this rule are: text-transform: uppercase, text-transform: lowercase and text-transform: capitalize. The first rule turns all characters into capital letters, the second turns them all into small letters, and the third makes the first letter of each word a capital letter.<\/p><\/blockquote>\n By using CSS to display the appearance of text on the site, it allows for change in the future and keeps things consistent over time.<\/p>\n This command is incredibly useful to help ensure consistency in style across an entire Website, particularly if it has a number of content editors. Say for example your style guide dictates that words in headings must always begin with capital letters. To ensure that this is always the case, use text-transform: capitalize. Even if site editors forget about the capitalisation, their mistake won’t show up on the Website.<\/p><\/blockquote>\n While text-transform is a small thing to add to add to a css layout, it can make a world of difference in the future when changes need to be made.<\/p>\n <\/p>\n1. Keep CSS simple – Peter-Paul Koch<\/h3>\n
The danger of CSS hacks<\/h4>\n
2. Keep CSS declarations in one line – Jonathan Snook<\/h3>\n
\nfont-size:18px;
\nborder:1px solid blue;
\ncolor:#000;
\nbackground-color:#FFF;
\n}<\/p>\n3. Use CSS shorthand – Roger Johansson<\/h3>\n
4. Allow block elements to fill space naturally – Jonathan Snook<\/h3>\n
5. Set a float to clear a float – Trevor Davis<\/h3>\n
Clearing floats<\/h4>\n
6. Use negative margins – Dan Cederholm<\/h3>\n
\n
Negative margins<\/h4>\n
7. Use CSS to center layouts – Dan Cederholm<\/h3>\n
8. Use the right DOCTYPE – Jeffrey Zeldman<\/h3>\n
DOCTYPE misunderstanding<\/h4>\n
9. Center Items with CSS – Wolfgang Bartelme<\/h3>\n
10. Utilize text-transform commands – Trenton Moss<\/h3>\n