© 2010, Robert K. Moniot Chapter 5 CSS : Cascading Style Sheets 1.

Slides:



Advertisements
Similar presentations
Copenhagen, 6 December 2004 Modern CSS Principles Miruna Bădescu Finsiel Romania.
Advertisements

CSS-Formatting Review Cascading Style Sheets addstyle to your HTML web pages.
Intro To Cascading Style Sheets By Mario Yannakakis.
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 (CSS) “Styles” for short. Pg. 418.
Today CSS HTML A project.
Lecture 5 Use Cases and Style Sheets
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.
Cascading Style Sheets Basics. Why use Cascading Style Sheets? Allows you to set up a series of rules for all pages in a site. The series may be changed.
Cascading Style Sheets By: Valerie Kuna. What are Cascading Style Sheets? Cascading Style Sheets (CSS) are a standard for specifying the presentation.
Today’s objectives  Element relations – tree structure  Pseudo classes  Pseudo elements.
© 2004, Robert K. Moniot Chapter 6 CSS : Cascading Style Sheets.
1 Web Developer & Design Foundations with XHTML Chapter 9 Key Concepts.
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.
1 Pengantar Teknologi Internet W03: CSS Cascading Style Sheets.
CIS 1310 – HTML & CSS 6 Layout. CIS 1310 – HTML & CSS Learning Outcomes  Describe & Apply the CSS Box Model  Configure Float with CSS  Designate Positioning.
Recognizing the Benefits of Using CSS 1. The Evolution of CSS CSS was developed to standardize display information CSS was slow to be supported by browsers.
CSS(Cascading Style Sheets )
Part 3 Introduction to CSS. CSS Text Text Styles h1 {color: green} // rgb(0,255,0), #00ff00 h2{letter-spacing: 0.5cm} // 2px h3 {text-align: right} //
กระบวนวิชา 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.
Web Design with Cascading Style Sheet Lan Vu. Overview Introduction to CSS Designing CSS Using Visual Studio to create CSS Using template for web design.
Cascading Style Sheets SP.772 May 6, CSS Useful for creating one unified look for an entire web site. Helps to seperate style from content. Can.
Using Cascading Style Sheets CSS Basics. Goals Understand basic syntax of Cascading Style Sheets (CSS) Understand basic syntax of Cascading Style Sheets.
Outline IS400: Development of Business Applications on the Internet Fall 2004 Instructor: Dr. Boris Jukic CSS: Cascading Style Sheets.
14-Jul-15 CSS Applications to XML. 2 A different emphasis CSS is the same for XML as it is for HTML, but-- HTML already does a pretty good job of layout.
3.1 Cascading Style Sheets. Motto Fashions fade, style is eternal. —Yves Saint Laurent.
CSS (Cascading Style Sheets): How the web is styled Create Rules that specify how the content of an HTML Element should appear. CSS controls how your web.
1 Working with Cascading Style Sheet (CSS). 2 Cascading Style Sheets (CSS)  a style defines the appearance of a document element. o E.g., font size,
Using Cascading Style Sheets. Introduction to Styles and Properties  Cascading Style Sheets (CSS) are a standard set by the World Wide Web Consortium.
Cascading style sheets (CSS)
Week 4.  Three ways to apply CSS: Inline CSS Internal style sheets ( header style information) External style sheets.
Cascading Style Sheet (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.
1 Cascading Style Sheets (CSS) Part 2. 2 The Sources of Style Sheets.
CSS The Definitive Guide Chapter 8.  Allows one to define borders for  paragraphs  headings  divs  anchors  images  and more.
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.
 A style sheet is a single page of formatting instructions that can control the appearance of many HTML pages at once.  If style sheets accomplished.
 Cascading Style Sheets (CSS) ◦ Used to specify the presentation of elements separately from the structure of the document.  Inline style ◦ declare.
Cascading Style Sheets Eugenia Fernandez IUPUI. CSS Purpose CSS allow you to specify the style in which your XML elements are displayed. CSS were originally.
XP Review 2 New Perspectives on JavaScript, Comprehensive1 Introducing Cascading Style Sheets Formatting Web Pages with CSS.
THE BOX MODEL Putting layouts together with CSS. The Box Model  How would you describe a box?  Container?  Tags or elements are “containers”  puts.
1 Working with Cascading Style Sheet (CSS). 2 Cascading Style Sheets (CSS)  a style defines the appearance of a document element. o E.g., font size,
CSS: Cascading Style Sheets Part II. Style Syntax.
CSS Cascading Style Sheets *referenced from
CSS FOUNDATIONS IN-DEPTH The nitty-gritty of css...
Blended HTML and CSS Fundamentals 3 rd EDITION Tutorial 3 Introducing Cascading Style Sheets.
Internet & World Wide Web How to Program, 5/e Copyright © Pearson, Inc All Rights Reserved.
Internet & World Wide Web How to Program, 5/e 1. 2.
Cascading Style Sheets
Cascading Style Sheets™ (CSS)
CSS: Cascading Style Sheets
Cascading Style Sheets
Chapter 6 Cascading Style Sheets™ (CSS)
IS 360 Declaring CSS Styles
Madam Hazwani binti Rahmat
Cascading Style Sheets
CSS Applications to XML 19-Sep-18.
IS333: MULTI-TIER APPLICATION DEVELOPMENT
Second CSS Lecture Applications to XML
CSS Style Sheets: Intro
Cascading Style Sheets
More CSS 22-Feb-19.
Cascading Style Sheets
CSS Applications to XML 20-May-19.
CSS Applications to XML 3-Jul-19.
Cascading Style Sheets™ (CSS)
Presentation transcript:

© 2010, Robert K. Moniot Chapter 5 CSS : Cascading Style Sheets 1

OBJECTIVES In this chapter, you will learn: How to control document appearance with style sheets: –Specifying font, size, color, etc. of text –Specifying element backgrounds and colors –Controlling text flow, margins, borders, and padding How to make web pages share style sheets for uniform look. How to use the class attribute to apply styles to document elements according to their function. How to use pseudo-classes to allow element styles to respond to dynamic events. © 2010, Robert K. Moniot2

Style Sheets Each element on a page has a style defined for it. The style is defined by a set of attribute : value pairs. Style attributes can control: –Typeface and font properties –Background properties –Box-related properties –List properties 3

© 2010, Robert K. Moniot Ways to define styles Default style: provides values for all element properties, unless you change it. (Note: user can customize browser to change defaults!) Inline style: style is defined as an attribute of the element in-place. Use this for “one-off” or special styles. Embedded style sheet: styles defined in the head portion of web page. Use this if you don’t have very many web pages, or for styles that are useful only for the given page. External style sheet: styles defined in a separate file. Use this to centralize style definitions and provide uniformity across all pages of a web site. 4

© 2010, Robert K. Moniot Embedded Style Sheet Page with embedded style selector { attribute : value ; attribute : value... } selector { attribute : value ; attribute : value... } Style definitions go into a element in document head. Selector determines what elements the style rule applies to. Style definitions separated by ; are enclosed in { } 5

© 2010, Robert K. Moniot Embedded Style Sheet (cont’d) Page with embedded style selector { attribute : value ; attribute : value... } selector { attribute : value ; attribute : value... } The type attribute can only be "text/css". Note: CSS is not HTML! 6

© 2010, Robert K. Moniot Example Example page with embedded style body { font-family : sans-serif; color : blue; background-color : yellow } h1 { font-style : italic } p { font-size : 14pt } ol { font-size : 12pt; color : red; font-family : serif }... Here the selectors are simply tag names. The style rules will apply to elements defined by those tags. Result (Example 1)Result 7

On-Line References There are many, many style properties. These slides only show a few. Your text shows more. For a complete list, plus examples, consult one of these references: w3.org – the World-Wide Web Consortium: the official standardw3.org w3schools.com – a commercial site, but free access, includes tutorialsw3schools.com © 2010, Robert K. Moniot8

Length Units Relative lengths (depend on screen resolution and/or local text size) –px = pixels –em = (originally) width of letter M –ex = (originally) height of letter x, roughly ½ an em –% = percentage of the default font size or window size Absolute lengths (rendered as closely as possible) –in = inches –cm = centimeters –mm = millimeters –pt = points = 1/72 in –pc = picas = 12 pt = 1/6 in © 2010, Robert K. Moniot9

Inheritance A descendant is an element that is enclosed (nested) in another, its ancestor. (If it is an immediate descendant, it is a child of the enclosing element, its parent. Elements having the same parent are siblings.) All descendants of an element inherit its style properties, unless these are overridden by their own style rules. 10

Precedence Rules User-defined styles (preferences) take precedence over browser defaults. Styles defined by web page author take precedence over user preferences. Styles defined by child element take precedence over styles inherited from parent. If two styles could apply to the same element, the one defined by the more specific rule will be used. For instance, an explicit rule is always more specific than an inherited rule. © 2010, Robert K. Moniot11

© 2010, Robert K. Moniot Compound Selectors Selectors can be defined so that a style rule applies to an element only when it is a descendant of a certain other type of element. Examples: ul ul { list-style-type : square } This specifies that an unordered list inside another unordered list will be bulleted by squares. h1 em em { color : red } This specifies that emphasized text inside emphasized text in an header will appear in red. 12

© 2010, Robert K. Moniot Compound selectors are more specific than simple selectors. For instance, if a style sheet defines both p { color : red } div p { color : blue } then for a tag that is inside a element, the second rule would apply. Compound Selectors 13

© 2010, Robert K. Moniot Style Classes These allow you to control which elements of a given type should use a style rule. This method has two parts: –In the style sheet, the selector defines the class name, which is preceded by a period. –In the HTML, the tag includes the class attribute and specifies the value of the class name Example: Define the nodec class for anchor tags: a.nodec { text-decoration : none } This suppresses the usual underlining. Use it in HTML like so: Link text 14

© 2010, Robert K. Moniot Style Classes Style classes can also be “generic,” i.e. not tied to a specific element type. Example: Define the zowie class:.zowie { text-decoration : blink } Use it on an emphasized element: Important! Use it with no other style attributes: Buy Now! By the way: promise me you won’t ever use blink! Example 2 15

© 2010, Robert K. Moniot The and Tags These tags are provided to allow arbitrary chunks of HTML to be treated as elements. This is usually done in order to apply style attributes to them, as in the preceding example. A... element defines an “inline” structure, i.e. it simply defines a stretch of text. Thus it can be used within a paragraph or table element without affecting the flow of the text. A... element defines a “block” structure. Usually the browser will place line breaks before and after this element, but otherwise it has no effect itself. 16

© 2010, Robert K. Moniot Pseudo-classes These are like style classes, but an element acquires a pseudo-class by user action or by a relationship other than descendancy. In the style sheet, a pseudo-class name is preceded by a colon. In the HTML, the pseudo-class name is NOT used with the affected tag, because it is implied. 17

© 2010, Robert K. Moniot Pseudo-classes Link-related pseudo-classes a:link { color : red } Applies when the link has not yet been visited. a:visited { color : green } Applies when the link has been visited. a:hover { color: yellow } Applies when the mouse is over the link. 18

© 2010, Robert K. Moniot Cascading Rule If two equally specific rules can apply to the same element, the one that comes last in the style sheet is used. Thus, in the example below, a:hover must follow a:link and a:visited in order to have effect, since a link can be both visited (or not) and hovering. The order of the first two doesn’t matter since they are mutually exclusive. a:link { color : red } a:visited { color : green } a:hover { color : yellow } ResultResult (Example 3) 19

© 2010, Robert K. Moniot Pseudo-elements :first-line is a pseudo-element that consists of the first line of text in a block-level element. :first-letter is a pseudo-element that consists of the first letter of the text in an element. Closely related to pseudo-classes, in that they are defined by relationships, not by explicit declarations. A pseudo-element refers to a virtual element that is part of an actual element, rather than a special case of a whole element. 20

© 2010, Robert K. Moniot Pseudo-elements p { text-indent: 1em } p.initial { text-indent: 0 } p.initial:first-line { text-transform: uppercase } p.initial:first-letter { font-size: 24pt } This indents all normal paragraphs. A paragraph that is declared with class="initial" is not indented, and its first line appears in all capital letters, with an extra-large first letter. ResultResult (Example 4) 21

© 2010, Robert K. Moniot Inline Styles Defined for individual elements, at the point of use (in the HTML). Useful for “one-off” styles that are not likely to be used elsewhere. Method: HTML text The attribute:value pairs are what would go between { } if this were a style-sheet rule. There is no selector since the style applies to this element only. Example 4 22

© 2010, Robert K. Moniot External Style Sheets A style sheet can be placed in a separate file (usually named with suffix.css) and referenced by HTML files that need it. Useful for centralizing control of style and ensuring uniform appearance across many pages of a web site. The contents of the file are what would go between... of an embedded style sheet. Note: this file is not HTML! The file is referenced using a tag in the HTML document's head portion. 23

© 2010, Robert K. Moniot Example of style sheet /* This style sheet defines an "excerpt" class for paragraphs that is much like blockquote. */ p.excerpt { font-style : italic; margin-right : 2em; margin-left : 2em; } Here is what an external style sheet (named style.css) could contain: Note that there is no HTML in this file! This example also illustrates a CSS comment between /* and */. Such comments can be placed in external or embedded style sheets. 24

© 2010, Robert K. Moniot Example using style sheet The rel attribute specifies the relationship of the referenced file to this page. The type attribute must be "text/css". The href attribute is a URL pointing to the external style sheet. Style Example... affected text... ResultResult (Example 5) 25

Box Model All block-level elements are considered to be inside a box surrounded by padding, a border, and a margin. Typically the border is black, and margin and padding provide space around it. © 2010, Robert K. Moniot26 Margin Border Padding Content

Borders, Margins, Padding Widths can be set all around (e.g. border-width, margin ) or on each side individually (e.g. border- top-width, margin-top). Different border styles can be selected with border- style. Default is none, i.e. border is invisible. © 2010, Robert K. Moniot27

Floats Block-level objects can be floated, so that they are positioned all the way to left or right on the page. –Usually used for images, but can also be used for text if width is made less than full page. Position specified by float property, with values left or right. Adjacent floats are rendered next to each other if space allows. Text following a float will flow around it unless the clear property is applied. © 2010, Robert K. Moniot28 Example 6

Controlling display Rendering of elements can be controlled using display property. Values: –none : element will not be rendered –inline : element will be rendered in-line with surrounding content, like a –block : element will be rendered as a block, i.e. with line break before and after, like a Use to change block elements to inline or vice versa Can change dynamically, e.g. using hover pseudoclass © 2010, Robert K. Moniot29 Example 7

Invisibility There is also a visibility:hidden property that makes an element invisible, but it still takes up space in the rendered document. © 2010, Robert K. Moniot30

User Style Sheets Users can specify their own style sheets, to apply in absence of overriding styles from document style sheets. Internet Explorer 7: Tools → Internet Options → General → Accessibility Allows user to specify a style sheet to use. Mozilla Firefox: uses a file named userChrome.css in chrome subdirectory of Firefox application data directory. © 2010, Robert K. Moniot31