Pages

Showing posts with label CSS Tutorials. Show all posts
Showing posts with label CSS Tutorials. Show all posts

Friday, October 29, 2010

Introduction to CSS

CSS stands for Cascading Style Sheets, which is a relatively new way to control the way your webpages look. It is used as an extension to HTML. CSS works in Internet Explorer 3+ Netscape Navigator 4+ and Opera 3.5+ as well as a few other, less popular, browsers. But enough about that, here's how you use it.

This is what a CSS tag looks like:
selector {property:value; property:value; property:value}
Selector is the element you wish to define, usually an HTML tag. Property is the attribute that will be changed, and value is how the attribute will be changed. The property and value are surrounded by {}, and if there is more than one property, they are seperated by semicolons. Here is an example:
body {font-family:Arial; font-size:10pt; font-style:italic}
The above code is like saying <font face="Arial" size="2"><i> in HTML, except it applies to all text within the body tags.



There are a few ways to use CSS. The way it's used most often is to embed the CSS code into the head of a HTML page. It looks like this:
<head>
<style type="text/css"><!--

CSS tags and information go here

--></style>
</head>
Note the comment tags <!-- --> surrounding the stylesheet attributes. These ensure that the attributes remain hidden from browsers not supporting style sheets.

Another way to use CSS is to create a seperate style sheet and link all your pages to it. To link to the style sheet, you would put this code between the <head></head> tags.
<link rel="stylesheet" href="style.css" type="text/css">
All you need to change here is href="style.css" by changing "style.css" to whatever your stylesheet is named. Be sure it ends with .css, or it won't work. The style sheet itself consists only of CSS tags, with no HTML or <style> tags.

It is best to use a linked style sheet when you want the same CSS to apply to more than one page. That way, when you wanted to change the look of your website all you would need to do is change the CSS file, and all the pages would change to suit.

The third way to use CSS is to inline it with certain tags, so that it doesn't affect the whole HTML page, just the tag you apply it to. Here is an example:
<font style="letter-spacing:5px; text-decoration:underline">
Now that you know how to use CSS, you'll want to know what you can do with it. A list of common CSS tags and properties can be found here.

Font & Text

PropertyValuesSample
font-familyfamily-name, generic-family{font-family:Verdana, MS Sans Serif}
font-sizepoint // pixel // percentage{font-size:12pt}
font-stylenormal / italic{font-style:italic}
font-variantnormal / smallcaps{font-variant:small-caps}
font-weightnormal / bold{font-weight:bold}
letter-spacingnormal / pixel{letter-spacing:2px}
word-spacingnormal / pixel{word-spacing:5px}
text-decorationnone / underline / overline / line-through{text-decoration:line-through}
vertical-alignbaseline / sub / super / top / text-top / middle / bottom / text-bottom // percent{vertical-align:sub}
text-transformcapitalize / uppercase / lowercase / none{text-transform:uppercase}
text-alignleft / right / center / justify{text-align:justify}
text-indentpixel // percent{text-indent:20px}
line-heightnormal / pixel // percent{line-height:5px}

Color & Background

PropertyValuesSample
colorcolor // hexidecimal value{color:orange}
background-colorcolor // hexidecimal value / transparent{background-color:green}
background-imageurl / none{background-image:url(image.jpg)}
background-repeatrepeat / repeat-x / repeat-y / no-repeat{background-repeat:no-repeat}
background-attachmentscroll / fixed{background-attachment:fixed}
background-positiontop / center / bottom / left / center / right // pixel{background-position:top center}

Box (table, paragraph, form, etc)

