A Brief Introduction to CSS
Sunday, June 8th, 2008
CSS stands for Cascading Style Sheet. It’s a mark up language used in all of today’s website designs which allows you to alter the way a website is formatted through a cascade of elements specified on a style sheet or ‘rule sheet’. A cascading style sheet is an external or internal ‘rule sheet’.
This rule sheet tells one or several pages of a website how to display information. With CSS, the way to style a website is endless. CSS also makes creating a multifunctional, user friendly and consistent website easy, even for a beginner.
Recognizing the basic form and function of CSS is key to adding style to the included elements. For every element in your website, you have a tag which separates each bit of information on a web page. For instance, you may want to add a paragraph. To begin this paragraph you must have a tag
The <p>tag in HTML stands for ‘paragraph’:
Example: <p>This is the first paragraph<p>
I’ll walk you through how CSS enables you to style one element as a simple example of how it works. Let’s say you want the text of your website to be red, with bold purple in some areas. In your style sheet, you must create a rule that will make the text red. First, come up with the name of the style. Since we are styling the text of a paragraph, we could call the style ‘ptext’. In CSS, a style is written with a period before the name, so on our style sheet it would read:
.ptext
As with the tag element, you will need something that marks the beginning and the end of a style or rule on your style sheet. We use what are called curly brackets to mark the beginning and end of a rule in CSS. Anything included within the curly brackets will be a command for the particular element of the website to which this style is connected.
This is how you would write the rule on your style sheet:
.ptext { }
There are a couple of ways you can include your command within the style sheet - inline style which means it’s on the same line and external style. With an inline style, your rules are included within the tag of your HTML document, instead of in an external style sheet.
For this example I will also add the style of font we would like to use - verdana. We are making the text red, so we need to be sure we include all the rules we want to make for the text. We also need to make certain that we spell everything correctly and include all the necessary elements of the rule. Even the smallest mistake will make the style malfunction.
This is how the style rule would look within an inline style. Remember the rules go within the tag of what you want to style:
<p class=”font-family: verdana; color: red;”>
When creating a style, you must also include a colon (:) and a semi colon (;). These determine the beginning and end of a rule within a style. Without these two punctuations added to the style, it won’t work.
An inline style can get confusing, especially when you start adding pages or get into more complex styles. Therefore, get into the habit of using an external style sheet, instead, and writing it like this:
.ptext {
font-family: verdana;
color: red;
}
As you can see from the above example, putting each rule of the style on a different line not only helps organize it, but also makes it easy to add and remove rules without spending too much time searching for the beginning and end.
Let’s go back to our HTML again:
<p class=”ptext”>This is the first paragraph</p>
As you can see by adding the style tag element to the p tag and labeling it ptext, the sentence takes on the commands outlined in the rule of the style sheet for .ptext.
Let’s try another example. This time we will keep the .ptext style rule but we also add another style rule for a second paragraph. Since we are adding paragraph #2 we will call the style rule for this paragraph .ptext2We will say we want the text in the 2nd paragraph to use the same font (verdana), but that the text will be purple, bolded, and larger than in the 1st paragraph.This is how the style rule will look on your style sheet for paragraph 2:
.ptext2 {
font-family: verdana;
color: purple;
font-weight: bold;
font-size: 18px;
}
Now, we add this element to the tag for the second paragraph. I’ll include the first paragraph and the second paragraph so you can see the differences;
The rule elements of the style have commanded the text of the second paragraph to follow the rules of the .ptext2 style.
Your CSS document should look nice and clean, like this:
Creating a style sheet can be quite easy if you use just a few basic elements and know how each style should be set up. The CSS rules I have included in this beginning explanation were just for text in paragraphs and will not necessarily apply to an entire webpage.
When using a style sheet, you can also create rules for multiple pages. Commanding the text on all pages to appear alike is an ideal way to create a cohesive look throughout the entire website.
Stay tuned for my next article, in which I’ll continue to explain the basic elements of CSS styling and touch on other important elements of a web page, such as how to set up an HTML webpage, which elements to include in your CSS and how to include the style sheet on the page so that the rules created in your style sheet cascade throughout your entire website.






















