Sie sind auf Seite 1von 8

What is CSS?

CSS stands for Cascading Style Sheets Styles define how to display HTML elements Styles are normally stored in Style Sheets Styles were added to HTML 4.0 to solve a problem External Style Sheets can save you a lot of work External Style Sheets are stored in CSS files Multi le style definitions will cascade into one

Styles Solve a Common Problem HTML tags were originally designed to define the content of a document. They were su osed to say !This is a header!" !This is a aragra h!" !This is a ta#le!" #y using tags like $h%&" $ &" $ta#le&" and so on. The layout of the document was su osed to #e taken care of #y the #rowser" without using any formatting tags. 's the two ma(or #rowsers ) *etsca e and +nternet Ex lorer ) continued to add new HTML tags and attri#utes ,like the $font& tag and the color attri#ute- to the original HTML s ecification" it #ecame more and more difficult to create .e# sites where the content of HTML documents was clearly se arated from the document/s resentation layout. To solve this ro#lem" the .orld .ide .e# 0onsortium ,.10- ) the non rofit" standard setting consortium" res onsi#le for standardi2ing HTML ) created ST3LES in addition to HTML 4.0. 'll ma(or #rowsers su ort 0ascading Style Sheets.

Style Sheets Can Save a Lot of Work Styles sheets define H4. HTML elements are to #e dis layed" (ust like the font tag and the color attri#ute in HTML 1.5. Styles are normally saved in external .css files. External style sheets ena#le you to change the a earance and layout of all the ages in your .e#" (ust #y editing one single 0SS document6 0SS is a #reakthrough in .e# design #ecause it allows develo ers to control the style and layout of multi le .e# ages all at once. 's a .e# develo er you can define a style for each HTML element and a ly it to as many .e# ages as you want. To make a glo#al change" sim ly change the style" and all elements in the .e# are u dated automatically. Multiple Styles Will Cascade into ne

Style sheets allow style information to #e s ecified in many ways. Styles can #e s ecified inside a single HTML element" inside the $head& element of an HTML age" or in an

external 0SS file. Even multi le external style sheets can #e referenced inside a single HTML document. Cascadin! rder

What style will be used when there is more than one style specified for an "#ML element? 7enerally s eaking we can say that all the styles will !cascade! into a new !virtual! style sheet #y the following rules" where num#er four has the highest riority8 %. 5. 1. 4. 9rowser default External style sheet +nternal style sheet ,inside the $head& tag+nline style ,inside an HTML element-

So" an inline style ,inside an HTML element- has the highest riority" which means that it will override a style declared inside the $head& tag" in an external style sheet" or in a #rowser ,a default value-. $ote% +f the external style sheet link is laced #elow the internal style sheet in HTML $head&" the external style sheet will override the internal style sheet. Syntax The 0SS syntax is made u of three arts8 a selector" a ro erty and a value8 selector : ro erty8 value; The selector is normally the HTML element<tag you wish to define" the ro erty is the attri#ute you wish to change" and each ro erty can take a value. The ro erty and value are se arated #y a colon" and surrounded #y curly #races8 #ody :color8 #lack; $ote% +f the value is multi le words" ut =uotes around the value8 :font)family8 !sans serif!; $ote% +f you wish to s ecify more than one ro erty" you must se arate each ro erty with a semicolon. The exam le #elow shows how to define a center aligned aragra h" with a red text color8 :text)align8center>color8red;

To make the style definitions more reada#le" you can descri#e one ro erty on each line" like this8

: text)align8 center> color8 #lack> font)family8 arial ;

&roupin! 3ou can grou selectors. Se arate each selector with a comma. +n the exam le #elow we have grou ed all the header elements. 'll header elements will #e dis layed in green text color8 h%"h5"h1"h4"h?"h@ : color8 green ;

#he class Selector .ith the class selector you can define different styles for the same ty e of HTML element. Say that you would like to have two ty es of aragra hs in your document8 one right) aligned aragra h" and one center)aligned aragra h. Here is how you can do it with styles8 .right :text)align8 right; .center :text)align8 center; 3ou have to use the class attri#ute in your HTML document8 $ classA!right!& This aragra h will #e right)aligned. $< & $ classA!center!& This aragra h will #e center)aligned. $< &