PropertyValuesSample
margin-topauto / pixel // percentage{margin-top:5px}
margin-rightauto / pixel // percentage{margin-right:0px}
margin-bottomauto / pixel // percentage{margin-bottom:5%}
margin-leftauto / pixel // percentage{margin-left:auto}
marginauto / pixel // percentage{margin:10px}
padding-topauto / pixel // percentage{padding-top:5px}
padding-rightauto / pixel // percentage{padding-right:0px}
padding-bottomauto / pixel // percentage{padding-bottom:5%}
padding-leftauto / pixel // percentage{padding-left:auto}
paddingauto / pixel // percentage{padding:10px}
border-widththin / medium / thick // pixel{border-width:10px 5px 10px 5px}
border-colorcolor // hexidecimal value{border-color:#130919}
border-stylenone / solid / double / groove / ridge / inset / outset{border-style:ridge}
borderborder-width / border-color / border-style{border:thin solid #FFFFFF}
floatnone / left / right{float:none}
clearnone / left / right / both{clear:left}

Lists

PropertyValuesSample
list-style-typedisk / circle / square / decimal / lower-roman / upper-roman / lower-alpha / upper-alpha / none{list-style-type:square}
list-style-imageurl / none{list-style-image:url(img.gif)}
list-style-positioninside / outside{list-style-position:outside}
list-styletype // position // image{list-style: disk inside url(img.gif)}
displayblock / inline / list-item / none{display: inline}
white-spacenormal / pre / nowrap{white-space: nowrap}

Position

PropertyValuesSample
heightpixel // auto{height:20px}
widthpixel // auto{width:20px}
leftpixel // percentage // auto{left:130px}
toppixel // percentage // auto{top:10%}
positionabsolute / relative / static{position:absolute}
overflowvisible / hidden / scroll / auto{position:scroll}
visibilityvisible / hidden / inherit{visibility:hidden}
z-indexinteger{z-index:1}

Miscellaneous

PropertyValuesSample
a:link, a:active, a:hover, a:visitedvarious{color:red; font-weight:bold; text-decoration:none; border:thin solid black}
cursorauto / crosshair / default / hand / move / e-resize / ne-resize / nw-resize / n-resize / se-resize / sw-resize / s-resize / w-resize / text / wait / help{cursor:n-resize}

Classes

Using classes with your CSS can make customizing your pages ten times easier, because you can give different occurances of the same element different styles. Say you wanted every cell of a table to have a different color and font, like this:
pinkgreen
yellowblue


Instead of defining everything with HTML, we use CSS classes. Here is the code:

<table border="1"><tr>
<td class="pink">pink</td>
<td class="green">green</td>
</tr><tr>
<td class="yellow">yellow</td>
<td class="blue">blue</td>
</tr></table>


Then, in your header, you'd define what happens with each different class, like so:

<style type="text/css"><!--
.pink {background-color:#FF99FF;font-family:Times New Roman,Times}
.green {background-color:#99FF99;font-family:Courier New,Courier}
.blue {background-color:#99CCFF;font-family:Verdana}
.yellow {background-color:#FFFF99;font-family:Arial,Helvetica}
--></style>


Now everything you add class="pink" to will have a pink background color and Times New Roman font.

This can also be useful if you want different links to look different ways. For example, links in the menu to look one way, and links in the body/text to be different. In the style sheet of your page you'd usually have this:

a:active {color:pink}
a:visited {color:pink}
a:link {color:pink}
a:hover {color:blue}


If you had a few links you wanted to be a bit different, you'd add this:

a.menu:active {color:blue}
a.menu:visited {color:blue}
a.menu:link {color:blue}
a.menu:hover {color:pink}


Then add class="menu" to the links you'd like different, like this:

Text Rollovers

One of the most common ways to use CSS is to add effects to text links. Take this example:
This is the code that produces it:

<style type="text/css"><!--
a:link {color:color1; text-decoration:none}
a:active {color:color2; text-decoration:none}
a:visited {color:color1; text-decoration:none}
a:hover {color:color2; text-decoration:underline overline}
--></style>


How does it work?

a:link applies to unvisited links.
a:active applies to a link being clicked.
a:visited applies to visited links.
a:hover applies to a link with the pointer over it.

Apart from changing the color and text decoration, you can also make them bold {font-weight:bold}, italic {font-style:italic} or give them a border {border:thin solid green} or a background color {background-color:gray}.

Custom Cursors

You can also change which cursor will be used on your pages. Hover your pointer over the links below, the cursor should change with each one.
To change the cursor when it hovers over links, use the following code:

<head>
<style type="text/css"><!--
a:hover {cursor:n-resize}
--></style>
</head>


Change n-resize to whichever cursor you prefer. To change the cursor on a per-link basis, add the code to each link instead. Like this:

<a href="page.html" style="cursor:crosshair">click here</a>

It is also possible to have completely customized cursors. If you've downloaded or made your own original cursor that you'd like to show up on your page, use this code:

<head>
<style type="text/css"><!--
a:hover {cursor:url("custom.cur"), pointer}
--></style>
</head>


"custom.cur" is the relative URL of your custom cursor file, uploaded to your server space. "pointer" is the general cursor that will be used if the custom cursor is missing, or if this function isn't supported by a viewer's browser. Here is an example, with a custom cursor I made myself:

hover your pointer over this link