CSS: Cascading Style Sheets

Similar documents
CSS.

CSS: The Basics CISC 282 September 20, 2014

CSS Cascading Style Sheets

CSS Selectors. element selectors. .class selectors. #id selectors

Cascading Style Sheets (CSS)

BIM222 Internet Programming

CMPT 165 INTRODUCTION TO THE INTERNET AND THE WORLD WIDE WEB

Creating and Building Websites

<body bgcolor=" " fgcolor=" " link=" " vlink=" " alink=" "> These body attributes have now been deprecated, and should not be used in XHTML.

Assignments (4) Assessment as per Schedule (2)

3.1 Introduction. 3.2 Levels of Style Sheets. - The CSS1 specification was developed in There are three levels of style sheets

- The CSS1 specification was developed in CSS2 was released in CSS2.1 reflects browser implementations

COSC 2206 Internet Tools. CSS Cascading Style Sheets

Chapter 3 Style Sheets: CSS

The Benefits of CSS. Less work: Change look of the whole site with one edit

CSS. Selectors & Measurments. Copyright DevelopIntelligence LLC

Introduction to Web Tech and Programming

CSS مفاهیم ساختار و اصول استفاده و به کارگیری

Cascading Style Sheets Level 2

Cascading Style Sheets

8/19/2018. Web Development & Design Foundations with HTML5. Learning Objectives (1 of 2) Learning Objectives (2 of 2)

Appendix D CSS Properties and Values

Web Information System Design No.4 Put Style to Web Documents. Tatsuya Hagino

Session 4. Style Sheets (CSS) Reading & References. A reference containing tables of CSS properties

CSS Cascading Style Sheets

- The CSS1 specification was developed in CSSs provide the means to control and change presentation of HTML documents

2. Write style rules for how you d like certain elements to look.

8a. Cascading Style Sheet

ITNP43: HTML Lecture 4

CSS. Lecture 16 COMPSCI 111/111G SS 2018

HTML and CSS COURSE SYLLABUS

Study Guide 2 - HTML and CSS - Chap. 6,8,10,11,12 Name - Alexia Bernardo

HTML & CSS. SWE 432, Fall 2017 Design and Implementation of Software for the Web

CSS Lecture 16 COMPSCI 111/111G SS 2018

CSS FOUNDATIONS IN-DEPTH. The nitty-gritty of css...

Review Question 1. Which tag is used to create a link to another page? 1. <p> 2. <li> 3. <a> 4. <em>

Introduction to Multimedia. MMP100 Spring 2016 thiserichagan.com/mmp100

INTRODUCTION TO CSS. Mohammad Jawad Kadhim

HTML Summary. All of the following are containers. Structure. Italics Bold. Line Break. Horizontal Rule. Non-break (hard) space.

CSC309 Programming on the Web week 3: css, rwd

Block & Inline Elements

Client-Side Web Technologies. CSS Part I

DAY 4. Coding External Style Sheets

<style type="text/css"> <!-- body {font-family: Verdana, Arial, sans-serif} ***set font family for entire Web page***

CSS for Styling CS380

CSS: Formatting Text. CSS for text processing: font-family. Robert A. Fulkerson

Deccansoft Software Services

To link to an external stylesheet, the link element is placed within the head of the html page:

Cascading Style Sheets. Overview and Basic use of CSS

EECS1012. Net-centric Introduction to Computing. Lecture 3: CSS for Styling

Tutorial 3: Working with Cascading Style Sheets

Chapter 4 CSS basics

HTML/XML. HTML Continued Introduction to CSS

Cascading Style Sheet. Styles as Tag Attributes. Syntax. <h1>: what font type/size is used? STYLE = SELECTOR {RULES} Attributes such as bgcolor

CSS

Unit 10 - Client Side Customisation of Web Pages. Week 5 Lesson 1 CSS - Selectors

2005 WebGUI Users Conference

Session 3.1 Objectives Review the history and concepts of CSS Explore inline styles, embedded styles, and external style sheets Understand style

Reading 2.2 Cascading Style Sheets

ADDING CSS TO YOUR HTML DOCUMENT. A FEW CSS VALUES (colour, size and the box model)

Adding CSS to your HTML

Cascade Stylesheets (CSS)

CSS: Cascading Style Sheets

