SM5312 week 6: basic CSS syntax1 An Introduction to Cascading Style Sheets Basic CSS Syntax Nick Foxall.

Slides:



Advertisements
Similar presentations
Intro To Cascading Style Sheets By Mario Yannakakis.
Advertisements

CSS Cascading Style Sheets. Objectives Using Inline Styles Working with Selectors Using Embedded Styles Using an External Style Sheet Applying a Style.
Cascading Style Sheets
Cascading Style Sheets
Introducing CSS CIS 133 mashup Javascript, jQuery and XML 1.
Cascading Style Sheets. CSS stands for Cascading Style Sheets and is a simple styling language which allows attaching style to HTML elements. CSS is a.
© 2004, Robert K. Moniot Chapter 6 CSS : Cascading Style Sheets.
CSS BASICS. CSS Rules Components of a CSS Rule  Selector: Part of the rule that targets an element to be styled  Declaration: Two or more parts: a.
Building a Website: Cascading Style Sheets (CSS) Fall 2013.
Web Pages and Style Sheets Bert Wachsmuth. HTML versus XHTML XHTML is a stricter version of HTML: HTML + stricter rules = XHTML. XHTML Rule violations:
กระบวนวิชา CSS. What is CSS? CSS stands for Cascading Style Sheets Styles define how to display HTML elements Styles were added to HTML 4.0 to.
Introduction to Cascading Style Sheets (CSS) Module 2: HTML Basics LESSON 4.
Cascading Style Sheets Scripting with Style. CSS versus HTML  Ways to format in HTML –HTML Tag extensions –Converting Text to images –Page Layout with.
XP 1 Working with Cascading Style Sheets Creating a Style for Online Scrapbooks Tutorial 7.
Controlling Page Style: Cascading Style Sheets (CSS) References: 1.Wang, P.S & Katila, S. (2004). A Introduction to Web Design and Programming. CA: Thomson.
4.01 Cascading Style Sheets
Tutorial 3: Adding and Formatting Text. 2 Objectives Session 3.1 Type text into a page Copy text from a document and paste it into a page Check for spelling.
CSS: Cascading Style Sheets. 2 History HTML tags were originally designed to define the content of a document. The layout of the document was supposed.
Principles of Web Design 6 th Edition Chapter 4 – Cascading Style Sheets.
Chapter 11 Cascading Style Sheets: Part I The Web Warrior Guide to Web Design Technologies.
Cascading Style Sheets Part 1 Library and Information Services, University of St Andrews.
Review: Three parts to a webpage: - Content - Structure  XHTML - Presentation  CSS In this chapter we will focus on introducing and examining the mechanics.
Cascading style sheets (CSS)
Copyright © Terry Felke-Morris WEB DEVELOPMENT & DESIGN FOUNDATIONS WITH HTML5 Chapter 3 Key Concepts 1 Copyright © Terry Felke-Morris.
Week 4.  Three ways to apply CSS: Inline CSS Internal style sheets ( header style information) External style sheets.
The Characteristics of CSS
CSS Cascading Style Sheets By Garrett Garman. CSS Why use Style Sheets? Separates Appearance and Structure Modularity Quick and Easy changes Flexibility.
Cascading Style Sheets CSS.  Standard defined by the W3C  CSS1 (released 1996) 50 properties  CSS2 (released 1998) 150 properties (positioning)  CSS3.
Cascading Style Sheet (CSS)
Today’s objectives  Complete web page  Using xhtml & CSS  Adding CSS to documents Embed url(File);  CSS.
Cascading Style Sheets. Defines the presentation of one or more web pages Similar to a template Can control the appearance of an entire web site giving.
Today’s objectives  Presentational | Inline | Block | Validate  CSS | Rules | Declarations.
 HTML stands for Hyper Text Mark-up Language. The coding language used to create documents for the World Wide Web  HTML is composed of tags. HTML tags.
1 What is CSS?  CSS stands for Cascading Style Sheets  Styles define how to display HTML elements  Styles are normally stored in Style Sheets  Styles.
ITCS373: Internet Technology Week 3: Introduction to CSS Dr. Faisal Al-Qaed.
 This presentation introduces the following: › 3 types of CSS › CSS syntax › CSS comments › CSS and color › The box model.
