Daniel T Ott » Home

Thoughts

“I could while away the hours, conferrin’
with the
flowers, consultin’ with the rain…”

Format Your CSS

For years, I’ve always formatted my CSS in what I like to think of as the “traditional” manner. Each selector on one line, and each property indented one tab underneath:


#content{
	width:49em;
	padding: 0 1.5em;
	margin: 0 auto;
	clear: both;
}
#content h2{
	font-size: 2em;
	height: 1.5em;
	margin-left: .75em;
	color: #000;
}
#content h2 a{
	font-weight: normal;
	float: left;
}
#content h3{
	font-size: 1.25em;
	line-height: 1.2em;
	color:#4d9fdd;
}

Pretty standard stuff. I’d break up sections with big comments, and that made it fairly easy to scan. While this format never caused and problems per se, and it was easy enough to implement, it never seemed completely ideal.

When I started working with Sprokets, the other front-end developer there was using one-line css declarations. This threw me for a loop for a while, but I slowly started liking it better and better.

The method I’ve finally settled on is based on the one-line approach, but with an extra twist: I indent the declarations based on it’s place in the HTML document. For example, take the HTML for this simple page:

<body>
	<div id="one">
		<p>text</p>
		<p>text</p>
	</div>
	<div id="two">
		<p>text</p>
		<ul>
			<li>list item</li>
		</ul>
	</div>
</body>

The CSS to access this code would be like this:


body{ properties; }

	#one{ properties; }
		#one p{ properties; }

	#two{ properties; }
		#two p{ properties; }

		#two ul{ properties; }
			#two ul li{ properties; } 

This has worked very well for me so far, and I’m looking forward to trying out on some bigger projects. It allows me to scan through my CSS much more quickly, view comments more easily, and has the added benefit of helping me think through things like descendants and inheritance. Take a look at the CSS for this site to see it in action on a bigger scale.

This method, while new for me, is clearly not revolutionary. However, since I stumbled on it on my own, I thought some of you guys might benefit from it as I have. How do you format your CSS?

It’s been a while.

Well, well. It looks like someone has been neglecting their blog.

It happens, I guess. The problem I ran in to as time wore on without a post was that the longer I went, the more epic the post had to be that finally got me started again. A lot of things have happened, and I wanted to talk about all of them. Obviously, the post that I had in mind kept getting longer and longer.

Well, no more. Partly inspired by Russ Casenhiser (via URN), and partly because I just needed to get going again, I present you with this post: a (mostly incomplete) roundup of most of the things I’ve wanted to touch on. Some of it may be old news, some of it may be new. Either way, after this post I can get back on the blogging horse again.

iPhoneification

I got an iPhone in late September. I love it. It’s one of those purchases where, after a couple weeks, I forgot how I lived without it. I was apprehensive about the keyboard, but it turns out that was completely unfounded. I don’t even have to really try to type correctly, because the iPhone’s auto-correct is so amazing. The EDGE network isn’t as slow as everyone says (it’s slow, but come on, people, it’s not that bad). The integration between Safari, Google Maps, and the actual phone features is so convenient, it’s ridiculous. I haven’t regretted buying one for an instant.

It’s Sproket, not socket!”

I joined the Sprokets team sometime a few months ago. This experience has been a revelation for me. I’d never worked with more than one other person on a project before, so coming on board with a team like Sprokets was, while a little intimidating at first, ultimately a huge breath of fresh air. It’s allowed me to concentrate on honing my front-end skills, and collaborating with other skilled developers always helps me improve across the board. It also lead me to a new way for me to format my CSS, so watch for a post on that in the future.

Leopardosity

I pre-ordered Leopard, and installed it as soon as I got it. Again, a new Apple product that I love. Leopard gave me Time Machine, Screen Sharing, a new sidebar in Finder, major improvements in Spotlight, and Spaces.
Spaces could use some improvement, especially when you mix Spaces and traditional, cmd-tab application switching, but it’s still awesome. I keep all of my chatting applications in one space, Coda in one space, etc. It’s nice.

The new sideboard just plain old rocks.