$ote% To a ly more than one class er given element" the syntax is8 $ classA!center #old!& This is a aragra h. $< & The aragra h a#ove will #e styled #y the class !center! '*B the class !#old!. 3ou can also omit the tag name in the selector to define a style that will #e used #y all HTML elements that have a certain class. +n the exam le #elow" all HTML elements with classA!center! will #e center)aligned8 .center :text)align8 center; +n the code #elow #oth the h% element and the element have classA!center!. This means that #oth elements will follow the rules in the !.center! selector8 $h% classA!center!& This heading will #e center)aligned $<h%& $ classA!center!& This aragra h will also #e center)aligned. $< & Bo $ # start a class name with a num#er6 +t will not work in Mo2illa<Cirefox.

'dd Styles to Elements with Particular 'ttributes 3ou can also a ly styles to HTML elements with articular attri#utes. The style rule #elow will match all in ut elements that have a ty e attri#ute with a value of !text!8 in utDty eA!text!E :#ackground)color8 #lue;

#he id Selector 3ou can also define styles for HTML elements with the id selector. The id selector is defined as a F. The style rule #elow will match the element that has an id attri#ute with a value of !green!8

Fgreen :color8 green; The style rule #elow will match the F ara% : text)align8 center> color8 red ; Bo $ # start an +B name with a num#er6 +t will not work in Mo2illa<Cirefox. element that has an id with a value of ! ara%!8

CSS Comments 0omments are used to ex lain your code" and may hel you when you edit the source code at a later date. ' comment will #e ignored #y #rowsers. ' 0SS comment #egins with !<G!" and ends with !G<!" like this8 <G This is a comment G< : text)align8 center> <G This is another comment G< color8 #lack> font)family8 arial ;

"ow to (nsert a Style Sheet .hen a #rowser reads a style sheet" it will format the document according to it. There are three ways of inserting a style sheet8 External Style Sheet 'n external style sheet is ideal when the style is a lied to many ages. .ith an external style sheet" you can change the look of an entire .e# site #y changing one file. Each age must link to the style sheet using the $link& tag. The $link& tag goes inside the head section8 $head& $link relA!stylesheet! ty eA!text<css!

hrefA!mystyle.css! <& $<head& The #rowser will read the style definitions from the file mystyle.css" and format the document according to it. 'n external style sheet can #e written in any text editor. The file should not contain any html tags. 3our style sheet should #e saved with a .css extension. 'n exam le of a style sheet file is shown #elow8 hr :color8 sienna; :margin)left8 50 x; #ody :#ackground)image8 url,!images<#ack40.gif!-;

Bo not leave s aces #etween the ro erty value and the units6 !margin)left8 50 x! ,instead of !margin)left8 50 x!- will only work in +E@" #ut it will not work in Cirefox or 4 era. (nternal Style Sheet 'n internal style sheet should #e used when a single document has a uni=ue style. 3ou define internal styles in the head section #y using the $style& tag" like this8 $head& $style ty eA!text<css!& hr :color8 sienna; :margin)left8 50 x; #ody :#ackground)image8 url,!images<#ack40.gif!-; $<style& $<head& The #rowser will now read the style definitions" and format the document according to it. $ote% ' #rowser normally ignores unknown tags. This means that an old #rowser that does not su ort styles" will ignore the $style& tag" #ut the content of the $style& tag will #e dis layed on the age. +t is ossi#le to revent an old #rowser from dis laying the content #y hiding it in the HTML comment element8 $head& $style ty eA!text<css!& $6)) hr :color8 sienna; :margin)left8 50 x;

#ody :#ackground)image8 url,!images<#ack40.gif!-; ))& $<style& $<head& (nline Styles 'n inline style loses many of the advantages of style sheets #y mixing content with resentation. Hse this method s aringly" such as when a style is to #e a lied to a single occurrence of an element. To use inline styles you use the style attri#ute in the relevant tag. The style attri#ute can contain any 0SS ro erty. The exam le shows how to change the color and the left margin of a aragra h8 $ styleA!color8 sienna> margin)left8 50 x!& This is a aragra h $< &

Multiple Style Sheets +f some ro erties have #een set for the same selector in different style sheets" the values will #e inherited from the more s ecific style sheet. Cor exam le" an external style sheet has these ro erties for the h1 selector8 h1 : color8 red> text)align8 left> font)si2e8 I t ; 'nd an internal style sheet has these ro erties for the h1 selector8 h1 : text)align8 right> font)si2e8 50 t ; +f the age with the internal style sheet also links to the external style sheet the ro erties for h1 will #e8

color8 red> text)align8 right> font)si2e8 50 t The color is inherited from the external style sheet and the text)alignment and the font) si2e is re laced #y the internal style sheet.

Das könnte Ihnen auch gefallen