CSC 443: Web Programming

CSS Styles Quick Reference Guide

INFORMATICA GENERALE 2014/2015 LINGUAGGI DI MARKUP CSS

CSS: formatting webpages

Parashar Technologies HTML Lecture Notes-4

Cascading Style Sheets (CSS)

CSc 337 LECTURE 3: CSS

IMY 110 Theme 6 Cascading Style Sheets

Introduction to CSS. Fijihosting.com Introduction to CSS page 1. What are style sheets? Lovely! How do I use them?

CSS3 Basics. From & CSS Visual Dictionary Learning Curve Books, LLC

Text and Layout. Digital Multimedia, 2nd edition Nigel Chapman & Jenny Chapman Chapter 11. This presentation 2004, MacAvon Media Productions

ID1354 Internet Applications

Zen Garden. CSS Zen Garden

Web Engineering CSS. By Assistant Prof Malik M Ali

Web Site Design. Stanford University Continuing Studies CS 03. Mark Branom

Tutorial 4: Creating Special Effects with CSS

CASCADING STYLESHEETS

3.1 Introduction. 3.2 Levels of Style Sheets. - HTML is primarily concerned with content, rather than style. - There are three levels of style sheets

INTERNATIONAL UNIVERSITY OF JAPAN Public Management and Policy Analysis Program Graduate School of International Relations

Networks and Web for Health Informatics (HINF 6220) Tutorial 8 : CSS. 8 Oct 2015

1/6/ :28 AM Approved New Course (First Version) CS 50A Course Outline as of Fall 2014

Web Development & Design Foundations with HTML5

First Name Last Name CS-081 March 23, 2010 Midterm Exam

CSE 154 LECTURE 3: MORE CSS

Setting a Background Image

Web Site Design and Development Lecture 5

CMPT 165 Advanced XHTML & CSS Part 3

Web Development & Design Foundations with HTML5

CSS. Shan-Hung Wu CS, NTHU

COMS 359: Interactive Media

LBS Polytechnic. Hey! Make With The Style Sheet Already, Bub!

Three Ways to Use CSS:

IDM 221. Web Design I. IDM 221: Web Authoring I 1

COMP519 Web Programming Lecture 7: Cascading Style Sheets: Part 3 Handouts

COPYRIGHTED MATERIAL. Contents. Chapter 1: Creating Structured Documents 1

Unveiling the Basics of CSS and how it relates to the DataFlex Web Framework

Fundamentals of Web Programming a

Transcription:

What are Style Sheets CSS: Cascading Style Sheets Representation and Management of Data on the Internet, CS Department, Hebrew University, 2007 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 W3Schools CSS tutorial 1 2 Style Sheets Usually, a file that ends with.css For example: Without a style sheet (i.e., with the default style definitions) - i.a.cnn.net/cnn/.element/ssi/css/1.1/main.css (CNN) - http://www.huji.ac.il/styles/style.css (HUJI) To attach a style sheet to an HTML file, add <link rel="stylesheet" type="text/css" href="css-file"/> to the head You can link to more than one css file 3 4 Simple Example without css <html> <head><title>a Joke</title></head> <body> <div><img src="tomato.gif" alt="joke"/></div> <h1>a joke</h1> div defines a division/section in a document. Using div you to group block elements and give them an equal style <p>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!"). </p> </body> </html> 5 6 1

Style File: joke.css Simple Example - with css body { background-image: url("bg.gif"); } h1 { background-color: green; color: rgb(250, 200, 250); /* pink */ font-family: cursive} p { background-color: yellow; color: purple; font-size: 200%;} 7 <html> <head><title>a Joke</title> <link rel="stylesheet" type="text/css" href="joke.css"/> </head> <body> <div><img src="tomato.gif" alt="joke"></div> <h1>a joke</h1> <p>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!"). </p> </body> </html> Open joke.html, joke_css.html 8 Background: Style in Legacy HTML In traditional HTML, every HTML tag which supported changing its font/colour/etc, supported this using a different syntax. Even tags which provided attributes which control their appearance, allowed for very limited customization. At a certain point, it was clear that because of these issues, the entire styling part of HTML needed to be revamped. One possibility to address this could have been adding a great number of standard styling attributes (spacing, font size, location, etc ) to all tags. Customizing each and every tag Why is this approach bad? with >10 attributes would have made HTML pages impossible to 9 write without automated tools! 10 The Solution: Style Sheets The Transition to Style Sheets Separates content from style Keeps HTML pages human-readable (how?) Reduces download time (how?) Allows to easily maintain a consistent appearance over a whole Web site (how?) A more flexible, unified way to specify style properties for different HTML elements (tables, paragraphs, etc ) 11 Style Sheets provide far more control over the appearance of the document than was available before but they also create redundancy: they provide new ways to accomplish tasks which were already possible. Once style sheets were introduced, most of the legacy features which provided equivalent functionality were deprecated in HTML 4.01 and in Transitional XHTML and were removed in Strict XHTML. It is good practice to use strict XHTML! 12 2

