Kent Pitman

Kent Pitman
Location
New England, USA
Title
Philosopher, Technologist, Writer
Bio
I've been using the net in various roles—technical, social, and political—for the last 30 years. I'm disappointed that most forums don't pay for good writing and I'm ever in search of forums that do. (I've not seen any Tippem money, that's for sure.) And I worry some that our posting here for free could one day put paid writers in Closed Salon out of work. See my personal home page for more about me.

MY RECENT POSTS

DECEMBER 7, 2008 12:40AM

Tutorial: Fancier-Looking Blog Posts with CSS

Rate: 62 Flag

Some people asked about how I did the fancy formatting in a recent post. This is a mini-tutorial on the CSS parts of things. It assumes you have done some HTML editing.

There is a special additional stuff you can do using something called CSS (Cascading Style Sheets). CSS is a whole extra language for specifying layout and can be piggy-backed onto HTML text by using a style attribute on various elements. You don't have to understand all of CSS to know how to do some cool things. Just take a look at some examples:

Use <p style="text-indent: 2em;">Your text here.</p> to create a paragraph that has 2 ems of indentation in the first line only. An "em" is a funny unit that is the width of the capital M in the current font. That character is usually the widest in the font, and so an em is considered to be a "wide space" in any font. This paragraph is indented 2 ems.

Don't be perplexed,
<p style="text-align: center;">this will center your text.</p>
It's more clear from afar,
if you use <br />
which forces a line break
to shorter the lines make.
The centering will show 'em.
You've got a great poem.
So work out a rhyme
Or just have a good time.

Use <p style="text-align: right;">Your text here.</p>
to align to the right instead of the left.
Not that you'd want to do that a lot here
since Salon leans left.

Draw a border with the border attribute. A solid black border one pixel wide (a pixel being a single screen dot, the smallest your monitor can display) is done with:
<p style="border: 1px solid black;">Your text here.</p>

That looks crowded, though, doesn't it. Usually you want padding around the text inside the border. Let's add about 5 pixels of padding:
<p style="border: 1px solid black; padding: 5px;">Your text here.</p>

You can collect several paragraphs into a group called a division using a “div” element:
<div>...multiple paragraphs here...</div>

A useful feature of a div is that it can have a background color. For example, in a style attribute, you can say “background: silver;” in order to see the text on a silver background.

Having done that, you can put a style attribute on the division that will apply to each of the things inside it. For example, this paragraph and the one before it are grouped into a div that looks like this:
<div style="border: 1px solid black; padding: 5px; text-indent: 2em; background: silver;">
<p>You can collect...</p>
<p>A useful feature...</p>
<p>Having done that...</p>
</div>

You can specify the font to use, but beware: Just about everyone on the net has different fonts on their computer, so often this doesn't work or has variable effect. You specify the “font-family” in a style setting, and the recommended value given is a list of fonts to try in order from left to right until one is found. For example, to get a font without serifs (the little horizontal bar at the top and bottom of a capitalized “i” for example), you specify
  style="font-family: Arial, Helvetica, sans-serif;"
