Click here to Skip to main content
Click here to Skip to main content

CSS For Beginners

By , 27 Nov 2000
 

 

CSS stands for Cascading Style Sheets. This is a simple styling language which allows attaching style to HTML elements. Every element type as well as every occurance of a specific element within that type can be declared an unique style, e.g. margins, positioning, color or size. So you might consider these style sheets as templates, very similar to templates in desktop publishing applications. For example:

     body { background-color: white; color: darkblue;font-size: 10pt;
                       font-family: Arial; margin-left: 10%}

Linking and Embedding

There are many ways to link style sheets to HTML, each carrying its own advantages and disadvantages. New HTML elements and attributes have been introduced to allow easy incorporation of style sheets into HTML documents.

External style sheets

An external style sheet can be linked with any number of HTML documents by using <link> that is placed in the document HEAD. The tag's various attributes indicate things about the style sheet - the rel attribute the type of link (a style sheet); the type attribute the type of style sheet (always text/css); and the href attribute the location of the style sheet. This is a very convenient way of formatting the entire site as well as restyling it by editing just one file. For example:

     <HTML><HEAD>
     <LINK rel="stylesheet" type="text/css" href="basic.css">
     </HEAD>
     <BODY>  ...  </BODY>
     </HTML>

Once you have linked the style sheet to your page, you then have to create the style sheet. For example:

     body { font-family: Verdana, Helvetica, Arial, sans-serif;
                       color: darkblue; background-color: #ffeeff} 

If you saved the example above as a style sheet, then every page that links to it will have the specified styles. Files containing style information must have extension .css.

Embedded Style Sheets

If you have a single document that has a unique style, you can use an embedded style sheet. If the same style sheet is used in multiple documents, then an external style sheet would be more appropriate. A embedded style sheet is inside the HEAD element with the STYLE element and will apply to the entire document:

     <STYLE TYPE="text/css" MEDIA=screen>
     <!--
     body  { background: url(flower.gif) lightyellow; color: darkblue }
     .zn { margin-left: 8em; margin-right: 8em }
     -->
     </STYLE>

The required TYPE attribute is used to specify a media type, as is its function with the LINK element. You should write the style sheet as a HTML comment, that is, between <!-- and --> to hide the content in browsers without CSS support which would otherwise be displayed.

Importing Style Sheets

You can import a style sheet with CSS's @import statement:

     <STYLE TYPE="text/css" MEDIA="screen, projection">
     <!--
     @import url(http://www.tongchiu.com/gen.css);
     @import url(/product/file.css);
     table { background: yellow; color: #003366 }
     -->
     </STYLE>

The @import allows you to keep some things the same while having others different; and follows this syntax: @import url(gen.css);

Note: If more than one sheet is imported they will cascade in order they are imported - the last imported sheet will override the next last; the next last will override the second last, and so on. If the imported style is in conflict with the rules declared in the main sheet then it will be overridden.

Inline Style

Inline style is the style attached to one specific element. Any opening tag may take the style attribute:

     <P style="font-size: 10pt">.

To use inline style, one must declare a single style sheet language for the entire document using the Content-Style-Type HTTP header extension. With inlined CSS, an author must send text/css as the Content-Style-Type HTTP header or include the following tag in the HEAD:

     <META HTTP-EQUIV="Content-Style-Type" CONTENT="text/css">

Syntax

Basic syntax as follows:

     selector { property: value }

That is a property (such as color) followed by a colon (:) and a value. A property is assigned to a selector in order to manipulate its style. Examples of properties include color, margin, and font. The value is an assignment that a property receives. Multiple style declarations for a single selector may be separated by a semicolon. The following example defines the color and font-size properties for H1 and P elements:

     <HEAD>
     <TITLE>CSS Example</TITLE>
     <STYLE TYPE="text/css">
     H1 { font-size: x-large; color: darkred }
     P { font-size: 12pt; color: darkblue }
     </STYLE>
     </HEAD>

You can use grouping of selectors and declarations to decrease repetitious statements within style sheets. For example:

     H1, H2, H3, H4 { color: #666666; font-family: Arial }

Selectors

Selectors are used to associate style declarations with an element or elements. This is done by placing the declarations within a block (enclosed in {}) and preceding it with a selector. For example:

     p {color: #008000}
     div {color: #cccccc; font-size: 14pt}

Tag selectors

You can take any opening HTML tag and use it as a selector:

     h3 {color: red}

Class selectors

These allow you to give elements a particular name. For example:

     <P class="zn"> .... </P>

In a style sheet, The syntax as the below:

     P.zn { color: blue }

Or like this:

     .zn { color: blue }

Pseudo-class selectors

Pseudo-classes can be assigned to the A element to display links, visited links and active links differently. The anchor element can give the pseudo-classes link, visited, or active. A visited link could be defined to render in a different color and even a different font size and style. The sample style sheet might look like this:

     A:link    { color: red }
     A:active  { color: blue; font-size: 150% }
     A:visited { color: green }

ID selectors

These selectors are very similar to classes except there can only be one element with a given ID in a document. An ID selector is assigned by using the indicator "#". For example:

     #abc { text-style: bold }

To use an ID selector:

     <P ID=abc>Welcome to my website</P>

Note: IDs like classes they should be in lowercase, may not start with a number or contain spaces. This selector type should only be used sparingly due to its inherent limitations.

Span

This element may be used as a selector in a style sheet, and it also accepts the STYLE, CLASS, and ID attributes. Some examples of SPAN follow:

     <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN">
     <HTML><HEAD><TITLE></TITLE>
     <META HTTP-EQUIV="Content-Style-Type" CONTENT="text/css">
     <STYLE TYPE="text/css">
     <!--
     .zn { font-size: 28pt }
     -->
     </STYLE>
     </HEAD>
     <BODY>
     <P><SPAN CLASS=zn>These words could be big.</SPAN></p>
     <p><SPAN STYLE="font-family: Arial;font-size:12"> And these ones are different.</SPAN>.</P>
     </BODY></HTML>

Div

DIV (short for "division") is a block-level element that, in function, is similar to the SPAN. But DIV may contain paragraphs, headings, and tables. For example:

     <DIV CLASS=zn>
     <H1>Welcome</H1>
     <P>Hello World</P>
     <P>Welcome to my website!</P> 
     </DIV>

Properties

Color

You can declare a color as the following example:

     P {color: red}
     H2 { color: #000080 }
     LI {color: rgb(12%, 51%, 62%)}

Background

Background-color This sets the background color of an element. For example:

     BODY { background-color: white }
     H1   { background-color: #000080 }

Note:
1. To help avoid conflicts with user style sheets, background-image should be specified whenever background-color is used. In most cases, background-image: none is suitable.
2. Netscape 4.* does not color in the background of block elements if they are given a background color that is different from BODY - it does not color in the spaces between words. To avoid this, explicitly set border: none

Background-image Specified with background-image. For example:

     BODY { background-image: url(/images/cloud.gif) }
     P { background-image: url(http://www.internetcollege.com/bg1.gif) }

Background-repeat This states the tiling of the background image. The possible values include: repeat | repeat-x | repeat-y | no-repeat.

The repeat-x value will repeat the image horizontally while the repeat-y value will repeat the image vertically. For example:

     BODY { background: white url(candybar.gif);
     background-repeat: repeat-x }

In the above example, the image will only be tiled horizontally. IE only draws repeat-x to the right, and repeat-y down, not left and right and up and down as it should do.

Background This allows one or more of the properties to be specified in the order color, image, repeat, attachment, position. For examples:

     BODY       { background: white url(http://www.internetcollege.com/bg1.gif) }
     BLOCKQUOTE { background: #6699ff }
     P          { background: url(image/line.gif) #e2e9ee fixed }
     TABLE      { background: #ffeeff url(house.gif) no-repeat top center }

Fonts

Font-family This allows a specific font to be used. For example:

     P { font-family: Times }

You may specify a couple fonts separated by comma. In case that if your preferred font is not available, your second choice is used. For example, font-family: Times, Arial. Note that any font name containing whitespace must be quoted, with either single or double quotes. For example:

     P { font-family: "New Times Roman", Times, serif    }

Font-size This can be specified as a length, or one of the following keywords: xx-small, x-small, small, medium (initial), large, x-large, xx-large. For example:

     H2 { font-size: large }
     P { font-size: 10pt }
     LI { font-size: 80% }
     Table { font-size: small}

Note: Internet Explorer 3 and Netscape 4.* treat all relative units and % as relative to the element default rather than as relative to the parent element.

Font-style This defines that the font be displayed in one of three ways: normal, italic or oblique (slanted). For example:

     P {font-style: italic} 

Font-weight This is used for specifying the weight of the font that can be specified as normal (initial value), or bold. For example:

     P {font-weight: bold} 

It can also be specified as an absolute number, being one of 100, 200, 300, 400 (the same as normal), 500, 600, 700 (the same as bold), 800, or 900, where 100 is the lightest and 900 the most bold. For example:

     H1 { font-weight: 800 }

Font This may be used as a shorthand for the various font properties. For example:

     P { font: italic bold 12pt/14pt Times, serif }

This specifies paragraphs with a bold and italic Times or serif font with a size of 12 points and a line height of 14 points.

Text

Text-align The value can be left (initial value), right, center, or justify (aligns to both margins). Text-align only applies to block elements and is inherited. For example:

     H1          { text-align: center }
     P.newspaper { text-align: justify }

Text-decoration This allows text to be decorated through one of five properties:

underline, overline, line-through, blink, or the default, none. For example:
     A:link, A:visited, A:active { text-decoration: none }

Text-transform allows text to be transformed by one of four properties: none (initial value), lowercase, uppercase, or capitalize (capitalize the first letter of every word). It applies to all elements and is inherited. For example:

     H1 { text-transform: uppercase }
     H2 { text-transform: capitalize }

Margin

This property sets the margin of an element by specifying a length or a percentage. Each element can have four margins - left, right, bottom and top. These are defined by the margin-left, margin-right, margin-top, margin-bottom properties. For example:

     P {margin-left: 20px}

The margins can be specified for all four sides at once with the margin shorthand. Margins can be negative, and initially margins are 0.

     P {margin: 10px 12px 6cm 8pt}

This would give P a top margin of 10 pixel, a right margin of 12 pixel, a bottom margin of 6 pixel and a left margin of 8 pixel.

Rules

Inheritance Basically a selector which is nested within another selector will inherit the property values assigned to the outer selector. For example, a font defined for the BODY will also be applied to text in a paragraph.

! important A style can be designated as important by specifying ! important. A style that is designated as important will win out over contradictory styles of otherwise equal weight. A ! important statement like this:

     BODY { background: url(man.gif) white; background-repeat: repeat-x ! important }

The weight sort The weight sort sorts declarations according to their weight. Declarations can have normal weight or important weight. Declarations are made important by the addition of !important (or ! important). For example:

     P {font-size: 36pt !important} P {font-size: 16pt}

36 pt will result because that declaration has greater weight.

The order sort When two rules have the same weight, the last rule specified wins. Thus:

     P {color: red}
     P {color: green}

It would result in green.

Case sensitivity All CSS is case insensitive.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Nongjian Zhou
www.itechcollege.com
East Timor East Timor
Member

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionExcellentmemberjisalazar8 Feb '13 - 9:30 
Very comprehensive, thanks you very much. Smile | :)
QuestionThanksmemberVercik5 Nov '12 - 1:45 
I was looking for this information. Thanks.
QuestionA Five Star..memberBitla Phanindra18 Oct '12 - 0:45 
Really Good article for beginners.. Smile | :)
Newsthkmemberlyosen28 Sep '12 - 5:39 
THank for your code.. so much Smile | :)
GeneralMy vote of 5memberlyosen28 Sep '12 - 5:38 
thak so much
GeneralThanksmembertruelydeeoly201021 Mar '12 - 16:50 
Thank You
 
i want to try web design and i also want to know how CSS work.
So Thank you for your sharing
GeneralMy vote of 5memberNeha Thanka31 Oct '11 - 23:48 
Really good for beginners....
GeneralThanks for your useful postmemberMember 801177321 Sep '11 - 2:35 
Hey thanks. I've never known exact behavior of @import direction. But now I know the difference between @import and importing by link Smile | :)
GeneralMy vote of 5memberSChristmas16 Jun '11 - 4:39 
Nice article. Bookmarked 5!
Generalthis is a testmemberMember 394051 Mar '11 - 22:55 
<script>
alert('hai');
</script>
GeneralVoted 5memberInktpatronen11 Jun '10 - 12:28 
This is really very handy for everybody who wants to start with CSS. Pity I didn't found this 5 years ago. Poke tongue | ;-P
Inktknal inktpatronen printer

GeneralApplying multiple styles HTML elements and significance of "!important " attribute in forcing stylesgroupelizas15 Feb '10 - 0:59 
There can be multiple CSS classes and inline style applied to a single html element. By the default CSS rule, if same style attribute is set with different values on top of a single element, the closest attribute value will dominate in the out put at the browser end. !important is a special attribute that can break the rule, and can force a particular style irrespective of the hierarchy of all the styles applied.
 

Any suggestions are appreciated
http://www.mindfiresolutions.com/Applying-multiple-styles-HTML-elements-and-significance-of-important--attribute-in-forcing-styles-440.php[^]
Cheers,
Eliza

GeneralThanks as well!memberDennis8828 Sep '09 - 12:14 
I would like to add my thanks as well. This article was very helpful! As an html "old schooler" I'm a little uncomfortable with CSS but its stuff like this that really helps.
GeneralThanks for thismemberrampurhaat1 Jul '09 - 22:20 
This is an excellent start for a CSS newbie like me. I like the documentation and I am sure I am going to help me in future.
 
I always wondered how can one document (a CSS) be generic enough for all the HTMLs one has. I now know to some extent.
_________________
Far infrared sauna
GeneralThis is not for REAL beginnermemberMichael Moreno18 Nov '08 - 9:44 
Hello,
 
I am afraid but I have understood almost nothing to this enumeration of code and find it inadequate for the beginner that I am. For the developer who knows everything about CSS and needs a Memo it is probably fine.
 
As an example when we look at "Inline Style" we read:
Inline style is the style attached to one specific element...

.
 
A beginner like would think... mmm... inline means we probably have to declare this in the css file and that will be inlined by the compiler. But then why there is no name and why do we need a closing tag. That must mean this is not inline this is written in the html document but then this is not CSS. etc...
 
The whole article is completely confusing and not useful at all for beginner.
 
I would suggest you change the title to "CSS Memo" or something like that.
 
Best regards,
MM


QuestionDoes article require modernising (in light of new document types)?memberBloodBaz17 Nov '08 - 1:17 
If this article is planned to be used for the Beginner's Guide, should we modernise some of the items in it?
From what I can tell, it was last changed in 2000!
 
I suggest the following:
1) Make HTML tags lower case (to comply with modern document types such as XHTML
2) Consider closing simply HTML tags with "/>" where appropraite (for same reasons).
3) Double check that all attribute values are in quotes
 
It is tricky as the document type can affect all sample code and yet will require assumptions to be made on what doctype is being used by a beginner. What do people think?
 
God is REAL unless declared int

Generalhtml tagsmembervinigoel12 Sep '08 - 5:51 
hi..
i m in bba vth sem..i have to make a project on adventure sports ang have to make a website of 40 pages..i want to attach template to external style sheet so that the same can be uploaded on all the pages..
kindly help me..
thanks
vini goel
Questioncss and more then one dynamic tablesmemberpunu_r10 Sep '08 - 22:23 
I have a two different tables in an .asp file , i also have a css file for printing, but there is always a gap betwueen the two tables.
I am attaching the codes here . Please advise me on the topic.
 

the aspcode:
 
<!-- Use IE Conditional Comments to hide table from IE5.x -->
<!--[if gte IE 6]>
 
<table width="910" border="1">
<tfoot>
<tr>
<td width="161" align="center" class="subheading">PERIOD
<br>(1ST JAN TO 31ST DEC 2007)</td>
<td width="506"><p align="center" class="head">FORM NO.1 </p>
<p align="center" class="subheading">STATEMENT SHOWING DETAILS OF IMMOVABLE PROPERTY ON
<br>FIRST APPOINTMENT AND ALSO ON 1ST JANUARY OF EACH CALANDER YEAR<br>(eg. Lands,House,Shops,other buildings etc.)</p></td>
<td width="221"><p class="subheading">Salary Code:5658
<br>Place of Posting:Duliajan </p></td>
</tr>
</tfoot>
</table>
 
<table width="907" border="1">
<tr class="subheading">
<td width="142">NAME </td>
<td width="348"><%=rs_gen.fields("gn_name")%></td>
<td width="96">ORGANISATION</td>
<td width="293">&nbsp;<%=rs_gen.fields("gn_orgnam")%></td>
</tr>
<tr class="subheading">
<td>DESIGNATION</td>
<td><%=rs_gen.fields("gn_desig")%></td>
<td>GRADE</td>
<td>&nbsp;<%=rs_gen.fields("gn_grade")%> &nbsp;&nbsp; &nbsp;DEPARTMENT : &nbsp;<%=rs_gen.fields("gn_dept")%></td>
</tr>
<tr class="subheading">
<td>DATE OF JOINING </td>
<td>&nbsp;<%=rs_gen.fields("gn_djoin")%></td>
<td>BASIC PAY </td>
<td><%=rs_gen.fields("gn_basic")%></td>
</tr>
</table>
<![endif]-->
AnswerRe: css and more then one dynamic tablesmemberBloodBaz17 Nov '08 - 1:10 
Before I say anything else, I should point out that your conditional comments will be hiding the table from non-IE browsers (such as Mozilla Firefox, Opera etc.). This is probably not what you want.
 
<!-- Use IE Conditional Comments to hide table from IE5.x -->
<!--[if gte IE 6]>
...
<![endif]-->
 
Secondly. You say you have a CSS for printing. Is it when printing that you are getting the unwanted gap between the tables? or just on Screen?
 
The normal way to remove any gap between elements (including tables) is to set margin: 0 on the elements.
In your case:
 
TABLE
{
    margin: 0;
}
 
I would suggest that you add an id to each TABLE element (e.g.
<table id="table1" width="910" border="1">
..
<table id="table2" width="910" border="1">
...
 
then add a stylesheet as follows:
TABLE#table1
{
    margin-bottom: 0;
}
TABLE#table2
{
    margin-top: 0;
}
Remember, if you are going to assign any stylesheet value to a non-zero value, you must state the units also e.g.
TABLE#table1
{
    /* e.g. one of the following */
    margin-bottom: 5px;   /* Pixels */
    margin-bottom: 1.2em; /* Ems */
}

 
God is REAL unless declared int

Generalshilan2003@yahoo.commemberhesarak1366 Mar '08 - 11:22 
very very good
GeneralCss in Mozilla FireFox Browsermembertanishka30 Oct '07 - 20:59 
Smile | :) hi
 
I create Css that applicable for the Internet Explorer 7.Then I Change the Browser to Mozilla Firefox then my Css Not working.Any Changes required if we use Mozilla Any one know pls tell me very urgent
 
swapna
GeneralRe: Css in Mozilla FireFox Browsermembersureshmuralirajan13 Jul '08 - 23:49 
Yes. It differs from browser to browser. You have to make some modifications....
Questioncan you add a section about compatiblity,please.memberAmerican_Eagle5 Jun '07 - 12:07 
thanks for this.
NewsNo titlemembernicemickey_mouse27 Apr '07 - 1:21 
Thank you!I am very pleasure about it.Cry | :((
 
where there is a will,there is a way!

Generala lot of great css ebooks in http://www.ebook5.comsussAnonymous12 Aug '05 - 0:35 
a lot of great css ebooks in http://www.ebook5.comBig Grin | :-D

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web03 | 2.6.130516.1 | Last Updated 28 Nov 2000
Article Copyright 2000 by Nongjian Zhou
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid