What are the Basic CSS Syntax?

The basic CSS syntax is made up of following 3 parts: selector {property: value}

The selector is typically an HTML tag or element such as <p>, <table>, <h1>,<div> etc .

The following is a CSS code example of an internal style sheet. The selector (the <p> tag in this example) is made bold.

Many of the properties used in Cascading Style Sheets (CSS) are similar to those of HTML. Thus, if you are used to use HTML for layout, you will most likely identify many of the codes. Let us look at a example.

Let’s say we want a nice green color as the background of a webpage:

Using HTML we could have done it like this:
<body bgcolor=”#0000FF”>

With CSS the same result can be achieved like this:
body {background-color: #0000FF;}

As you will note, the codes are more or less identical for HTML and CSS. The above example also shows you the fundamental CSS model:

But where do you put the CSS code? This is exactly what we will go over now.

Applying CSS to an HTML document

There are three ways, you can apply CSS to an HTML document. These methods are all outlined below. We recommend that you focus on the third method i.e. external.

In-line (the attribute style)

One way to apply CSS to HTML is by using the HTML attribute style. Building on the above example with the green background color, it can be applied like this:

<html>
<head>
<title>Example<title>
</head>
<body style=”background-color: #0000FF;”>
<p>This is a red page</p>
</body>
</html>

Internal (the tag style)

Another way is to include the CSS codes using the HTML tag <style>. For example like this:

<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<style>
body {
background-color:’#FFCCDD’;
}
</style>
</HEAD>
<BODY>
</BODY>
</HTML>
Click here to view the output.

External (link to a style sheet)
The recommended method is to link to a so-called external style sheet(saved as a .css file) . Throughout this course we will use this method in all our examples.

An external style sheet is simply a text file with the extension .css. Like any other file, you can place the style sheet on your web server or hard disk.

For example, let’s say that your style sheet is named style.css. The trick is to create a link from the HTML document (say “abc.php”) to the style sheet (style.css). Such link can be created with one line of HTML code, using <link> element :
<link rel=”stylesheet” type=”text/css” href=” style.css” />

Notice how the path to our style sheet is indicated using the attribute href.
The line of code must be inserted in the header section (between the <head> and </head> tags) of theHTML code . Like this:

<html>
<head>
<title>My document</title>
<link rel=”stylesheet” type=”text/css” href=”style.css” />
</head>
<body>

This link tells the browser that it should use the layout from the CSS file when displaying the HTML file.
One CSS file can be used to control the layout of many HTML documents.In other words, t he really smart thing is that several HTML documents can be linked to the same style sheet.

This technique can save you a lot of work. If you, for example, would like to change the text color of a website with 500 pages, a style sheet can save you from having to manually change all 500 HTML documents. Using CSS, the change can be made in a few seconds just by changing one code in thecentral style sheet.