CSS


1
Aug 07

Making paragraph indent just like in the paperbacks

Pick up any paperback and look at the paragraph indentation. You’ll generally see that indentation isn’t used on the first paragraph after a heading, but is on the subsequent paragraphs. If there’s a big bold heading, it’s pretty obvious that a new section is beginning, but for the paragraphs after that, the indent acts as a visual clue, letting you easily see the organization. Here’s how to do that technique in CSS.

The trick we’ll use is the p+p rule. The plus sign in CSS means “immediately following”, so p+p means a p tag which comes straight after a p tag. This means that we can set paragraphs which don’t have a heading before them to indent. Paragraphs which follow a heading won’t be affected by this rule, but the next paragraph will be (since the tag before it is a p).

Here’s an example of the HTML code to use:
<h2>The secret history of animals</h2>
<p>This paragraph is coming straight after an h2 tag, so the p+p won't apply to it. The p+p rule only applies to a paragraph tag coming straight after a paragraph tag</p>
<p>This paragraph will be affected though, and so will another paragraph coming straight after it. It gives the paragraphs a very "designed" feeling, looking sleek and professional.</p>
<p>Here's the last paragraph, just to prove a point. If we put another heading, then a paragraph after it, that paragraph will pick up the same traits too.</p>

Here’s the basic CSS to use:

p+p { text-indent: 1em;}

you can also explicitely state that the ordinary p should have a zero indent:

p { text-indent: 0; }

but it’s not really necessary.

Heres how it looks (with a little more formatting added:

p_plus_p css

And here’s the full code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>p_plus_p</title>
<style>
* { font-family: Georgia; color: #333;}
div.box { width: 400px; }
p { line-height: 1.3em; font-family: Arial, sans-serif; font-size: 0.9em; color: #000;}
p+p { text-indent: 1em;}
</style>
</head>
<body>
<div class="box">
<h2>The secret history of animals</h2>
<p>This paragraph is coming straight after an h2 tag, so the p+p won't apply to it. The p+p rule only applies to a paragraph tag coming straight after a paragraph tag</p>
<p>This paragraph will be affected though, and so will another paragraph coming straight after it. It gives the paragraphs a very "designed" feeling, looking sleek and professional.</p>
<p>Here's the last paragraph, just to prove a point. If we put another heading, then a paragraph after it, that paragraph will pick up the same traits too.</p>
</div>
</body>
</html>


27
Jul 07

Remember the symbol for class and id in CSS

I always mix up class and id in CSS. “Is class a dot or a hash?” “Is id a period or a pound?” So early on, I came up with a quick brain trick to help me remember which is which.

For id, the symbol is a # (hash or pound depending on your dialect). For this, I think of an id number, as in “My id is #43368″.

For class I think of the word period, and how in school we used to have a break period in the middle of the day for lunch. I imagine all these little dots sitting at their desks, and the bell going off.. end of class.