Tutorial #4 Formatting Text and Links. Tutorial #3 Review - CSS CSS format Selector { property1: value1; /* Comments */ } Embedded, In-Line, and External.
Today’s objectives  Review  CSS | Rules | Declarations  HTML Structure document  Class asignment.
XHTML/CSS Week 3. This Week  Quiz to revise last week (XHTML and DW)  CSS - Part 2  Using HTML Tables.
Cascading Style Sheets Orientation Learning Web Design: Chapter 11.
CO1552 – Web Application Development Cascading Style Sheets.
Tutorial #3 Cascading Style Sheets. Tutorial #2 Review - Anchors Links to Site DMACC Internal Links Go to Top Mail To me Local.
Introduction to CSS. What is CSS?  Cascading Style Sheets  Used for styling HTML  Also important in javascript and jquery for selectors  External.
Introduction to Programming the WWW I CMSC Winter 2003 Lecture 7.
Cascading Style Sheets Part 1. CSS vs HTML HTML: Originally intended to markup structure of a document (,...,,,,,...) CSS Developing technology, CSS1,
XP Tutorial 7New Perspectives on HTML and XHTML, Comprehensive 1 Working with Cascading Style Sheets Tutorial 7.
CSS Tutorial 1 Introduction Syntax How to use style specifications. Styles.
Chapter 6 Introducing Cascading Style Sheets Principles of Web Design, 4 th Edition.
ECA225 Applied Interactive Programming Cascading Style Sheets, pt 1 ECA 225 Applied Online Programming.
INT222 - Internet Fundamentals Shi, Yue (Sunny) Office: T2095 SENECA COLLEGE.
The Web Wizard’s Guide To DHTML and CSS Chapter 1 A Review of CSS1.
Cascading Style Sheets CSS.  Standard defined by the W3C  CSS1 (released 1996) 50 properties  CSS2 (released 1998) 150 properties (positioning)  CSS3.
Cascading Style Sheets (CSS). A style sheet is a document which describes the presentation semantics of a document written in a mark-up language such.
CSS – 1h. Without stylesheets proprietary HTML extensions making text into images proliferate use of "spacer" GIF images deprecated HTML elements and/or.
CSS Cascading Style Sheets A very brief introduction CSS, Cascading Style Sheets1.
CSS Hadas Kahsay. Overview  What is CSS  Basic syntax of CSS Rules  How to link CSS style to html documents  Browsers and CSS  Advantages of CSS.
Cascading Style Sheets Primary readings Presentations Explain & review projects with class mates.
CSS: Cascading Style Sheets Part II. Style Syntax.
NASRULLAHIBA.  It is time to take your web designing skills to the next level with Cascading Style Sheets (CSS). They are a way to control the look and.
ECA 228 Internet/Intranet Design I Cascading Style Sheets.
CSS Introductions. Objectives To take control of the appearance of a Web site by creating style sheets. To use a style sheet to give all the pages of.
1 Cascading Style Sheets
WEB FOUNDATIONS CSS Overview. Outline  What is CSS  What does it do  Where are styles stored  Why use them  What is the cascade effect  CSS Syntax.
Cascading Style Sheets
Cascading Style Sheets
IS333: MULTI-TIER APPLICATION DEVELOPMENT
What are Cascading Stylesheets (CSS)?
Cascading Style Sheets
Presentation transcript:

SM5312 week 6: basic CSS syntax1 An Introduction to Cascading Style Sheets Basic CSS Syntax Nick Foxall

SM5312 week 6: basic CSS syntax2 What is CSS? CSS stands for Cascading Style Sheets Styles define how XHTML elements and markup should be displayed by the browser (or user agent) Styles can be included in the area of an XHTML document, or placed in external Style Sheet files. Multiple style definitions are able to cascade into one…

SM5312 week 6: basic CSS syntax3 What’s a “Cascade”? All styles will "cascade" into a new "virtual" style sheet in the following rule order, where number four has the highest priority: 1. Browser default 2. External style sheet 3. Internal style sheet (inside the tag) 4. Inline style (inside an HTML element) An inline style (inside an HTML element) with the highest priority will override a style declared inside the tag, in turn overrides an external style sheet, which finally overrides browser default values.

