CSS Basics
Basics of CSS Revisited
Nov 29th
After awhile of going back and re-reading the previous CSS tutorials, I realized there was some flaws here and there and some things that should have been covered earlier but were not. So we’re going to start anew with a whole new CSS series that contains CSS2.1, CSS3 and HTML5 (including various hacks for webkit-based browsers, IE, FF, and Opera). Shall we begin?
This tutorial is the absolute basics of CSS and aimed at those that would like to learn about CSS but already have an understanding of HTML.
CSS isn’t a web markup language like HTML, XHTML, MathML and so on. CSS is a Cascading Style Sheet and is the backbone for styling web pages. CSS assumes that are you already have prior knowledge of HTML and understand how HTML works so it is critical that an understanding of HTML is present.
What and Why CSS?
Perhaps one might find themselves asking, “what is CSS and why do we need it?” The answer lies in technology. Perhaps you have made a website before using tables in HTML. Yes, this works but it is, however, deprecated (meaning it’s not generally practiced any more). Websites composed of HTML tables are a thing of the past because we now have rich client-side applications and complex server-side applications acting as blogging software, content management systems and online stores. Without CSS, these applications would be extremely difficult to update and maintain.
How to CSS?
Luckily for us, CSS is pretty straight forward and easy to learn! Let us get started with the basics!
CSS has the following structure:
1 2 3 4 5 6 7 8 9
selector {property:value;} single-line statement
selector { multi-line statement
property:value;
property:value;
}
Both examples follow the same general format. We have our selector, property and value.
- Selector – Is the part of the CSS that is used to specify what to style
- Property – Is the part of the CSS that states which method should be used to style the Selector
- Value – Is the part of the CSS that states how much to style or how specifically to style the Selector
Selectors
Selectors can have three different basic forms:
- HTML (Type) Selectors – “Global” Styles that apply to specific HTML elements
- Class Selectors – Can be used multiple times on a web page
- ID Selectors – Mostly used to indicate major sections of a web page
Note: Neither the Class or ID selector can start with a number or a special character, A-Z,a-z only for the first character!
1 2 3 4 5 6 7 8 9 10 11 12
.1bad-selector {property:value;}
.!bad-selector {property:value;}
.good-selector {property:value;}
#1bad-selector {property:value;}
#!bad-selector {property:value;}
#good-selector {property:value;}
HTML selectors, otherwise known as by Type selectors, are literally just HTML tags without angled brackets. For example,
1 2 3 4 5 6 7
a {
color:#ff9300; Controls color of anchor element
text-decoration:underline; controls how the text appears for the anchor
}
“a” represents it html tag to style, that is, <a href=”#”>Link</a> e.g. This is a link.
That link entitled, “This is a link” has two different properties that are controlling the behavior of the link. One property is controlling the color and a separate property is controlling how the text appears. For example:
We have a link here. The code that makes this link work is like the following CSS:
1 2 3 4 5 6 7
a {
color:#0071BB;
text-decoration:unerline;
}
We have our color (color:#0071BB) and we have the underline (text-decoration:unerline;).
We are choosing to type the a tag. We are changing the default color of the anchor and we are changing how the anchor will appear on a page by adding an underline to the anchor.
A breakdown of this would reveal the following relationship: Selector > Property > Value
The selector relies on the property and value to change how the selector will look.
Another example using HTML selectors would be the following:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
ul {
list-style-type:disc;
padding-left:20px;
}
ul li {
float:left;
margin-left:10px;
}
ul li a {
padding:5px 10px;
background-color:black;
color:white;
}
The CSS above styles the HTML below:
1 2 3 4 5 6 7 8 9
<ul>
<li><a href="#">Item 1</a></li>
<li><a href="#">Item 1</a></li>
<li><a href="#">Item 1</a></li>
</ul>
Although this example is a little longer and contains more code, there is still the relationship between the selectors, properties, and values. We still are choosing which HTML elements to style and we are using properties and values to style them.
Class Selectors
Class selectors are different than HTML selectors. We can use class selectors multiple times on a web page. Class selectors can be used on any HTML element that allows for the use of the class attribute in HTML. Class selectors are detonated by an end stop (.) before the name of the selector.
For instance,
1 2 3 4 5 6 7 8 9 10 11
<ul>
<li class="active"><a href="#">Item 1</a></li>
<li><a href="#">Item 2</a></li>
<li><a href="#">Item 3</a></li>
<li><a href="#">Item 4</a></li>
</ul>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
/*"Global" Styles*/
ul {
list-style-type:disc;
padding-left:20px;
}
ul li {
float:left;
margin-left:10px;
}
ul li a {
padding:5px 10px;
background-color:black;
color:white;
}
/*Class Selector*/
.active {
padding:5px 10px;
background-color:black;
color:white;
}
On the first li element we have <li class=”active”>. The class attribute is looking for the class selector named “active” and applying those styles to that element. We will discuss class selectors in greater detail in a later tutorial. The main idea for now is that they can be used multiple times in a web page and the selector can be called by using the “class” attribute on virtually any HTML element.
ID Selectors
ID selectors are traditionally used to indicate the major divisions between markup and are mostly used on div (or other block-level element) tags. ID selectors are denotated by the use of a pound (#) symbol.
For example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
<div id="header">
<div id="nav">
<ul>
<li><a href="#">Item 1</a></li>
<li><a href="#">Item 2</a></li>
<li><a href="#">Item 3</a></li>
<li><a href="#">Item 4</a></li>
</ul>
</div>
</div>
<div id="sidebar">
<h2>Joe Smith</h2>
<ul>
<li><a href="#">Item 1</a></li>
<li><a href="#">Item 2</a></li>
<li><a href="#">Item 3</a></li>
</ul>
</div>
<div id="content">
<p>Some text here</p>
</div>
<div class="clearfix"></div>
<div id="footer">
<p>Copyright © AtomicPages.net 2010. All Rights Reserved.</p>
</div>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
body {
margin:0 auto;
width:1000px;
font:11px sans-serif;
color:#fff;
}
.clearfix {clear:both;}
#header {
margin:0 auto;
display:block;
}
#nav {display:table;}
#nav ul {list-style-type:none;}
#nav ul li {
float:left;
padding-left:8px;
}
#sidebar {
width:200px;
float:left;
}
#content {
width:800px;
float:right;
}
#footer {display:table;}
Notice how the ID attribute for HTML works the same way as the class attribute. The ID attribute calls for the id selector with the given name and applies the styles to it. The <div id=”nav”>, for instance, is calling for the id selector names nav and applying the styles to it. The divs and the ids are also creating logical divisions on our web page e.g. header, nav[igation], sidebar, content and footer.
Comments
Comments are essential to creating any website or program. This allows us to understand what we are thinking or to make additional notes on a web page or program. This also allows for other people to easily see what we our intent is without having to guess.
CSS comments are similar to most multi-line comments in PHP and other programming languages.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*This is a single line comment*/
/*
*This document was created by AtomicPages.net
*/
body {
margin:0 auto; /*sets top + bottom margin to 0 and left + right margins to auto*/
width:1000px;
font:11px sans-serif; /*keep sans-serif for continuity across the site*/
color:#fff; /*do NOT change this value!!*/
}
Comments are useful for making annotations for programmers who don’t deal with the design of website very much, making your code easier to understand, and giving yourself or whoever created the style sheet credit!
Think you got it? Take the quiz and find out!
Basics of CSS – Part #10
Jul 6th
Today we’re going to talk about tables and how they can look significantly better when we add a little bit of CSS to them. If you need a quick refresher on tables, please click here.
Styling Tables
As mentioned above, styling tables is very useful and can instantly make a webpage come to life. The HTML default table is like the following:
| Item 1 | Item 2 |
|---|---|
| Item 1 | Item 2 |
| Item 1 | Item 2 |
Pretty boring if you ask me. Notice how the table has a background of transparent so it will take the color of any background element. The table has that border by default and it definitely could use some tweaking to make it look better.
We’re going to use some key properties:
- border-collapse
- border
The border property allows us to style all four corners of a border at once, or each side individually. There are no arbitrary shapes with borders. All elements have a clearly defined left, right, bottom, and top side to the element.

