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.

Slides:



Advertisements
Similar presentations
CSS Cascading Style Sheets. Objectives Using Inline Styles Working with Selectors Using Embedded Styles Using an External Style Sheet Applying a Style.
Advertisements

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 By: Valerie Kuna. What are Cascading Style Sheets? Cascading Style Sheets (CSS) are a standard for specifying the presentation.
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.
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. What is CSS? CSS stands for Cascading Style Sheets Styles define how to display HTML elements Styles were added to HTML 4.0 to.
© Yaron Kanza HTML,CSS and JavaScript Written by Dr. Yaron Kanza, Edited by permission from author by Liron Blecher.
CS4370/6370 Web Development Cascading Style Sheets (CSS)
Web Design & Development Cascading Style Sheets (CSS)
1 Cascading Style Sheets (CSS) DBI Overview of CSS.
Cascading Style Sheets Scripting with Style. CSS versus HTML  Ways to format in HTML –HTML Tag extensions –Converting Text to images –Page Layout with.
CSS: Cascading Style Sheets. What are Style Sheets A style sheet is a mechanism that allows to specify how HTML (/XHTML/XML) pages should look The style.
Cascading Style Sheets (CSS). DBI 2008 HUJI-CS 2 What are Style Sheets A style sheet is a mechanism that allows to specify how HTML (/XHTML/XML) pages.
4.01 Cascading Style Sheets
3.1 Cascading Style Sheets. Motto Fashions fade, style is eternal. —Yves Saint Laurent.
Principles of Web Design 6 th Edition Chapter 4 – Cascading Style Sheets.
Cascading Style Sheets CS3505. What are CSS? Method for adding style attributes consistently to HML tags Cascading because styles are applied in order.
CSS Dvijesh Bhatt.
Cascading style sheets (CSS)
Dreamweaver -- CSS. Dreamweaver -- MX New icons are added in MX Most of the features commonly used in web design, and are same as FrontPage. New feature.
Week 4.  Three ways to apply CSS: Inline CSS Internal style sheets ( header style information) External style sheets.
Cascading Style Sheet (CSS)
1Computer Sciences Department. And use
Tutorial #3 Cascading Style Sheets. Review: Last Class Image sizing Pathnames Project Default Path Relative Path Absolute Path Blackboard Homework Submission.
New Perspectives on XML, 2nd Edition Tutorial 5 1 TUTORIAL 5 WORKING WITH CASCADING STYLE SHEETS.
XP New Perspectives on XML Tutorial 5 1 TUTORIAL 5 CSS Tutorial – Carey ISBN
Tutorial 4 Creating Special Effects with 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.
Doman’s Sections Information in this presentation includes text and images from
CSS: Cascading Style Sheets. 2 What are Style Sheets A style sheet is a mechanism that allows to specify how HTML (/XHTML/XML) pages should look The style.
Cascading Style Sheets by Pavlovic Nenad by. Presentation Contents  What is CSS?  Why CSS?  Types of Style Sheets  Style Sheets Syntax  Box Formatting.
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.
 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 by Pavlovic Nenad by. 2Cascading Style Sheets Presentation Contents What are CSS? What are CSS? History of CSS History of CSS.
