tags:

views:

1095

answers:

16

CSS looks like a set of tricks to me. Whenever I read about some css technique, it looks much more like tricks that techniques. When I write CSS, I often need to adapt the html structure and I feel bad.

Did some of you had some enlightenment and enjoy CSS zen ? How did you reach CSS zen ?

+7  A: 

start re-using classes, and stacking classes on elements, as well get familiar with the specificity rules so you can override some styles as needed, you'll see really quickly where it's actually useful and more than just "tricks".

Grey
A: 

I'm not quite sure I get what you mean, however, most of my HTML coding is pretty much dictated by my CSS. Its probably not the best way to do it, but I try and use no tables whatsoever and div's as much and as often as possible. From layout to look and feel as well as wherever else I can apply CSS.

I think CSS is a great tool and more than just tricks. It's a whole other way of looking at things.

Liam
nothing wrong with tables, as long as they aren't abused. The two main places where I'm fine with using tables: *Tabled data*, which makes sense. *Forms*, keeping the label and the fields nicely aligned.
Sekhat
Yep that's where I use them when I do use them most of the time.Though I can't really escape it when it comes to ASP.NET and DataGrid controls, etc.
Liam
@Sekhat: tables should *not* be used for forms! You said it yourself that the only reason you're using it is for presentational purposes, not semantic! The most common semantic markup for forms I've seen is `ol` with `label`. See here: http://alistapart.com/articles/prettyaccessibleforms
nickf
+7  A: 
  1. That's because a lot of the "techniques" are tricks or hacks and

  2. Whatever you mean by CSS zen, you reach it by working your ass off. 10000 hours of work, to be precise, if Gladwell is to be believed.

Nouveau
years and years...
annakata
+14  A: 
  1. CSS resets are incredibly helpful.

  2. After a while you learn the different bugs (mostly in IE) and avoid them.

  3. CSSEdit on the mac has more to do with my knowledge of CSS than anything else. It's simple and amazing. Firebug has some similar ability here, but not nearly as awesome.

Most of my stuff works in all major browsers right from the start, even though I develop in webkit. It only comes from doing lots and lots and lots and lots of CSS.

CSS has got to be my favorite part of development. I wouldn't consider it a "bag of tricks". It's a well thought out language with the ability to do just about any layout.

The only time it affects my HTML is when I need to do some javascript effects (like sliding drawers, etc.) -- and usually that just means putting a wrapper around things.

rpflo
+32  A: 

First of all, separate content and presentation completely, and I mean COMPLETELY so... no more

<strong class="red">[...]</strong>

(Sadly I see this too much) and all of that other stuff.