(Trivia: “sans” is French for “without” if you didn't know.)
To get a font with serifs, try:
  style="font-family: Times New Roman, Times, serif;"
But as long as you put in these fallbacks, you can try others. For example, if ou write:
  style="font-family: Goudy Old Style, Goudy, Bookman, Century Schoolbook, Times New Roman, Times Roman, Times, serif;"
you should get Goudy Old Style (if it's present on the end-user's machine), or Goudy (if it's present), or Bookman. If this paragraph you're reading looks different than others before it, that's because you've got one of those fonts. I set up this paragraph using:
<p style="font-family: Goudy Old Style, Goudy, Bookman, Century Schoolbook, Times New Roman, Times Roman, Times, serif;">You can specify...</p>

Some colors are possible to name. For those that are not, you can use a “#” character followed by 6 letters which are hexadecimal digits. If you don't know what a hexadecimal digit is, don't get scared. It just means that letters are bigger than numbers and that f is the biggest. So ffffff is the biggest six-digit number and 000000 is the smallest. You're almost there. Don't panic. Each pair of digits represents a primary color. The first two are red, the next two blue, and the next two green. So 000000 means no red, no blue, no green (the color ends up black, by the way). ffffff means lots of red, lots of blue, and lots of green (and ends up being white). 00ffff has no red but lots of blue and green. That's going to come out aqua (or some might say cyan, but aqua is the official name). If you don't know a lot about mixing colors of light, it's not like paint. Yellow is made up of red and green in light. (Trust me.) So ffff00 is yellow. But if you put in more blue, you'll get a color closer to white, which means a pastel yellow. ffffbb for example. This paragraph is that pastel yellow color, #ffffbb.

pumpkin face

You can make text flow around a picture or a div by using the CSS directive “float: right;” or “float: left;”. Put the thing you want to be flowed around first, then the text that follows will do that. You can set the width of a div with a width specification. A normal Open Salon column is 485px wide, so if you use, for example, 200px, you'll get just under half a column. The pumpkin that is featured in this column is set to be 50 pixels wide and because no height is set will appear proportional height. A margin of 5 pixels is added around the pumpkin outside of its border so it doesn't run into the text. (The margin is like the padding, but margin is on the outside of the border instead of the inside.) That pumpkin was specified by:
<img style="float: right; width: 50px; margin: 5px; border: 2px solid black;"
  src="http://open.salon.com/files/pumpkin-face1221728008.gif" mce_src="http://open.salon.com/files/pumpkin-face1221728008.gif"
  alt="pumpkin face" />

A div or paragraph can be resticted to a particular width, forcing the item to be that particular width rather than the default of 485px. This paragraph is 300px wide, as done by:
<p style="width: 300px;">

Another thing that can be set to a width is a horizontal rule, <hr />.


You can set its width explicitly. You can also say whether the short line should appear left, center, or right.
<hr style="width: 150px; text-align: center;" />


In fact you can give the rule a height and a color if you like:
<hr style="width: 200px; height: 5px; text-align: left; color: green;" />


This is one last example, pulling it all together. Hopefully it will demonstrate the use of a custom color as well as a custom font.

The div starts with:
<div style="border: 1px solid black; margin: 20px; padding: 10px; text-indent: 2em; background: #ffffbb; font-family: Goudy Old Style, Goudy, Bookman, Century Schoolbook, Times New Roman, Times Roman, Times, serif;">

And, of course, the custom indentation.

You can override any of those attributes on a per-paragraph basis, of course. The “Sincerely” below is done with:
<p style="text-indent: 0em;">Sincerely,</p>

Sincerely,

Kent Pitman

The official CSS specification can be found at w3.org.


If you got value from this post, please "rate" it.

Your tags:

TIP:

Enter the amount, and click "Tip" to submit!
Recipient's email address:
Personal message (optional):

Your email address:

Comments

Type your comment below:
Fantastic! And remarkably clear instructions, even for someone like me who has only the most basic experience with html. Thank you.
Clear, helpful advice, which I rate for the wonderfulness and for the fact that I don't think I've yet seen "fancy" used as a tag anywhere.
Glad it struck your fancy for whatever reason, Saturn. :)

Biblio, if you run into questions, feel free to post them.
Thank you! This comes at a great time for me, as I just learned HTML and realized I needed a little CSS to go with it. This is perfect! Thank you!!! Rated, needless to say.
Very nice tutorial! I was going to use a few of your tricks right here but common-taters don't get to have formatting fun. Wah.
Thanks for this, Kent. I thought you had imported this from an office program like word. I am happy you can instruct me on this matter. I feel happy at this point if all my words are spelled right and i can et my ideas accross.
Leslie, yes, the rules for what you can and can't use in different places in OS are odd. They have programs that actually go in and strip out different parts of your HTML or CSS notation when you submit it, depending on what the context is. The rules for a post are different than for a comment and different for mail and different for that little bio blurb under your avatar. Highly confusing.

Their choice to do the editing of markup is not a bad thing, as it retains the quality of the site in the face of ordinary mortals who if they try this by hand may mess things up pretty badly by making typos... it repairs a lot of would-be damage. I just think there should be fewer context variations and that the rules should be published so that people can understand them better.
Kent, a great tutorial that you have presented here and I imagine that anyone who wants to start coding their posts this way will be thrilled with the additional options available to them for presenting their story. There are a number of great CSS books on the market if anyone wants additonal ideas and visual references, too.
I'm totally impressed with the clarity of this tutorial.
bowing in awe to your CSS godliness. very impressive, clear & most of all, extraordinarily helpful
Kent, you might consider adding a "OS User Manual" tag to the post. There are several posts already tagged that way. I'm going to add it to my "Making OS Easier" list as well.
jiminy crickets! This is amazing, Kent! You are the master.
Thank you! I had cracked HTML at another site, but haven't started with the CSS yet! This post is bookmarked, so please, never delete it!
You are a God-send to the CSS-challenged!
GREAT POST KENT. and surely appreciated.
Greg
rated
Thanks from someone who is thrilled when she manages to insert a picture or a link...don't know that I'll get "fancy" soon but like knowing how if I want to!
Thanks, Kent.
On another day when I am feeling smarter, I will tackle this and be fancy too!
BTW, have you noticed the new look to the cover? Many more cover stories and an elongated activity feed for new posts and rated stories? Looks good.
rated and always appreciate your help in these matters.
Holy cow, you made a poem out of that. Gifted, you are.
It was necessity, sandra. The instructions for centering looked stupid and hard to understand in prose. I dunno if you know the old elephant joke about “how do you make a statue of an elephant?” for which the answer is “get a big block of stone and chip away everything that doesn't look like an elephant.” I often feel like that's how I write... just following the constraints until the only possible thing to write falls out as the natural result. In this case it was a pretty industrial grade poem that I chiseled out, but I figured it'd do for the purpose—and perhaps inspire some people to do better. But I'm glad if you found it fun.
Thanks! I will make myself try some of these. (I am very non-techie, but willing to learn.)
Will file this and even though it seems daunting to my ADD self, I do so appreciate the effort you made here, step-by-step.
I am back reading again and am still impressed. I can't thank you enough. Now, If could just write a comment without misspelling it........
Yeah, I wish I could tell you it was as easy as style="Misspellings: None;" but alas misspellings are beyond the scope of what I'm discussing here. Sorry!
Thanks for all the plain English decoding!

I also found one other handy work-around last night. If you use a blog platform (e.g. TypePad) that shows a post in both HTM and Rich Text, you can display the HTM version, and copy/paste the code right into your blog at OS [using the HTM option]. It will come up on the OS side with all formatting (and images) intact, where you can preview and tweak before you hit "publish."

I don't know if this also works with other blog platforms or offline editing tools, but I imagine it should. This little discovery may save me a ton o' tme, but I will keep your post as a great crib sheet too ;->
Nina, I imagine it works in some and not others. I didn't mention it in my post because I wasn't sure where it would and wouldn't work. There are ways that style info can be attached in other systems that don't involve direct use of the style attribute, though, so it might lose some aspects when moving things about. It really depends on how the particular program works. But thanks for mentioning it as an option—it can't hurt to try! It might indeed save some people some time.
The latest, greatest example of how helpful and unselfish the OS community can be. Technical writing for non-technical readers, who's have thought?

Kudos.
Thanks Kent - tutorials are always appreciated, especially when they're clear and include lots of examples like this one.
Thank you for taking the time to explain this. I don't have much faith in my HTML abilities, but I'm going to try this sometime when I'm feeling patient ; )
Paris, Open Salon aggressively “tidies” your HTML and CSS, so that many things you might want to do will get removed. This is needed for safety, efficiency, and overall look-consistency. So before you assume anything really elaborate will work, you should just try it. I find, for example, it even does silly rewrites like removal of the final semicolon in things like: style="width: 100px; height: 100px;", which becomes style="width: 100px; height: 100px". I'll be surprised if fancy mouse events work but I've not tried. Note, too, that nothing (that I know of) gives you access to script definition areas or real style sheets; the only CSS you can do is what actually fits in style attribute on an element.
I usually just put the big images on another site and then upload a low-res image to Open Salon, with a regular anchor to an offsite html page so that people can go to that page to see the bigger images. See, for example, my blog post “Life's Scattered Moments.” You can use your browser's View Source to see how the HTML is set up if you're ambitious.
Great stuff Kent, thanks for posting.
This looks amazing... but if this is college... I am still in elementary school. Wow.
Some very helpful tips here. Now help me figure out how to code a simple table in OS. I've tried using an HTML editor (arachnophilia), and am currently googling for some CSS tips.
I've managed to copy/paste some code that does what I want in the OS blog editor, but when I save and preview, all table formatting is gone.
:-(
You seemed like a good candidate user to ask here. My HTML skills are basic, but I'm a quick learner.
Oh yeah.... rated!
OS removes table markup. There are some things you can do with CSS but not enough, I think. Unless I've overlooked something. I saw someone recently screenshot a table from elsewhere and then paste it in as a picture—that's probably the easiest thing.
That verifies my own experimentation so far, so I think you're right. Not sure how "easy" it will be, given the size of the table, but I'll either figure it out or come up with a different way to present the information. Maybe less detail in the post, with an external link...? (Just thinking out loud).
Thanks so much for responding Kent.
Yowza cool beans Kent! I'm going to experiment and fool around with this, but to be honest I hardly use the html stuff I learned in the mid 90s anymore. All that a href and frames or no frames...

I should be more studious, at the the very least I'd like to be able to post stuff so that extended dialogue is tabbed in. So close, and yet so far.

I know, I just need to concentrate...
Hi, AB. Sometime I'm planning (and have partly written) an HTML tutorial to teach/remind people of the basics of that. Tabbed/navigation stuff is beyond the scope of what you can do, probably, though, because OS runs a program that reads your HTML and removes most of it, leaving only certain kinds of markup that they want you to be able to use. So things like tables and various kinds of active navigation tools are hard to squeeze in. Still, there's a lot that can be done with what they give. Thanks for stopping by anyway. If you have questions, do ask.
Kent, I used this post to meet a goal of coloring text, which I used to do easily at my other hangout site. Here's the result, and thanks!

http://open.salon.com/blog/zumalicious/2009/05/15/orgasmic_explosion_of_colorific_madness

Next, I'm going to play with the fancy boxes and indentation!
Well, it makes no sense at all to me, but it sure looks cool!!!
An excellent introduction to some basics CSS functionality. Another great thing these days is that with the advent with IE 8 we will actually see some beginnings of compliance with web standards by most browsers (well..by IE in particular). Now web designers like myself just need everyone to stop using old browser versions...
Kent - I was looking at your links and saw the posts of tech advice on OS. This is exactly what I need! Thanks so much for sharing your expertise - it is very generous of you. I have been very frustrated lately trying to polish up my posts. Can't wait to try your formating techniques. THANK YOU
Thanks again, Kent!

Lezlie