Introduction to Programming the WWW I CMSC Summer 2004 Lecture 7.
Cascading Style Sheets Chapter Four. What are they? A set of style rules that tell the web browser how to present a web page or document. Cascading Style.
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.
MySQL and PHP Review CSS. Cascading Style Sheet (CSS) Style sheets are files or forms that describe the layout and appearance of a document. Cascading.
Lesson 03 // Cascading Style Sheets. CSS Stands for Cascading Style Sheets. We’ll be using a combination of Html and CSS to create websites. CSS is a.
Web Design and Development for Business Lecture 4 Cascading Style Sheet (CSS)
Cascading Style Sheets Robin Burke ECT 270. Outline Midterm The Layout Debate CSS properties Fonts Alignment Color CSS selection selectors pseudo-classes.
ECA225 Applied Interactive Programming Cascading Style Sheets, pt 1 ECA 225 Applied Online Programming.
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.
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.
1 Web Development CSS (Cascading Style Sheet). 2 1.Setting rules for multiple elements To decrease the amount of typing for setting rules for multiple.
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,
Managing Data on the World-Wide Web cs What is HTML? A Markup Language for representing documents text (data) structure appearance functionality.
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.
Web Technologies Beginning Cascading Style Sheets (CSS) 1Copyright © Texas Education Agency, All rights reserved.
CSS: Cascading Style Sheets Part II. Style Syntax.
1 CSS: Cascading Style Sheets W3Schools CSS tutorialCSS tutorial.
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.
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.
Cascading Style Sheets Robin Burke ECT 270. Outline CSS properties Fonts Alignment Color CSS selection selectors.
CONFIGURING COLOR AND TEXT WITH CSS Chapter 3. Cascading Style Sheets (CSS) Used to configure text, color, and page layout. Launched in 1996 Developed.
Cascading Style Sheets
Cascading Style Sheets
Cascading Style Sheets
IS333: MULTI-TIER APPLICATION DEVELOPMENT
Presentation transcript:

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 to be taken care of by the browser, without using any formatting tags. As the two major browsers - Netscape and Internet Explorer continued to add new HTML tags and attributes (ex: tag, color attribute) to the original HTML specification, it became more and more difficult to create Web sites where the content of HTML documents was clearly separated from the document's presentation layout.

3 What are Style Sheets A style sheet is a mechanism that allows to specify how HTML (/XHTML/XML) pages should look. The style is specified by style rules. The style rules appear either in the document or in external files, called style sheets.

4 Style Sheets A file that ends with.css Contains a list of style rules for HTML elements Case insensitive Comments are enclosed in /* */ Demo:

5 Without a style sheet

6 Simple Example A Joke A joke A mama tomato, a papa tomato and a baby tomato are walking down the street. The baby tomato keeps falling behind so the papa tomato goes back, steps on the baby tomato and says, ketchup ("Catch-up!"). The tag defines a division/section in a document.

7

8 Style File: joke.css body { background-image: url("water.jpg"); } h1 { background-color: green; color: rgb(250, 200, 250); /* pink */ font-family: cursive } p { background-color: yellow; color: purple; font-size: 200%;}

9 Simple Example (with css) A Joke A joke A mama tomato, a papa tomato and a baby tomato are walking down the street. The baby tomato keeps falling behind so the papa tomato goes back, steps on the baby tomato and says, ketchup ("Catch-up!").

10

11 Why do we Need a Style Sheet? Separates content from format In HTML 4.01 styling is very limited Consistent appearance over a site Allows to easily change style -In one page -In a whole site Reduces download time (how?) the same style sheet may apply to many pages in a website  reduces the need to send redundant presentation information over the network

Inserting Style to a Page

13 How and Where? The style rules appear either in the document or in external files, called style sheets -Inside a document, there are inline styles and embedded style sheets -External style sheets are either linked or imported

14 Inline Styles The style declaration appears as the value of the attribute style of the element Almost every tag can have the style attribute -Some exceptions: head, html, meta, style and title Use this method sparingly, such as when a style is to be applied to a single occurrence of an element. This text will be shown in italic green and the size will be 1.5 times the current font size

15 body {color: red; background: skyblue;} h1 { color: blue }... Embedded Style Inside the head element Use when a single document has a unique style.

16 Link External Style Sheets... Inside the head element Ideal when the style is applied to many pages.

17 rule imports style rules to the beginning of the style sheet rule imports the style rules of another style sheet Several import rules may appear Imported Style url(general.css); body { color: red; background:skyblue } h1 { color: blue } An Example:

Style Syntax

19 Style Rules h1 { color: purple; font-family: Impact, Arial Black; font-size-adjust:.46; font-size: 2.33em; font-weight:400; font-style:normal; text-decoration: none; word-spacing:normal letter-spacing:normal; text-transform: none; } Declaration Property Value(s) Selector