SM5312 week 6: basic CSS syntax4 External Style Sheets Having all styles in an external Style Sheet allows for the style formatting of many pages from one file, saving a lot of updating work. An external Style Sheet is a text file saved with the extension.css At the top of a new CSS document, the character set should be "UTF-8"; Not essential, but good practise to ensure the CSS file is compliant with web standards. A new, blank.css file created in Dreamweaver will automatically include this syntax.

SM5312 week 6: basic CSS syntax5 Rules in CSS A CSS Style Sheet is basically a collection of rules, describing how the browser should display XHTML elements. Each rule contains 2 parts: A Selector, stating which element in the XHTML a rule applies to; One or more Declarations, which describe how these elements should be displayed.

SM5312 week 6: basic CSS syntax6 CSS Rules: Selectors and Declarations h1 { font-family: Times, Georgia, serif; font-size: 24px; text-align: center; } Selector Declarations Enclosed in curly brackets { } Rule

SM5312 week 6: basic CSS syntax7 h1 { font-family: Times, Georgia, serif; font-size: 24px; text-align: center; } CSS Rules: Selectors and Declarations Selector Declarations Rule A more readable way of writing CSS rules

SM5312 week 6: basic CSS syntax8 h1 { font-family: Times, Georgia, serif; font-size: 24px; text-align: center; } CSS Rules: Declaration Parts ValuesProperties Declarations consist of 2 parts: a property and a value. Each declaration ends with a semi-colon ( ; ). Properties and values are separated with a colon ( : ). Properties and values separated with a colon Each declaration separated with a semi- colon :;

SM5312 week 6: basic CSS syntax9 h1 { font-family: “Trebuchet MS”, serif; font-size: 24px; text-align: center; } CSS Rules: Declaration Parts Unlike XHTML, values do not have to be enclosed in quotation marks, unless the value name includes a space (e.g multi-word name). Only values with a space in the name need to be enclosed in quotes

SM5312 week 6: basic CSS syntax10 Versions of CSS Like XML and XHTML, CSS specifications are laid down by the World Wide Web Consortium. The current, most widely used version of the CSS specification is version 2.1 There are over 100 different properties available in CSS 2.1. You can view the current CSS 2.1 specification at You can also validate the CSS you are working on by going to