Notice how the square has an irregular shape and doesn’t appear to be a conventional square. We would call this a diamond shape or something along those lines. It’s actually a perfect square that was rotated 45° on a canvas. Notice how the diamond has a background since it is an image and that background has four sides: top, right, bottom, and left.
To see the border tutorial click here
Now that we have a firm understanding and we have refreshed our minds on the border property, we can fully style our tables!
If we add a little bit of CSS and tweak the table to our liking then it could very well look like this:
| Item 1 | Item 2 |
|---|---|
| Item 1 | Item 2 |
| Item 1 | Item 2 |
Although this dives into the world of pseudo-classes, which is discussed in the advanced css tutorial series, we know what our tables can potentially look like with a little bit of work.
Styling
Consider the following code:
1
table, td {border:1px solid #ff9300;}
This code would add a 1px border that is solid with the color #ff9300 (AtomicPages Orange) around the table and td (table cell) elements. The reason why we chose these elements is because the tr (table row) element does not have a border around it be default. (Usually applying a border color to a table would only mean applying it to the table element only and not the td element.)
This code will outline the table and td elements with a border that is 1px in width, that is solid and the color #ff9300. This can be useful if you want to quickly style a table quickly and easily.
| Item 1 | Item 2 |
|---|---|
| Item 1 | Item 2 |
| Item 1 | Item 2 |
border-collapse
The border-collapse property is a very useful property that collapses the default double border into a single border. For example:
1 2 3 4 5 6 7
table {
border-collapse:collapse;
border:1px solid #ff9300;
}
| Item 1 | Item 2 |
|---|---|
| Item 1 | Item 2 |
| Item 1 | Item 2 |
Notice how the border collapse property instantly gives the table a sharper and less “busy” look. The default border around the table and td elements ceases to exist and we are given a sharper look.
With a little padding (for spacing) for the td elements we can easily space out the table like the following:
1 2 3
th, td {padding:3px;}
/*use as an alternative for cellpadding*/
| Item 1 | Item 2 |
|---|---|
| Item 1 | Item 2 |
| Item 1 | Item 2 |
Using that same principle, we can apply background images and colors to the table, tr, th, and/or the td elements.
For example:
1 2 3 4 5 6 7 8 9
table {
border-collapse:collapse;
border:1px solid #ccc;
background-color:#dbdbdb;
}
| Item 1 | Item 2 |
|---|---|
| Item 1 | Item 2 |
| Item 1 | Item 2 |
We’ve seen how tables can look and we know what they look like by default — boring. Styling tables can be a great addition to any website whether it is made from tables itself or from a tableless markup using CSS.

Styling Tables
Size: 8.36 kB
Website Layout
Apr 16th
What is generally accept as a “good website layout”? A subject of debate, however, a good layout will have a clearly defined header area, sidebar (optional), content area, and footer area. It can look like the following:

The sidebar can be neglected, on the left side, the right side, or on both sides. The fact still remains that there is a clear cut header, sidebar, content area, and footer, however.
Note: the widths and height of these elements will differ of course.
Tables or CSS?
Starting with CSS2.1 in table designed websites started to phase out rapidly. This is because rich content management systems like WordPress, Drupal, and Joomla were being born and refined. The developers of these fine CMSs did not use tables to do their bidding for their rich admin panels and client-side website interactivity. Today, tables are nearly deprecated when it comes to web layout. CSS is used to create tableless layouts. If you don’t know CSS and you want to learn CSS you can click here to learn it!
How to Start?
When I start a website I will work on my external style sheet first and foremost. I will set all of the link colors, define by background, etc… Like the following example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
html, body {
margin:0;
padding:0;
}
body {
margin:auto;
width:1000px;
background:#3b3b3b url (bg.png) repeat-x top center;
font-family: Tahoma, Arial, sans serif;
font-size:12px;
color:#fff;
}
a, a:visited {
text-decoration:underline;
color:#ff9300;
outline:none;
}
a:hover {
text-decoration:none;
color:#ff9300;
outline:none;
}
img {border:none;}
Once I have my foundation elements all set and ready to go I will create my ID selectors that I will use for the website. If you are unfamiliar with ID selectors or just want a refresher click here for basics or click here for id v. class selectors. The main ID selectors we want are the following:
- header
- sidebar (optional)
- content
- footer
The CSS could look like the following:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
#header {
height:75px;
background:#4c4c4c url(images/header-bg.gif) repeat-x;
border:2px solid #fff;
}
#sidebar {
float:left;
width:150px;
background:#DBDBDB url(images/sidebar-bg.gif) repeat-x;
}
#content {
float:right;
width:850px;
}
#footer {
clear:both;
height:75px;
background-color:#4c4c4c;
}
The HTML code to make this happen would be the following:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html;
charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<div id="header">
<p>Add content here</p>
</div>
<div id="sidebar">
<p>content here</p>
</div>
<div id="content">
<p>content here</p>
</div>
<div id="footer">
<p>content here</p>
</div>
</body>
</html>
We are using a div element that is a block level element so we can negate display:block; for the ID selector. It is common practice that we use div elements to call certain ID selectors since div, by default, does nothing.
Once we have the basic structure of the website we can add our custom styles, class selectors, additional class selectors, nested selectors, pseudo-classes, pseudo-elements, etc… Consider the following for the sidebar:
1 2 3 4 5 6 7 8 9 10 11
#sidebar.container {
background:#DBDBDB url(images/gradient.png) repeat-x top top;
width:125px;
max-height:250px;
overflow:hidden;
}
This creates a class selector called container which can only be used for the ID called sidebar. This container will be called multiple times in the sidebar (hence, the class selector) and will reside inside the sidebar since it’s a child element.
Want to see a working example and then some? Please download web-layout.zip where you can see multiple examples of web layouts each with a combined and their own individual style sheet so you can easily see what the CSS is doing to each layout.