20 Style Rules (cont) A rule has the following form: selector {property: value} -Selector: HTML element/tag to define -Property: attribute to change -Value: value given to the property For example, the following rule applies to text that is inside a tag p {color: green; font-size: 1.5em; font-style: italic} em: the 'font-size' of the relevant font

21 What Kind of Properties can a CSS Style Sheet Change? Style properties Layout properties There are many properties and many possible values -We will not cover all of them here (list, table, border) -Everything is in the Web ! -A good source:

Style Properties

23 Our Examples We use the following HTML example: This is our example for css. The tag is used to group elements for formatting with styles -Extremely useful tag...

24 Font Properties Font properties: family, size, weight, style,... span { font-family: courier; font-size: 130%; font-style: italic; font-weight: bold}

25 Text Properties Text properties: color, transform, decoration, word-spacing, text-align, direction, … span { color: #00cc00; text-decoration: line-through; text-transform: uppercase}

26 Background Properties Background properties: background-color, background-image, background-repeat, … span {background-color: #00ff00} span {background-image: url('bg.gif');}

Layout Properties

28 Page Layout Each HTML element defines a layer (rectangular box) that is placed in some location on the page Layers are nested with correspondence to the nesting of their elements

29 Inline vs. Block Elements There are two types of elements: -Block elements: p, ol, table, div, h1, etc. -Inline elements: b, i, a, span, etc. Layers of block elements are separated from their adjacent elements (i.e., a new line before and after), while inline elements are not You can turn a block into an inline or block, using the display property, e.g., h1 { display: inline }

30 Positioning Elements Using CSS, you can define the position of an element inside its parent layer For that, use the properties position, left, right, top and bottom span { position:relative; left: 1cm; top: 1cm; color: #00cc00;}

31 Relative vs. Absolute In CSS, positions (of boxes) and sizes (of fonts and boxes) could be either relative or absolute In an “absolute” style -Font size is fixed -Sizes and positions of elements are fixed In a “relative” style -you can change the font size -The sizes and positions of elements may change when the size of the window is changed

32 Position Types But 1cm left of what?? For that, we have the position property Four position types: - static : the default position - relative : relative to the static position - absolute : relative to the parent layer coordinates - fixed : relative to the window coordinates

33 Position Examples span { position:static; left: 1cm; top: 1cm; color: #00cc00;} Totally Ignored! This is the default position type

34 Position Examples span { position:absolute; left: 1cm; top: 1cm; color: #00cc00;} span { position:fixed; left: 1cm; top: 1cm; color: #00cc00;}

35 More Layout Properties Layer properties - margin (-top, -bottom, -left, -right) Defines the space around elements - border (-width, -color, -style, … ) Defines the border around an element - padding (-top, -bottom, -left, -right) Defines the space between the element border and the element content Text Layout - direction, word-spacing, white-space, letter-spacing, line- height, text-align, text-indent, …

36 Length Units CSS has several types of length units: - em, ex : height of current fonts (relative) - px, in, cm, mm, pt, pc : international units - % : ratio of parent’s respective dimension A page should remain a proper layout when windows (and even fonts) resize -Hence, do not assume anything about default sizes, always check

Selector Types

38 Several Kinds of Selectors Type Selectors Class Selectors ID Selectors Attribute Selectors Universal Selector Descendant Selectors Pseudo-Class Selectors Pseudo-Element Selectors

39 Type Selector A type selector is the name of an element type A type selector matches every instance of the element type LI {color: red; font-size: 16px} Matches: An item Another item

40 Class Selector A class selector is a selector of the form x.y It matches x s that have the class attribute with value y LI.reditem {color: red} Matches: An item Another item.reditem { color: red} will also work!

41 ID Selectors IDs are identical to classes, except that there can only be one element with a given ID in a document LI#23 {color: red} Matches: An item Another item #23 { color: red} will also work!

42 Attribute Selectors p[ attribute ] -matches p when attribute is set to any value p[title=intro] or p[title="intro"] (the quotes are optional) -matches p when its title attribute is set to “ intro ” p[class~=green] -matches p when the class attribute value includes the word “ green ”

43 Universal Selector The universal selector matches every element The following rule means that all the text will have a size of 40px * {font-size: 40px }

44 Descendant Selector A descendant selector has the form S 1 S 2 where S 1 and S 2 are selectors It matches all elements that -match S 2, and - are descendants (nested in) elements that match S 1

45 An Example.reditem em {color: blue} Matches: This is not blue. This is blue and so is this. What will this match? p em {color: blue}

46 Pseudo-Classes Pseudo class selectors are similar to class selectors, but they match states rather than class values -For example, a link can be in the states: visited, active, mouse-over (“hover”), etc. -Another example: a text-area can be focused

47 Examples of Rules for Pseudo-Classes A:link {color: blue} A:visited {color: purple} A:hover {font-size: 1.5em} A:active {color: red} INPUT:focus {background-color: yellow}

48 The syntax of pseudo-elements: selector:pseudo-element {property: value} CSS classes can also be used with pseudo- elements: selector.class:pseudo-element {property: value} Pseudo-Elements

49 Grouping Selectors We can group several declarations by specifying several selectors, separated by commas For example, the following rule applies to all elements that match either H1, P B, or H2[class=largehead] P B, H1, H2.largehead {font-size: 120%}

50 Adding Style to Inner Text We want to add style to a fragment of some text We need to wrap the text with tags that do not have a style of their own: - Some text : does not stop the text flow - Some text : separated from the other text

Cascading and Inheritance

52 Cascading of Styles CSS merges style rules from different places (inline, embedded, linked and defaults) Different places may have conflicting style rules -conflicts may even arise in a single source The process of merging (cascading) styles from different places determines which style rules have higher priority

53 Determining Property Values Suppose that we would like to determine the value of property p for element e Choose all declarations that have a selector that matches e and have the property p Choose the first declaration in the cascading order, and apply the corresponding rule

54 Cascading Order The cascading order of declarations: 1.Primary sort: weight and origin 2.Secondary sort: class 3.Third sort: specificity of selectors 4.Final sort: order of appearance

55 Weight and Origin A style defined “closer” to a tag takes precedence: Author style sheets override user style sheets User style sheets override default (browser) style sheets !important declaration overrides normal declaration -body { color: black !important; background: white !important;} If both author and user declarations have !important, the author declarations still override the user declarations

56 Example (sort by class) Consider the two rules -P {…} -.introductory {…} If an element matches both, then the second rule has a higher specificity and overrides the first

57 An Example (specificity of selectors) Consider the following rules: - li {…} - #34 {…} - ul ol li.red {…} Which is the most specific? …there can only be one element with a given ID in a document

58 Order of Appearance If two rules have the same origin importance and specificity, then the one that appears last in the style sheet overrides the others Rules in imported style sheets are considered to appear before local rules

59 Inheritance of Properties If an element does not match any rule for some given property, then the element inherits the computed value for that property from its parent element For example, color, font and word-spacing are inherited

60 An Example (1) Given the rules: - body { font-size: 10pt } - h1 { font-size: 120% } What will be the font size of the element? A large heading Computed Value: 12pt

61 An Example (2) Given the rules: - body { font-size: 10pt; color=red; } - h1 { font-size: 120% } What will be the font color of the element? A red heading ?

62 Inheritance of Properties If some properties have been set for the same selector in different style sheets, the values will be inherited from the more specific style sheet. Example: Result (properties for h3 in the document): color :red text-align : right font_size:20 pt external: H3 { color : red; text-align : left; font-size : 8 pt; } internal: H3 { text-align : right; font-size : 20 pt; }

63 The End! These slides are based on those developed for the course: More information about CSS can be found at: Colors: very useful for styling!