SM5312 week 6: basic CSS syntax11 CSS Simple, or Element Selectors The most basic form of CSS selector is an XHTML element name; h1, h2, p, ol, ul, table, etc. This is the simple or element selector. Example: p { color: #003366; } This will set every occurrence of content marked up the paragraph tag to be a dark blue colour.

SM5312 week 6: basic CSS syntax12 CSS Class Selectors However, in XHTML markup, elements can have class attributes assigned to them. The value attached to a class attribute can be one (or more) names, separated by spaces. Example: or The actual definition of the value “special” is defined in a CSS class selector…

SM5312 week 6: basic CSS syntax13 CSS Class Selectors h1.special { color: #FF0000; } This will now make all elements with the class “special” display text with a red colour. elements that don’t have the class attribute “special” will continue to display in the default colour. A CSS class selector can also be defined more generally, without the element name (just the dot):.special { color: #FF0000; } This class can now be applied to any XHTML element that has the class attribute “special”. Actually the full CSS syntax is *.special, where * is a selector that matches anything. However, CSS shorthand means we can drop the *.

SM5312 week 6: basic CSS syntax14 CSS ID Selectors XHTML elements can also have id selectors assigned to them. Example: blah, blah, blah. In the CSS, the id “summary” can be styled in a rule, thus: #summary { font-style: italic; } Whereas class selectors can be used across a number of elements in an XHTML document, ID selectors can only be assigned to one specific element in any given XHTML document.

SM5312 week 6: basic CSS syntax15 CSS ID Selectors In the CSS, id selectors are always defined with a # (hash) symbol first: #summary { font-style: italic; } Again, this is CSS shorthand for… *#summary { font-style: italic; } …meaning the id #summary can be applied to any XHTML element (but only one element can have that id name in the XHTML document).

SM5312 week 6: basic CSS syntax16 Class Selectors vs ID Selectors How do we know which to use and when? ID selectors: 1.As they must be unique to a page, ID selectors are useful for persistent structural elements, such as navigation zones, or key content areas, that occur once on a page, but that are consistent across a site. 2.For example, #mainNav may be the selector to style the the main navigation element, which will likely appear on every page of your site. 3.So, ID selectors are generally applied to conceptually similar elements across a site.

SM5312 week 6: basic CSS syntax17 Class Selectors vs ID Selectors How do we know which to use and when? Class selectors: 1.As they can be allied to any number of elements on a page, class selectors are useful for identifying (and targeting) types of content, or similar items. 2.For example, you have a news page with a date at the start of each story. If you use ID selectors, you’d have to give every occurrence of the date a separate ID name. Much easier to give every date one class name and style that one class.

SM5312 week 6: basic CSS syntax18 CSS Pseudo-Classes Pseudo-classes are CSS classes used to add effects to certain elements. They are used most often to style the anchor elements of hyperlinks. Example: a:link { color: blue; text-decoration: underlined; } Can also be written without the a (anchor) element: :link { color: blue; text-decoration: underlined; }

SM5312 week 6: basic CSS syntax19 CSS Pseudo-Classes There are four pseudo-class elements provided to make rollover and on-click effects possible: a:link { color: blue; text-decoration: underlined; } link not yet visited a:visited { color: green; text-decoration: underlined; } visited link a:hover { color: red; text-decoration: none; } effect on the link when the mouse hovers over it a:active { color: purple; text-decoration: none; } effect on the link when the mouse button is pressed down on it Note that pseudo-classes for rollover effects must be written in this order in a CSS file for them to work correctly.

SM5312 week 6: basic CSS syntax20 Combining Pseudo-Classes with Classes Pseudo-classes can also be combined with regular CSS class selectors to create multiple link and rollover styles, depending on the parent class. Examples: a.main:link { color: blue; } a.sidebar:link { color: grey; } a.footer:link { color: white; }

SM5312 week 6: basic CSS syntax21 Element Selector Grouping Element selectors can be grouped together if you want to apply one CSS rule to a range of elements. Example: h1, h2, h3, h4, h5, h6 { color: #FF0000; } Each element is separated by a comma (except for the last element before the start of the declaration). However…

SM5312 week 6: basic CSS syntax22 Inheritance Certain CSS properties are inherited by the descendents of the elements those styles are applied to. For example, if you give the element a color style of gray, all descendents of will inherit the color gray, until you specify otherwise.

SM5312 week 6: basic CSS syntax23 Inheritance By setting a number of ‘basic’ styles in top-most elements, such as, you can avoid having to style many descendent elements. This… body { color: gray; } Is much better than having to write this… p, div, h1, h2, h3, ul, ol { color: gray; } Note: inheritance is not the same as cascade!

SM5312 week 6: basic CSS syntax24 The Universal Selector There is also a Universal Selector, that acts like a wildcard, styling every available element that isn’t specifically styled elsewhere. Example: * { padding: 0; margin: 0; } …would usefully set the overall page styling to zero padding and margin, which could then be adjusted as-needed further down the line. The universal selector is donated by an * symbol

SM5312 week 6: basic CSS syntax25 Descendent Selectors Descendent Selectors (sometimes called contextual) can give a finer level of control over class selectors. Examples: p em { font-weight: bold; color: red; } Bold and blue will be applied to the element, only if it occurs inside a element. It would not apply if the was inside say an element. #mainNav ul { font-family: Arial, sans-serif } The font of the element would only change to Arial if it was inside the content area defined by the ID #mainNav Each descendent element is separated by a single space in the CSS code (no commas!)

SM5312 week 6: basic CSS syntax26 Class & Element Selector Order A class selector will take precedence over a more general element selector. Example, These two CSS rules set the color of paragraph elements: p { color: gray; } p.first { color: black; } The top rule sets all general content to a grey colour. The next rule sets all paragraphs with the class attribute “first” (i.e. ) to black text. The p.first rule takes precedence and changes the text to black.

SM5312 week 6: basic CSS syntax27 Class & Element Selector Order If an XHTML element belongs to more than one class, then the later CSS rules take precedence over earlier ones Example: p.first { color: #000000; } p.changed { color: purple; } If a element in the XHTML is specified with both classes (i.e. ) then the text will become purple, not black. The last class rule in the order takes precedence.