Custom Search

Thursday, January 18, 2007

learning CSS - part2

To start off our understanding of cascading style sheets, we're going to use a special line of CSS code that does something HTML alone could never do right… we're going to indent every paragraph automatically.

Here's the CSS code:

p { text-indent: 3em; }

As you can imagine, in its current form, the code does not but display p { text-indent: 3em; } on the screen instead of changing the way the paragraph tag behaves. We have several choices to implement this as an actual style, all of which are covered in this chapter:

  • Write it inline inside each and every tag
  • Place a at the beginning of the web page
  • Dedicate and link a CSS file and write it inside that file
  • Use @import to include it as portion of a page's CSS

Writing inline style inside each tag is one of the worst ways to design a CSS page, although there are a few rare circumstances where it may be appropriate. But to know CSS, you need to know how to do it, and it will also help you understand how the CSS system works.

In our example, p is the selector, that is, what is selected to follow your chosen style. What the web browser understands is that when it sees the tag p, before writing any content, it needs to indent the text 3em. (1 em is a size for laying out glyphs in print... the important thing to understand about 3em is that 3em is about 5 spaces, the typical indentation choice). text-indent is a property, and 3em is the value of that property.

All definitions in CSS follow this format:

selector { property: value; }

You can also include multiple properties and values for a single selector, merely separate them with a ;. If we wanted the paragraph text to be green, we could add color: green; after the text-indent: 3em;.

To write inline, we can start with

, the normal surroundings of a paragraph. Then, in the opening

tag, we'll add special code to say we want a style, merely

. Then, inside the quotes, we'll add all the properties and values we want this specific paragraph tag to have. So we'll make ours

This works just fine, as you can see below, however, we certainly do not want to write that for every paragraph we want to indent!

This paragraph is indented at
the very beginning by using an inline style.

(to be continued....)


No comments:

Post a Comment