css classes (and id's) should describe the content, not define the style. for example, this is perfectly allowed:

<div id="question">
  <p>[...]</p>
</div>
<div id="answers">
  <div class="answer">
    <p>[...]</p>
    <div class="comment"><p>[...]</p></div>
    <div class="comment"><p>[...]</p></div>
  </div>
  <div class="answer">
    <p>[...]</p>
  </div>
</div>

you have enough tags classes and id's to do something with, yet no diveritis or presentation logic inside it.

remember... the most important thing to reach CSS zen is extremely well-formed HTML without getting diveritis

Also, if you read about a CSS trick, it is usually not very optimised. The example you gave can probably be more generalized, and finally work with normal coding. However the person who found this version didn't pursue it any further, since he found what he needed.

One last note is to design first for content, and include the HTML for features later on.

alexanderpas
+1. This is the whole point of CSS - to do stuff semantically correct. Hacks come later.
Chetan Sastry
I disagree with your "strong" example. The strong tag adds semantic meaning to the content. It's GOOD to use the strong tag. What's bad is using its pre-defined rendering as bold. Use Yahoo's reset-fonts.css to remove all pre-defined tag rendering, then add your own definitions as necessary.
lo_fye
@lo_fye: you're right, the class="red" is the really bad part.
Tim Büthe
@lo_fye While it's good to use <strong> to define the semantic meaning, you wouldn't want to use class="red". That's defining the style, not the meaning. You'd want something like class="warning".
Rudd Zwolinski
@lo_fye: firstly, no one said that *strong* was bad, he was referring to `class="red"` which is non-semantic (that is, it doesn't *mean* anything). Secondly, there's nothing wrong at all with using bold as a visual representation of strongly emphasised text. It's very common in both printed and online text. I would be curious to hear what you suggest instead though?
nickf
A: 

The more you use CSS, the better you'll get -- that's all there really is to it. You can read up on CSS techniques every day, but you won't remember a thing until you use it.

CSS mastery doesn't involve knowing a specific trick for a specific situation (that's what the internet's for), but rather being able to come up with a new trick for a new situation.

NolanDC
+6  A: 

Hang around in the css Zen Garden?
www.csszengarden.com

Bing
If I wanted to learn new CSS techniques, I would personally use Zen Garden as a last resort. After all, CSS can't be self-documenting the way a well-written traditional program can (with variable and function names), and I'd guess most techniques used in the majority of Zen Garden designs aren't explicitly commented either. Zen Garden shows very well what's possible with CSS, but it's bad at showing <i>how</i> it's possible. For that, books and blogs would be my first choice by far.
Jakob
+7  A: 

CSS zen is LESS CSS

  • LESS is a CSS extension that enables reuse and encapsulation of values (color values, for instance), improves inheritance, allows a better nesting of related properties and operations also.

There is a .NET Edition for LESS CSS.

Victor Rodrigues
I had never heard of that before - very interesting!
unforgiven3
A: 

As the other posts have mentioned - CSS mastery comes with practice (a couple of small-mid sized projects should be enough to get you to an advanced level and better understand the "frameworks" (quoted because I haven't researched if there are any HTML frameworks but that's the best word I can think of to describe the HTML/CSS separation of content and styling) used by current developers).

Some good resources for you to check out are the W3Schools CSS Tutorial/Reference and of course a plain old Google search to find all those cross-browser compatibility (or box-model related) hacks.

raptors
A: 

I read an excellent presentation called "Object-Oriented CSS", and I wish I had read it before I started my most recent long-term project.

2 Main Principles:

  1. Separate Structure and Skin
  2. Separate Container and Content

The presentation goes on to detail 10 Best Practices, and 9 Pitfalls.

Combine what it teaches you with ample use of the Google PageSpeed plug-in for Firefox. It will tell you which CSS selectors could be more efficient, and how to make them more efficient.

lo_fye
is CSS really a bottleneck ...ever?
nickf
it's not a bottleneck per-se... more like mud. Everything still gets to you quickly, but every CSS rule still has to be interpreted, applied to everything on the page, and rendered. Less is more. This is especially true on mobile browsers.
lo_fye
A: 

I find CSS incredibly useful for re-using and organizing my layout and code. I can't imagine developing a site without using a decent amount of CSS, and since it's a relatively straight-forward language it's easy to make quick changes to a set of elements without having to dig through thousands of lines of code. Maybe I'm just naive, but I love CSS.

I kind of feel like I'm using 'tricks rather than tequniques' with Javascript. Though it's also a must for any dynamic site, it kind of makes me feel dirty to hack together a set of functions that sends asynchronous requests (mainly) because it just looks cooler :).

Adamjstevenson
+3  A: 

The road to enlightenment comes when you realise that CSS is great for styling elements but awful for defining layout. So just except that for complex, cross-browser layouts you are never going to come up with a solution that is as neat and pleasing as you would hope. Oh, and excepting the revelation that IE6 was put on this Earth as a means of testing your patience to snapping point would also help!

Dan Diplo
+1+1+1!! I thought I was the only one who was bothered by some of the viewpoints, that CSS is gods gift to all things visual, and HTML is really a data format... Yes, as you say, its for styling elements, NOT for other aspects of presentation such as layout.
AviD
CSS3 looks set to make layout using CSS a much easier proposition, although it's success is entirely dependent on slow moving committees and (not always slow to deliver) browser vendors. WebKit particularly is making leaps and bounds in delivering the latest features—whilst remaining extremely web standards compliant.
different
+4  A: 

Understand the basic layout model of CSS.

The basic unit is the box. Block-level HTML-elements like BODY, P, H*n* etc. corresponds to a box in the layout.

A box is rectangular, and by default expands horizontally to the available width. Vertically they are stacked. Boxed can be nested, e.g. a P-box will at least be nested inside a BODY-box.

Boxes can have margin, border and padding, according to the box model: http://www.timofejew.ca/2006/css-box-model-hierarchy/

So, you have a vertical stack (or column) of nested boxes. If you want some boxes to move outside the main column, e.g. as a sidebar, you float (float: left|right) it and it is moved to the left or right edge of the containing block, and the main stack wraps around.

If you want boxes to be laid out horizontally (rather than vertically), you use display:inline-block.

If you need a grid where edges of boxes are aligned in two dimensions, you use display:table. But since IE before version 8 doesn't support that, you may choose to "cheat" and use HTML-markup tables to create the grid.

If you want to place a box completely independent of the flow of boxes, you use absolute positioning, which will place the box exactly where you want - possibly overlapping other content.

JacquesB
+16  A: 
Jakob
One important thing to note is that, in some cases, there actually *is* something inherent about certain elements that make them appear in a certain way. Some browsers don't support display: table-cell, for example, but they can all display tables anyway—assuming we use table markup. How is this possible? My guess is that the implementation of CSS in those browsers is simply coded in an inflexible way.
Jakob
This is good thinking. I definitetly haven't thought about tables in this way, but will from now on.
Martin
+3  A: 

Remember that anything you do can be superseded, and that your site must be adaptable to change.

Look good naked.

Content is king. Websites are no different to books in that they format text in a way that is pleasing, visually, to the reader, but if you remove the wrapper the site must still flow like a document should.

The best way to know you have got it right is once you've removed all the CSS - this way you can get the bare bones of your informational hierarchy, and if it all looks like everything has equal weight then it's been done wrong.

By using HTML elements for their semantic meaning, you're doing more than reducing the amount of divs you're using, you're creating meaning for your document. Search engines give more weight to pages that have the search term in the heading than in the body content. Screen readers give emphasis to headings, not because they appear larger, but because you have given them priority in the document.

<div class="heading">What I ate for breakfast</div>
<div class="paragraph">Cereal, orange juice, toast.</div>

Even though this works from a presentational perspective, it has far less meaning than this:

<h1>What I ate for breakfast</h1>
<p>Cereal, orange juice, toast.</p>

Being even stricter, you could break down what I ate for breakfast into an unordered list, but that can be considered overkill for this short example. The point is that by using HTML as description of your content, you're benefiting everyone - yourself for better search ranking and your readers using alternative methods to display the page.

Not everybody is the same.

Designing for the web is certainly not designing for print. You have to make your site look good for your readers, on whatever platform they decide to use - Mac OS X, Windows, Linux, iPhone, mobile, etc. Before the iPhone appeared, mobile browsers sucked. Most still do, and in these cases once you've written your semantically correct markup you will be able to restyle the site accordingly - after all CSS includes support for many different media, including but not limited to computer screen, projector, mobile, print, screen reader etc.

Not everyone's computer is the same either - colours can be different, fonts installed can be different, hey, they could be using IE6. Never make a user feel that the browser they have is inadequate with popup messages that say 'Your browser is out of date'. Instead adapt. Include wide ranging font families that cover the latest and greatest fonts mixed in with the commonly used ones lower down the tree. Enhance your site progressively, so you can have a great design in IE—and make it even better for those Safari/Firefox users with things like CSS3 multicolumn, or multiple background images.

Code sensibly.

Thirdly I just want to touch on CSS best practices. It is a mistake to think that you need to give everything an ID and a class in your HTML. Unless you are running a page that needs those IDs for javascript, you should always start without the classes and add them in later.

Make use of descendant selectors like p em { }, child selectors p > em { } and adjacent selectors p + em { } to target specific elements. It's easy to predict patterns in how your documents are styled, and you should not need to create classes 'just because'. A good trick I use is the element + * { } rule - it applies styles to anything that comes after a certain element and it's nifty for clearing floated elements.

I prefer to consolidate many common rules into one big rule, for example if I wanted a bunch of different elements red I'd use something like this:

blockquote, a, h3 { color:red; }

This is much better than giving each of these elements a class, and of course you can always override styles later on. This is the real beauty of css, that any general rule can be cancelled out with a specific one later on.

This turned out a little longer than I planned. To summarise - write great HTML that doesn't need divs (unless you have to for styling workarounds), plan your design to be cross browser bearing in mind all the differences for every user, and code minimally. There's many more selectors out there, and I only touched upon a few here - the point is to code without classes unless you really have to.

different
I agree with most of what you wrote, but I'd like to point a few things out:- The phrase "all CSS removed" actually means "using the default browser stylesheet." It may seem like nitpicking, but it's an important distinction in this context.- The more advanced selectors aren't supported in IE6 and older browsers, so care must still be taken in that regard.- I don't agree in general with your last advice (adding only what's strictly necessary). Why I should have to foresee the site's CSS while I'm still writing the markup? And what if someone else creates or modifies the CSS later on?
Jakob
Hi Jakob, agreed on point 1. Should have clarified that.I do have a problem with over using classes when the cascade is more than adequate, and do recognise that previous versions of IE don't support the more advanced selectors. However, if selectors are universally supported it's better to use these than classes, if only for consistency.You're designing the website as a whole, although you must keep CSS/HTML separate you should be in control of both from day one. For complex layouts it does make sense to use many divs, but do so semantically. :-)
different
A: 

well... to reach "css zen" you must enjoy css... if you don't, its not gonna happen. BUT, if you do, then you need to practice. Just make some fake html and try to use css to make it look good.

Jcubed