web-layout.zip
Size: 14.43 KB
Basics of CSS Part – #9
Feb 3rd
Today we’re going to talk about list styles. Lists are a vital commodity to web site creation. Most side-navigation menus and inline menus are actually lists that are simply listing the things we want them to. From there, we can add styles to these lists and manipulate them however we please.
Lists
In HTML there are two types of lists:
- Ordered Lists <ol>
- Unordered Lists
Ordered Lists are usually marked with numbers or letters whereas Unordered Lists are usually marked with bullets or squares.
List Type
We can change how a list looks by using the list-style-type property. Consider the following code:
1 2 3
ul {list-style-type:circle;}
ol {list-style-type:upper-roman;}
The CSS code above will make all ul elements have a list style type of a circle and all ol elements have a list style type of upper roman numerals. The output will look like the following:
- Item 1
- Item 2
- Item 1
- Item 2
Properties
We can have a few properties for list types and those properties operate the same way as the background properties
| Property | Description | Values |
|---|---|---|
| list-style | This is the all-inclusive property where you can define all of the values | list-style-type list-style-image list-style-position |
| list-style-type | This property will define type of list style you wish to have in the <li> element. |
none disc circle square decimal decimal-leading-zero armenian georgian lower-alpha upper-alpha lower-greek lower-latin upper-latin lower-roman upper-roman hebrew cjk-ideographic hiragana hiragana-iroha katakana katakana-iroha |
| list-style-image | This property will allow you to define an image as the list marker. | url(image url) none |
| list-style-position | This property will allow you to specify whether you want the list to be inside or outside the content flow. | inside outside |
We can also narrow down the list-style and list-style-type properties for each list element.
Unordered Lists
For unordered lists we can use the following values:
- circle
- disc
- square
- none
Technically we can use any of the values listed in the table above, however, the purpose of the unordered list would cease to exist.
Ordered Lists
We can have many options for the ordered list. Out of the 23 values we posted for list-style-type, only three were solely for unordered lists. We can use the following values:
|
|
Using images with lists
Using images can spice up just a standard web page by a lot. It is very easy to create your own bullet, circle, square, or a small arrow pointing toward the item you are showing in the list. We can use the all-inclusive,
1 2 3 4 5
list-style:url ("image/bullet.png");
/*OR*/
list-style-image:url ("image/bullet.png");
This will replace the default bullet in the unordered list with an image of our choice.
To see these properties in action feel free to click on the download button below!