I’ve also been using Spotlight for launching applications/documents/websites (a la Quicksilver), which turns out to be surprisingly fast and efficient, even for just-downloaded-and-installed applications. It’s real cool.

Portfoliofy

Finally, I’m in the process of reworking my portfolio. This site has a distinct lack of Javascript, but I figured since that was a large part of my skill set, I should include at least a little bit on my site. The portfolio is now set up slideshow-style. I’ll be working on it more now, and there are some existing issues (it looks a little off in Safari, and I haven’t even begun to test it in legacy browsers), but it’s fun so far.

So, what now?

I’ve got some ideas. I’m going to try to start a once-per-week blogging schedule as far as articles go (I know, I know, there is no try, do or do not, etc…), and I’m going to step up my linking. All in all, I’m feeling good about the whole thing.

We’ll see what happens.

Cleveland Has A New CSS Off Winner!

Congrats to Brad Dielman for pulling in the win in this month’s CSS Off. Attention to detail, clean code, and a sly use of hReview helped Brad in the newest victory for Cleveland. Nice work, Brad!

As you may or may not know, I was one of the judges for this month’s contest. Judging was a very rewarding process for me. It’s one thing to sneer at someone’s code and talk about Standards and best practice and everything, but it’s an entirely different thing to have to back it up with reasons. I also strived, when I could, to offer suggestions on how to fix something, instead of just pointing out that it was wrong. It was a very fun experience, and I’m looking forward to helping out on next month’s CSS Off.

Again, congratulations, Brad! Cleveland represent!

New Section: Linked

You may or may not have noticed, but there is a new section here. It consists of sites that I have saved to my Ma.gnolia account. Every day, I find things on the web that I want to share with people, but I haven’t wanted to clutter up my blog to do so. The solution is Ma.gnolia. There’s a feed and everything. I hope you find it useful/interesting.

CSS Off Judging

So here’s some good news. The guys over at CSS Off recruited five judges to help out, including yours truly.

I’m really excited for the chance to help out with this project. We have a good group of people who are all active in CSS Off as well as the blogging community, so it’ll be fun to put all of our heads together and read some code!

Ok so we’re all probably huge nerds as well…but I’ve never been one to deny that about myself(:

Anyway, like I said before, the next contest starts at 12:01 am CST (1:01 am EST) on September 15. That basically means twelve (one) in the morning on this Friday night. I’ll see you all there!

CSS Tip #1: Use A Reset

So I’ve been thinking for a while about what I’d like to blog about, now that I’m at least comfortable (if not perfectly happy) with the presentation of dtott.com. What I decided on is starting an ongoing series of in-depth CSS tips. This will (obviously) be the first one.

What’s A Reset?

A CSS reset is a set of CSS declarations designed to strip away all possible default formatting before adding any of your own. Every browser out there has default formatting for every HTML element. The body element in almost every browser has padding, margins, a set font size and color, and a set background color. Headings have margins, padding, different font sizes, and different font weights. Turn of CSS on your browser while viewing a site, and you’ll see the default CSS that the browser uses. What the reset does, then, is strip all of that away. Headings, paragraphs, fieldsets, and everything else, set to one font, one size, one weight, and no extra paddings and margins.

But…Why?

Well, there’s a few reasons. First, by resetting the CSS, you remove the temptation to rely on browser defaults that may or may not be the same cross-browser. H1s, at least to the naked eye, are pretty consistent across the field. What about fieldsets or dls?
Second, removing that temptation will make you a better CSS coder. By having to explicitly set each attribute on each element you want to use, it forces you to learn what you might normally gloss over.
Third, it will make your code meaner and cleaner. You won’t have to set the margin and padding every time you use a ul unless the design calls for it. No more wasting lines and time undoing what the browser does for you.

OK, I’m Sold. What Now?

Go out there and get a reset! If you are a control-freak-ninja-CSS-master, you could write your own, but there’s no need for this. Eric Meyer has already done it for us: The Eric Meyer CSS Reset. Is this the only one? No. The Yahoo! User Interface Library has a reset, and I’m sure there are more out there. Eric’s is the best. I could go into why, but it’s already been done here and here (including the comments). Just go, get the code, and put it above your stylesheets.

Cool!

I concur. Do you concur?