CSS Introduction<br />Colin Gourlay & Kevin Vanderbeken<br />
CSS<br />=<br />(the presentation layer)<br />
Today:<br />What is CSS?<br />Where can CSS be used?<br />CSS Syntax - selectors, properties & values<br />Selecting HTML ...
what is CSS?<br />
CSS = Cascaded Style Sheets<br />A way to customise the presentation of your HTML.<br />A language designed to allow us se...
why do we use CSS?<br />
better type & layout controls<br />less work<br />smaller documentsand faster downloads<br />
better type & layout controls<br />less work<br />smaller documentsand faster downloads<br />more accessible sites<br />
better type & layout controls<br />less work<br />smaller documentsand faster downloads<br />more accessible sites<br />re...
less work<br />smaller documentsand faster downloads<br />more accessible sites<br />reliable browser support<br />
smaller documentsand faster downloads<br />more accessible sites<br />reliable browser support<br />
a great example...<br />http://www.csszengarden.com<br />
where can you use CSS?<br />
inline styles<br />embedded style sheets<br />external style sheets<br />
<html><br />  <head><br />    <title>Document Title</title><br />  </head><br />  <body><br />    <h1 style="color: red;">...
inline styles<br />embedded style sheets<br />external style sheets<br />
<html><br />  <head><br />    <title>Document Title</title><br />    <style type="text/css"><br />      h1 { color: red; }...
inline styles<br />embedded style sheets<br />external style sheets<br />
<html><br />  <head><br />    <title>Document Title</title><br />    <link rel="stylesheet" <br />href="stylesheet.css" <b...
CSS syntax<br />
selectors, properties & values<br />
selector { property: value; }<br /> <br />selector {<br />     property1: value1;<br />     property2: value2;<br />     p...
h1 { color: orange; }<br /> <br />p {<br />     color: #000000;<br />     font-size: 12px;<br />     font-family: Arial;<b...
h1 {}<br /><h1>Introduction</h1><br />
#article {}<br /><p  id="article">...text...</p><br />
.tab {}<br /><li  class="tab">...text...</li><br />
inheritance & the cascade<br />(a.k.a. the BIG concepts)<br />
inheritance<br />
HTML<br /><p class="bold"><br />  <a href="http://www.google.com"><br />    Google<br /> </a><br />is an excellent search ...
document structure<br />
all elements contained within another element are its descendants<br />title, style and headare descendants of html<br />
a direct descendant is called a child element<br />bodyis a child of html<br />
a containing element is called the parent element<br />htmlis the parent of body<br />
the trail of parents leading back to the root are an element’s ancestors<br />p, body and htmlare ancestors of img<br />
all elements with the same parent are siblings<br />h1, p, p, h2, etc are siblings<br />
If we wanted all text elements to be shown in verdana font, we apply one rule to the <body><br />body{font-face: Verdana; ...
the cascade<br />
 the closer the style sheet is to the content, the more weight it is given<br />
rules can still conflict...<br />...but the cascade still applies<br />
HTML<br /><li class="myHappyShoes"><br />One of many happy shoes<br /></li><br /><li id="happyShoe" class="myHappyShoes"><...
about CSS & web standards<br />http://www.w3.org/Style/CSS<br />
how’s our speed?<br />
email us...<br />kevin.vanderbeken@apn.com.au<br />colin.gourlay@apn.com.au<br />
next week...<br />
file:///C:/Users/Colin.Gourlay/Downloads/BackToTheFutureLogo.jpg<br />
Upcoming SlideShare
Loading in …5
×

1 03 - CSS Introduction

803 views

Published on

1 Comment
1 Like
Statistics
Notes
No Downloads
Views
Total views
803
On SlideShare
0
From Embeds
0
Number of Embeds
11
Actions
Shares
0
Downloads
43
Comments
1
Likes
1
Embeds 0
No embeds

No notes for slide
  • This lesson we’ll expand on the presentation layer – CSS – which is all the styling of content on the web.
  • Document overview:When you punch in a URL into your web browser or click on a hypertext link such as http://finda.com.au, your browser asks our servers to send back what web content it finds at that address. What the browser gets back is an (X)HTML formatted document. Document is the term that describes the full page and all it&apos;s associated bits and pieces. Everything that makes up the page you see when rendered is &quot;marked-up&quot; in that document.
  • What is CSS for:While (X)HTML is used to describe/mark up the content in a web page, it&apos;s Cascading Style Sheets (CSS) that describe how you want everything to look. The way the page looks is known as the presentation (remember the presentation layer?). CSS is the official standard way for formatting text and page elements. CSS is a separate language with its own syntax.CSS is very versatile, and it gives us the ability to control how documents will be presented in areas other than the browser on a screen. It can allow us to style the presentation of printing and on handheld devices. In advanced uses of CSS, It also has rules for defining the non-visual parts of documents, such as how they will sound when read by a screen reader.
  • Style sheets help us apply consistant automatic looks to our marked up documents, and there&apos;s plenty of benefits to it. Some of the main benefits could be summarised as:
  • Presentational (X)HTML never gets close to offering the kind of control over type, backgrounds, and layout that is possible with CSS.
  • You can change the appearance of an entire site by editing one style sheet. Making small tweaks or entire site redesigns with style sheets is much easier than when presentation instructions are mixed in with the markup.
  • Old school practices of using the old font elements and nested tables make for bloated documents. Not only that, you can apply a single style sheet document to all the pages in a site for further download savings.
  • When everything related to presentation is handled by CSS, you can mark up your content meaningfully (the way it&apos;s supposed to be), making it more accessible for nonvisual or mobile devices.
  • Nearly every browser in current use supports all of CSS Level 1 (not ie6 though) and the majority of CSS Level 2. Reliability of browser support, and differences between the use and support of CSS can often be a difficult thing to work out at times, but it&apos;s getting better.
  • When used to its full potential, CSS is a robust and powerful design tool.An awesome example of how powerful css is, can be seen with the CSS Zen Garden (www.csszengarden.com). All of the design there use the exact same XHTML source document and doesn’t include a single img element (all of the images are used as backgrounds). Look how different each page is! It&apos;s all done with style sheets. This demonstrates the power of separating content and presentation
  • A few things are required to invoke the magic of CSS as part of development.Create a document that has been marked up in XHTML.Writing out the rules for how you&apos;d like certain elements to look.Attach the style rules to the document. Once it&apos;s attached, the browser will use that presentation when showing the XHTML document! We&apos;ve started looking at markup and documents, and we&apos;ll be looking at the syntax for writing out these css rules, but how, even if we have these rules, can we tie the two together.
  • You can apply CSS properties and values to a single element using the style attribute in the element itself, as shown here:      &lt;h1 style=&quot;color: red;&quot;&gt;Introduction&lt;/h1&gt; To add multiple properties, just separate them with semicolons, like this:      &lt;h1 style=&quot;color: red; margin-top: 2em;&quot;&gt;Introduction&lt;/h1&gt; Inline styles apply only to the particular element in which they appear. Avoid writing inline styles as much as you can, unless it is absolutely necessary to override styles from an embedded or external style sheet. Inline styles can cause problems in that they mix presentation information into the structural markup. We like to keep the layers as separate as we can! They make it more difficult to make changes because every style attribute must be hunted down in the source markup for that page to be changed. These down points are some of the same reasons we don&apos;t like the old obsolete font element. 
  • This is the type is placed in a document using the style element and its rules apply only to that document. The style element must be placed in the head of the document and it must contain a type attribute that identifies the content of the style element as “text/css”. Note, This example also includes a css comment in purple. &lt;head&gt;    &lt;title&gt;Required document title here&lt;/title&gt;    &lt;style type=&quot;text/css&quot;&gt;        /* style rules go here */    &lt;/style&gt;&lt;/head&gt;
  • An external style sheet is a separate, text-only file that contains a number of style rules written in CSS language. It has to be named with the .css suffix. The .css file is linked to one or more (X)HTML documents. In this way, all the files in a web site may share the same style sheet. This is the most powerful and best method for attaching style sheets to content.  &lt;head&gt;      &lt;link rel=&quot;stylesheet&quot; href=&quot;/path/stylesheet.css&quot; type=&quot;text/css&quot; /&gt;      &lt;title&gt;Titles are required.&lt;/title&gt; &lt;/head&gt;
  • The two main sections of a CSS rule are the selector that identifies the element (or group of elements) to be affected, and the declaration that explains how those elements should be displayed on the screen.
  • The declaration has a property (such as color) and its value (green), separated by a colon and a space. Declarations are placed inside curly brackets. Everything inside the curly brackets is called the Declaration Block.In some of our earlier sessions we&apos;ve demo&apos;ed with the h1 and p elements. We could select those in css with:
  • This most basic type of selector is called an element type selector. The properties defined for each will apply to every h1 and p element in the document. Here we just wrote a rule that heading1 elements should be orange and paragraphs should be black in text colour. The color property in the declaration part of the css rule is the american spelling of the word, and unfortunately for us....is standard in CSS. Welcome to CSS, start spelling americanenglish. Values and property names shouldn&apos;t have spaces in them, as seen by writing “font-size” or “12px”.
  • The element selector
  • Selecting elements by ID.When an element is given an ID by using the ID attribute, we can uniquely apply a css rule to it. This is known as selecting by ID. The id attribute can be used with any (X)HTML element, and it is commonly used to give meaning to the generic div and span elements. ID Selectors are identified by the hash (#). An example of a list item with an id reference.&lt;p id=“article&quot;&gt;...text...&lt;/li&gt; #Note# Do the next part on the blackboard?Now we write a style rule just for that list item using an ID selector (notice the #):p#article { color: red; } Because id values must be unique in the document, it is acceptable to omit the element name. This rule is equivalent to the last one:#article { color: red; }
  • Selecting elements by class.The class identifier is used to classify elements (go figure!) into a conceptual group. Where it differs from the the id attribute is that more than one element can share a class name, but also, an element may belong to more than one class. The class is also specified in our markup with it&apos;s attribute:&lt;li class=&quot;myHappyShoes&quot;&gt;One of several happy shoes&lt;/li&gt;&lt;li class=&quot;myHappyShoes&quot;&gt;Another one of these happy shoes&lt;/li&gt; We target classes with their selector type. Class names are written with a period (.) in the selector. For example, to select all list items with class=&quot;myHappyShoes&quot;, use this selector (the period indicates the following word is a class selector):li.myHappyShoes { color: orange; } To apply a property to all elements of the same class, we can leave out the element name in the selector (leave the period there though; it’s the character that indicates a class). This target&apos;s all paragraphs and any other element that has been marked up with class=&quot;myHappyShoes&quot;..myHappyShoes { color: green; }
  • Human beings inherit things from their parents, like hair colour, big toes, etc. CSS has the same inheritance at work. Parents, pass on their traits to children. XHTML elements, will pass down certain style properties to others that are on a lower level in the document. This means that elements inside of other elements, can inherit from that parent.
  • For example, we may have a paragraph styled with bold text. If that paragraph also contains an inline anchor (&lt;a&gt;...&lt;/a&gt;) element, the contents of that one will also be bold with the rest of the text, unless we tell it not to be specifically.
  • This is where knowing your document’s structure comes in. We mentioned (X)HTML documents have a structure/hierarchy.Eg. A document has a html root element that contains a head and a body, and the body contains heading and paragraph elements. A few of the paragraphs, contain inline elements like images (&lt;img /&gt;) and emphasized text (&lt;em&gt;...&lt;/em&gt;). Visualize the structure as an upside-down tree, branching out from the root.The inheritance tree structure shows that all elements contained within another element are it&apos;s descendants.If an element is directly contained in another element, it&apos;s the child of that element. The containing element is the parent element.Elements higher up than that, are it&apos;s ancestors.Two elements with the same parent are siblings.
  • Pretty straight forward use of terminology right?With all this in mind, we can consider the idea that certain styles are passed on between levels in css. In general, properties related to the styling of text—font size, color, style, etc.—are passed down. Properties such as borders, margins, backgrounds, and so on that affect the boxed area around the element tend not to be passed down. We use inheritance to our advantage when writing CSS stylesheets - if we wanted all text elements to be shown in the verdanda font, we could set absolutely every element to be verdana - or... because we&apos;re smart... we&apos;ll write one rule and apply it to the body element. Then all the decendant text elements inside the body tag get that style applied.Example:   body { font-face: Verdana; }
  • The Cascade in Cascading Style SheetsBecause we can write rules that cascade down our document tree and apply styling... we can create conflicts.What should the browser do if a document’s linked style sheet says that &lt;h1&gt; elements should be red, but itsembedded style sheet has a rule that makes &lt;h1&gt;&apos;s purple? The designers of the CSS specification knew this would happen, and so they created a system that assigns different weight to the style we apply in different places and ways.
  • We&apos;ve learned that we can apply style rules in at least 3 ways. Inline, Embedded, and External. The closer the style sheet is to the content, the more weight it is given. Eg. Embedded have more weight than external.So from our example, the h1 elements would end up purple as specified in the embedded style sheet, not red as specified in the external .css file that has less weight. Inline styles could have even more weight, because you can&apos;t get any closer to the content than style information right in the elements&apos;s opening tag! What&apos;s all this weight business?? - The more weight a rule or set of rules caries, the more likely it will be applied in the end. Heavy ends up making their mark.
  • But what if there&apos;s still rules that conflict in CSS?The cascade keeps going of course! - This time, it&apos;s weighting at the rule level, not CSS sources. This happens after the various sources of CSS are weighted. When two rules in a single style sheet conflict, the type of selector that was used determines the next winner. The more specific a css selector rule is to the element, the more weight it is given. That more specific rule ends up overriding the other conflicting rule.
  • The class selector (the one with the period) sets the colour of the text as yellow for both our list elements and the paragraph element.The class selector on the next line is the same selector! When this happens, because it&apos;s written last compared to the one it&apos;s conflicting with.... we use it instead. So now this rule sets the colour of the text as green for both our list elements and the paragraph element.The use of the element in the selector for li.myHappyShoes specifies higher (has more weight) than just the class selector for the list elements that have that class. The list elements have orange text while the one paragraph element with that class keeps it&apos;s green.The id selector of #happyShoe is very specific, as an id must be unique for only one element in the page. This means the color of red is applied for the list element that has that id of happyShoe. The other list element stays orange, and the paragraph stays green.The li#happyShoe rule is actually redundant as we&apos;ve already established that there&apos;s only one possible element with that id of happyShoe... but, because it specifies that it must be for only list elements, it&apos;s even more specific... and get&apos;s more weighting... and hence makes the second list element turn from red to violet.The anchor element inside the list element has been specified here as well - the last rule shows that we&apos;ve selected a child element inside of our very specific happyShoe id element. The anchor link becomes blue.The child element &lt;a&gt; is written with a space after the first selector, and each space after that, indicates that it is nested in the previous one.
  • The first official version of CSS (the CSS Level 1 Recommendation, a.k.a CSS1) was officially released in 1996, and included properties for adding font, color, and spacing instructions to page elements. Unfortunately, lack of dependable browser support prevented the widespread adoption of CSS for several years. CSS Level 2 (CSS2) was released in 1998. It most notably added properties for positioning that allowed CSS to be used for page layout. It also introduced styles for other media types (such as print, handheld, and aural) and more sophisticated methods for selecting elements for styling. CSS Level 2, Revision 1 (CSS2.1) makes some minor adjustments to CSS2. Fortunately, most good browsers support the majority of the CSS1, CSS2, and CSS2.1 specifications. Some browsers are already supporting features from CSS Level 3 (CSS3) that is still in development. It adds support for vertical text, improved handling of tables and international languages, better integration with other XML technologies, and other little perks like multiple background images in a single element and a larger list of color names.Keep up to date with the W3C’s development of CSS at www.w3.org/Style/CSS. 
  • 1 03 - CSS Introduction

    1. 1. CSS Introduction<br />Colin Gourlay & Kevin Vanderbeken<br />
    2. 2. CSS<br />=<br />(the presentation layer)<br />
    3. 3. Today:<br />What is CSS?<br />Where can CSS be used?<br />CSS Syntax - selectors, properties & values<br />Selecting HTML elements<br />Inheritance & the cascade<br />
    4. 4. what is CSS?<br />
    5. 5. CSS = Cascaded Style Sheets<br />A way to customise the presentation of your HTML.<br />A language designed to allow us select and style the elements on our page.<br />Like HTML, it has gone through various changes.<br />Also like HTML, it is not a programming language.<br />It can allow us to present our content in multiple formats for multiple devices.<br />
    6. 6. why do we use CSS?<br />
    7. 7. better type & layout controls<br />less work<br />smaller documentsand faster downloads<br />
    8. 8. better type & layout controls<br />less work<br />smaller documentsand faster downloads<br />more accessible sites<br />
    9. 9. better type & layout controls<br />less work<br />smaller documentsand faster downloads<br />more accessible sites<br />reliable browser support<br />
    10. 10. less work<br />smaller documentsand faster downloads<br />more accessible sites<br />reliable browser support<br />
    11. 11. smaller documentsand faster downloads<br />more accessible sites<br />reliable browser support<br />
    12. 12. a great example...<br />http://www.csszengarden.com<br />
    13. 13. where can you use CSS?<br />
    14. 14. inline styles<br />embedded style sheets<br />external style sheets<br />
    15. 15. <html><br /> <head><br /> <title>Document Title</title><br /> </head><br /> <body><br /> <h1 style="color: red;"><br />Introduction<br /></h1><br /> </body><br /></html><br />
    16. 16. inline styles<br />embedded style sheets<br />external style sheets<br />
    17. 17. <html><br /> <head><br /> <title>Document Title</title><br /> <style type="text/css"><br /> h1 { color: red; }<br /> </style><br /> </head><br /> <body><br /> <h1>Introduction</h1><br /> </body><br /></html><br />
    18. 18. inline styles<br />embedded style sheets<br />external style sheets<br />
    19. 19. <html><br /> <head><br /> <title>Document Title</title><br /> <link rel="stylesheet" <br />href="stylesheet.css" <br /> type="text/css" /><br /> </head><br /> <body><br /> <h1>Introduction</h1><br /> </body><br /></html><br />h1 {<br />color: red;<br />}<br />stylesheet.css<br />
    20. 20. CSS syntax<br />
    21. 21. selectors, properties & values<br />
    22. 22. selector { property: value; }<br /> <br />selector {<br />     property1: value1;<br />     property2: value2;<br />     property3: value3;<br />}<br />
    23. 23. h1 { color: orange; }<br /> <br />p {<br />     color: #000000;<br />     font-size: 12px;<br />     font-family: Arial;<br />}<br />
    24. 24. h1 {}<br /><h1>Introduction</h1><br />
    25. 25. #article {}<br /><p  id="article">...text...</p><br />
    26. 26. .tab {}<br /><li  class="tab">...text...</li><br />
    27. 27. inheritance & the cascade<br />(a.k.a. the BIG concepts)<br />
    28. 28. inheritance<br />
    29. 29. HTML<br /><p class="bold"><br /> <a href="http://www.google.com"><br /> Google<br /> </a><br />is an excellent search engine.<br /></p><br />CSS<br />p.bold {<br />font-weight: bold;<br />}<br />
    30. 30. document structure<br />
    31. 31. all elements contained within another element are its descendants<br />title, style and headare descendants of html<br />
    32. 32. a direct descendant is called a child element<br />bodyis a child of html<br />
    33. 33. a containing element is called the parent element<br />htmlis the parent of body<br />
    34. 34. the trail of parents leading back to the root are an element’s ancestors<br />p, body and htmlare ancestors of img<br />
    35. 35. all elements with the same parent are siblings<br />h1, p, p, h2, etc are siblings<br />
    36. 36. If we wanted all text elements to be shown in verdana font, we apply one rule to the <body><br />body{font-face: Verdana; },<br />Then all the decendant text elements inside the body tag get that style applied.<br />
    37. 37. the cascade<br />
    38. 38.  the closer the style sheet is to the content, the more weight it is given<br />
    39. 39. rules can still conflict...<br />...but the cascade still applies<br />
    40. 40. HTML<br /><li class="myHappyShoes"><br />One of many happy shoes<br /></li><br /><li id="happyShoe" class="myHappyShoes"><br />     A <a href="http://shoe.com/">happy shoe</a><br /></li><br /><p class="myHappyShoes">Happy shoes paragraph</p><br />CSS<br />.myHappyShoes { color: yellow; }<br />.myHappyShoes { color: green; }<br />li.myHappyShoes { color: orange; }<br />#happyShoe { color: red; }<br />li#happyShoe { color: violet; }<br />li#happyShoe a { color: blue; }<br />
    41. 41.
    42. 42. about CSS & web standards<br />http://www.w3.org/Style/CSS<br />
    43. 43. how’s our speed?<br />
    44. 44. email us...<br />kevin.vanderbeken@apn.com.au<br />colin.gourlay@apn.com.au<br />
    45. 45. next week...<br />
    46. 46. file:///C:/Users/Colin.Gourlay/Downloads/BackToTheFutureLogo.jpg<br />

    ×