lists.zip
Size: 9.76 KB
Basics of CSS Part – #8
Nov 23rd
Today we’re going to continue our journey into the Box Model. If you aren’t familiar with this box model, you should read about it!
Padding
Padding — defines the space between the element border and element content and will be the same color as the background color of the element.
If we recall that in the box model we have margins > borders > padding > content so Padding is tertiary. Let’s assume you have an HTML paragraph element:
1 2 3 4 5 6 7 8 9 10 11
<!--No Padding-->
<p style="background-color:#fff;">Lorem ipsum dolor sit amet,
consectetur adipiscing elit.</p>
<!--With padding-->
<p style="background-color:#fff; padding:10px;">Lorem ipsum dolor
sit amet, consectetur adipiscing elit.</p>
- Lorem ipsum dolor sit amet, consectetur adipiscing elit.
-
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
So what is different from the first line and the second line? The first line is without padding so the white background only fills to the end of the text instead the width of the box. The second line, however, spreads across the entire width of the box and it is a padding of 10px on all sides.
Padding works in very similar ways as the margin property except the margins are always invisible and the padding is based on the background of the element you are adding padding to.
Properties
Like margins, there are five possible properties we can use. They are the following:
- padding-bottom
- padding-left
- padding-top
- padding-right
- padding
The first four properties define each side for padding. For example, if you wanted to adjust the bottom and right padding for a certain element you could use the following:
1 2 3 4 5 6 7
p.element {
padding-bottom:3px;
padding-top:7px;
}
This will change the bottom padding of the element by 3px and the top padding of the element by 7px. Example:
- Lorem ipsum dolor sit amet, consectetur adipiscing elit.
-
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Remember that padding is basically an extension of the background of an element and you also need to take into account any other elements (whether above, below, left or right) that might get in the way of this padding. In theory, you could make an entire HTML and CSS website using only margins, borders, padding, and content.
Values
- em, px, pt – numeric values e.g. 1em, 15px, 2pt
- percent % – percent of the containing element e.g. 10% of 500px is 50px.
1 2 3 4 5 6 7 8 9
span.padding {
padding-top:2em;
padding-bottom:10px;
padding-right:16%;
}
All of the above are valid ways to define padding of our span element and class of padding.
Padding – All Inclusive
The all-inclusive padding property works the same way as the all-inclusive margin property. This singular padding property will define padding how ever we need it to using one property instead of writing out each side.
1
padding:15px 30px 45px 60px;
- top padding – 15px
- right padding – 30px
- bottom padding – 45px
- left padding – 60px
This example has four values for the padding property. The values start with padding-top and then go clockwise to padding-left. So if you think of clock and a new day, which always starts at 12 or the top of the clock, and then rotate clockwise the next 90° angle is at the right, then the 180° is at the bottom, and then the 270° is at the left. So it’s like looking at the clock at every 90° angle you get top, right, bottom, left.
1
padding:15px 30px 45px;
- top padding is 15px
- right and left padding is 30px
- bottom padding is 45px
This might come as a surprise to us because you would think they would group the Top and [bottom and left] segments separately, however; we can see in this example that the padding-top is 15px and the padding-bottom is 45px. The left and right padding is grouped together because they just belong together. If someone asks, “when I say a word you think of something that is opposite to that” and they ask, “up” you’ll probably say “down” off the bat.
1
padding:15px 30px;
- top and bottom padding 15px
- right and left padding 30px
This might be more clear since there are only two values here. Naturally, we’d think Top and [right and left] so the top and bottom padding will have a value of 15px whereas the right and left padding will have a value of 30px.
1
padding:15px;
- top, right, bottom, and left padding is 15px
One single value for the padding property will set all padding to that value, in our case 15px.
