Tuesday, October 21, 2008

Knowing the basics of CSS (Cascading Style Sheets)

If you want to know more about CSS and how to use it, there are many great resources out there that can help you get started: I would start with http://www.w3schools.com/Css/default.asp

Here are the basics that you will use:

Syntax
selector {property: value;}

External Style Sheet - (Added within the <head> tags)
<link rel="stylesheet" type="text/css" href="style.css" />

Internal Style - (Added within the <head> tags)
<style type="text/css">
selector {property: value;}
</style>

Inline Style- (Added within the <body> tags)
<tag style="property: value">

Or use the following table with some of the most basic Properties. I am using the .MS-Main class as a reference for all of the examples below.

Properties

Example

Other Values

Backgrounds

   

background-color

.MS_Main{
background-color:  #000000
}

black
rgb(0,0,0)
transparent

background-image

.MS_Main{
background-image:  url(imgname.gif);
}

none

 

background-position

.MS_Main{
background-position:  top left;
}

top center
top right
bottom right
x% y%
xpos ypos
(Many Others)

background-repeat

.MS_Main{
background-repeat:  no-repeat;
}

repeat
repeat-X
repeat-Y

background-attachment

.MS_Main{
background-attachment: fixed;
}

scroll

Borders

 

 

border

.MS_Main{
border:  1px #000000 solid;
}

none

Element Position and Visibility

 

 

display

Note:  You will never use display none for the body of you website.

.MS_Main{
display: none;
}

Block
(Many others)

visibility

.MS_Main{
visibility: hidden;
}

visible
collapse

position

.MS_Main{
position: relative;
top: 10px;
left: 10px;
}

static
absolute
fixed

Heights and Widths

 

 

height

.MS_Main{
height: 400px;
}

auto
length (px)
%

width

.MS_Main{
width: 980px;
}

auto
length (px)
%

Fonts and text treatments

 

 

Font-family

.MS_Main{
Font-family: arial, sans-serif;
}

Times, Serif
(many Others)

Font-size

.MS_Main{
Font-size: 12pt;
}

10px
1.1em
x-large

Font-style

.MS_Main{

Font-style: italic;

}

normal

oblique

Font-weight

.MS_Main{
Font-style: bold;
}

normal
bolder
lighter
100
(Many Others)

Text-align

.MS_Main{
Text-align: center;
}

left
right
justify

Text-decoration

.MS_Main{
Text-decoration: underline;
}

none
overline
line-through
blink

Line-height

.MS_Main{
Line-height: 20px;
}

normal
number
length (px)
%

Spacing of Elements

 

 

Padding

.MS_Main{
Padding: 20px;
}

padding-top
padding-left
(Many Others)

Margin

.MS_Main{
margin: 10px;
}

margin-top
margin-left
(Many Others)

Pseudo-classes

 

 

Hover

Note: you apply this pseudo class after the class name to achieve a hover state effect.

.MS_Main:hover {
Background-color: #CC0000;
}

Transparencies

Note: You can make a class’s opacity to give it a transparent look.

.MS_Main: {
filter:alpha(opacity=50);
-moz-opacity:0.5;
-khtml-opacity: 0.5;
opacity: 0.5;
}

!Important tag

Note: Sometimes your css will not override hardcoded inline styles or ids so you can simply force the style with the !important tag.

.MS_Main{
Background-color: #CC0000 !important;
}

Differences between Classes and ID's

A class is referenced in the HTML as the following:
<td class="ms-main"></td>

Notice the class does not have the "." period in front of it. To add style to this class you would use the following syntax:
.ms-main {color: red;}

A ID is referenced in the HTML as the following:
<DIV ID="ms-main"></DIV>

To add style to this class you would use the following syntax:
#ms-main {color: red;}

So if you can remember that Classes use (.) periods and ID's Use (#) Pound signs you should be golden.

1 comment:

Mike said...

Now if only MSFT would implement a decent CSS solution in SharePoint we would all be happy :)

Imho they really should have let the team that did the CSS implemtation in CMS do it instead of using the CSS from SPS... It's still a mess :(