h1 { Style Rules Style Rules (cont) color: purple; Selector font-family: Impact, Arial Black; font-size-adjust:.46; Declaration A rule has the following form selector {declaration block} font-size: 2.33em; Property font-weight:400; Value(s) font-style:normal; text-decoration: none; word-spacing:normal!important; letter-spacing:normal; important rule The selector determines when the rule is applied For example, the following rule applies to text that is inside a <p> tag p{color: green; font-size: 1.5em; font-style: italic} text-transform: none; } 13 em is the current font-size of the current element 14 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 - Look in the Web!!! Style Properties 15 16 Our Examples We use the following HTML example: This is <span> our example </span> for css. The <span> tag is used to group inline elements for formatting with styles - Extremely useful tag... Font Properties Font properties: family, size, weight, style, variant,... span { font-family: courier; font-size: 130%; font-style: italic; font-weight: bold} Enable to transform letters into small-caps 17 18 3

Text Properties Text properties: color, transform, decoration, Transforms the letters case (upper / lower / capital) span { color: #00cc00; text-decoration: line-through; text-transform: uppercase} Background Properties Background properties: background-color, background-image, span {background-color: #00ff00} span {background-image: url('bg.gif');} 19 20 Page Layout Layout Properties 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 21 22 Inline vs. Block Elements Positioning Elements There are two type of elements: Block elements: p, ol, table, div, h1, etc. Inline elements: b, i, a, span, cite, 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 and vice-versa (highly useful for img elements, among others), using the display property, e.g., h1 { display: inline } 23 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; distance from left color: #00cc00;} distance from top 24 4

Position Types Position Examples But 1cm left to what?? For that, we have the position property Four position types are supported: - static: the default position - relative: relative to the static position - absolute: relative to the parent layer coordinates - fixed: relative to the window coordinates (remains at span { position:absolute; left: 1cm; top: 1cm; color: #00cc00;} span { position:fixed; left: 1cm; top: 1cm; color: #00cc00;} the same position regardless of scrolling) 25 26 Position Examples More Layout Properties Totally Ignored! span { position:static; left: 1cm; top: 1cm; color: #00cc00;} This is the default position type Layer properties - margin-top (-bottom, -left, -right) - padding-top (-bottom, -left, -right) - border-width (-color, -style, ) Text Layout The space around elements. Could be negative The space between the element borders and the element content. - direction, word-spacing, white-space, letterspacing, text-align, text-indent, Open joke1.html 27 28 Length Units CSS has several types of length units: em is the distance between the bottom of one row and the next (something like a grid size). - em, ex: height of current fonts (ex is the x-height) - px, in, cm, mm, pt, pc: international units - %: ratio of parent s respective dimension the height of a lowercase x. A page should remain a proper layout when fonts and/or windows are resized (usually by the user) - Hence, do not assume anything about default sizes Selector Types 29 30 5

Several Kinds of Selectors Type Selector Type Selectors Class Selectors ID Selectors A type selector is the name of an element type A type selector matches every instance of the Attribute Selectors Universal Selector Child Selectors Adjacent-Sibling Selectors Descendant Selectors Pseudo-Class Selectors Pseudo-Element Selectors Not supported by IE 5, 5.5 and 6. The good news (to us): supported by IE 7 The bad news (to others): can no longer be used to hide CSS code from IE 31 element type li {color: red; font-size: 16px} Matches: <ol> <li> An item </li> <li class="reditem"> Another item </li> </ol> 32 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 } Attribute Selectors p[title] - matches p when its title 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 33 34 Class Selector ID Selectors A class selector is a selector of the form x.y It matches xs that have the class attribute with value y (i.e., it is a shorthand for x[class=y]) li.reditem {color: red} Matches:.reditem {color: red} will <ol> also work! (it will match any element <li> An item </li> with class="reditem ) <li class="reditem"> Another item </li> </ol> 35 IDs are similar to classes, except that there can only be one element with a given ID in a document That is, an ID is unique per element li#23 {color: red} Matches: <ol> <li> An item </li> <li id="23"> Another item </li> </ol> #23 { color: red} will also work! 36 6

Descendant/Child/Sibling Selector emphasized text An Example A descendant selector has the form S 1 S 2 where S 1 and S 2 are (possible complex) selectors It matches all elements that - match S 2, and - are descendants (nested in any level in) elements that match S 1 To match only immediate descendants (children), use a Child Selector which has the form S 1 >S 2 To match S 2 immediately following S 2, use an Adjacent p em {color: blue} Matches: This is <em>not blue</em>. <p> This is <em> blue </em> <span><i>and so is <em> this </em></i></span>. </p> What will this match? Sibling Selectore which has the form S 1 +S 2.href div>span em {color: blue} 37 38 Pseudo-Classes Examples of Rules for Pseudo-Classes Pseudo class selectors are similar to class a:link {color: blue} 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 39 a:visited {color: purple} a:hover {font-size: 1.5em} a:active{color: red} input[type=text]:focus {background-color: yellow} when typing a text into a text input box (meaningful with other input types as well) 40 Pseudo-Elements Grouping Selectors Pseudo element selectors select abstract elements which are not specified as elements in the source HTML. - For example, to transform the first line of every p into uppercase, use: P:first-line {text-transform: uppercase} Why can t this be faked by enclosing the first line with a span? The definition of first line might change, for example, if the user resizes the window 41 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%} 42 7

Inline Styles Inserting Style to a Page In an inline style, the declaration block is the value of the attribute style of the element <p style="color: green; font-size: 1.5em; font-style: italic"> This text will be shown in italic green and the size will be 1.5 times the current font size </p> 43 Almost every tag can have the style attribute - exceptional: base, head, html, meta, param, script, style and title 44 Document-Level Style Imported Style Sheets <html> <head> <style type="text/css"> The @import rule imports the style rules of another style sheet body {color: red; background: skyblue;} Usage: @import url(file.css) h1 { color: blue } </style> Several import rules may appear at the beginning of the style sheet </head> <body>... </body> Import rules can appear in embedded style sheets or in external style sheets </html> 45 46 Imported Style Sheets An Example: @import url(general.css); body { color: red;background:skyblue } h1 { color: blue } Inheritance and Cascading Why do we need the import command when we have the <link> tag? Using @import in a css file, one can create stylesheets which are based on others 47 48 8

Inheritance of Properties Consider a property of an element that does not match any rule For some properties (inherited properties), the computed value of this property is inherited from the parent of the element For example, color, font and word-spacing are inherited Number of white spaces between words Yet border, margin and padding are not! An Example Given the rules: - body { font-size: 10pt } Computed Value: 12pt - h1 { font-size: 120% } What will be the font size of the <em> element? <body> <h1>a <em>large</em> heading</h1> </body> 49 50 Cascading of Styles CSS merges style rules from different places (inline, document-level, 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 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 Sort all declarations according to cascading order Choose the first declaration in the cascading order, and apply the corresponding rule 51 52 Cascading Order Importance of Origin The cascading order of declarations: 1. Primary sort: importance of origin 2. Secondary sort: specificity of selectors 3. Final sort: order of appearance 53 There are two rule origins: author and browser (either defaults or user customizations) strongest weakest browser!important rules author!important rules author rules browser rules For example, you can add stylesheets to IE in the following way: Tools internet options Accessibility User style sheet. Of course you can add!important browser rules this way as well 54 9

strongest weakest Specificity of Selectors is rule in style attribute? number of ID attributes number of attributes and pseudo-classes number of element names An Example Consider the following rules: - li { } - #x34y { } - ul ol li.red { } Which is the most specific? li.red is a descendent of ol, which is a descendant of ul 55 56 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 (@import) are considered to appear before local rules 57 10