CSS Basics

By Xah Lee. Date: . Last updated: .

Cascading Style Sheet (CSS) is a markup language that allows you to specify appearances of HTML. This page shows you the practical basics.

CSS is used for HTML. If you don't know HTML, see: HTML Tutorial .

Adding CSS to HTML

3 ways to add CSS.

CSS for Single HTML Tag

If you want to specify a style only for one particular HTML tag in a HTML file, you can embed a attribute style=val inside the tag, like this:

<p>… this is <span style="color:red">RED</span></p>

CSS for Single HTML File

If you want to use CSS only for a single file, you can embed it in HTML file's header using the <style> tag, like this:


p {color:red}

<title>My Title</title>


This will make all “p” (paragraph) elements red.

CSS for Entire Site

To add CSS in your HTML file, do like this:

<link rel="stylesheet" href="mystyle.css" />
<title>My Title</title>

In the above example, whenever the HTML file is loaded, it will load the file mystyle.css. The <link > line must be inside the “head” tag.

If you use the above for all your files, then you can control your whole site's appearance by modifying the single CSS file.

CSS Syntax

CSS source code looks like this:

p {

li {

blockquote {

img {
  border:solid thin black;

/* basic table style with border */
table.xyz {
  border:1px solid black;

table.xyz th, table.xyz td {
  border:1px solid gray;

In the above example, it says that any “p” tag's content (a paragraph) will have a line height that's 1.5 times of default, and has a width of 40 “em” (“em” is the font size), and the font used will be sans-serif font such as Arial. [see Common Web Fonts]

Similiarly, other tags's appearance are also specified.

In general, CSS code has lines like this:

selector { property_name_1 : value_1; property_name_2 : value_2; property_name_3 : value_3; … }

The selector basically means the tag matching syntax. It is technically called “selector”.

Semicolon is used as separator, and is optional for the last item.

Space or line return has no meaning, unless they are inside a quoted string such as "Courier New".

code {font-family:"DejaVu Sans Mono", monospace;}

Tag Matching Syntax

The tag matcher is often just the tag's name, but it can also be specified for tags that have a particular “class” attribute, or tags with a particular “id” attribute, or tags that are child of another tag, and others. For example, if you have <p class="warning"></p> for any important paragraph, you can make all such paragraph red by p.warning {color:red}.

/* most common examples of CSS selector syntax */

p {color:red} /* match any 「p」 tag */

p.xyz {color:red} /* match p tags that have 「class="xyz"」 */

.xyz {color:red} /* match any tags that have 「class="xyz"」 */

#xyz {color:red} /* match the tag that has 「id="xyz"」. Note, id must be unique per html page */

div > p {color:red} /* match 「p」 that's direct child of 「div」 */

div p {color:red} /* match 「p」 that's nested in 「div」 however deep */

span.booktitle {color:red}  /* match 「span」 tags that has 「class="booktitle"」 */

CSS is really simple. 90% of CSS can be mastered in just a day. Complete mastery of CSS will usually take a year or more of experience.

For a detailed tutorial on tag matching, see: CSS Tag Matching (Selector) Syntax .

JS in Depth





Text Decoration

Box Model

Box Decoration




Line Wrap