The Need for Classes and IDs

Similar documents
Three Ways to Use CSS:

ITNP43: HTML Lecture 4

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

CHAPTER 4 CASCADING STYLE SHEETS BASICS KEY CONCEPTS

What is CSS? NAME: INSERT OPENING GRAPHIC HERE:

INFORMATICA GENERALE 2014/2015 LINGUAGGI DI MARKUP CSS

CSS is applied to an existing HTML web document--both working in tandem to display web pages.

HTML Hyperlinks (Links)

INTRODUCTION TO CSS. Mohammad Jawad Kadhim

This document provides a concise, introductory lesson in HTML formatting.

Web Programming and Design. MPT Senior Cycle Tutor: Tamara Week 2

Web Development & Design Foundations with HTML5, 8 th Edition Instructor Materials Chapter 3 Test Bank

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

Setting a Background Image

CSS how to display to solve a problem External Style Sheets CSS files

COMP519 Web Programming Lecture 6: Cascading Style Sheets: Part 2 Handouts

CS 350 Internet Applications I Name: Exam II (CSS) October 29, 2013

Web Programming and Design. MPT Junior Cycle Tutor: Tamara Demonstrators: Aaron, Marion, Hugh

Web Development & Design Foundations with HTML5

COSC 2206 Internet Tools. CSS Cascading Style Sheets

DAY 4. Coding External Style Sheets

Web Development & Design Foundations with HTML5

CSS. Lecture 16 COMPSCI 111/111G SS 2018

Web Technology. Assignment 3. Notes: This assignment is individual assignment, every student should complete it by himself.

Chapter 1 Getting Started with HTML 5 1. Chapter 2 Introduction to New Elements in HTML 5 21

CSS CSS how to display to solve a problem External Style Sheets CSS files CSS Syntax

CSS. M hiwa ahamad aziz Raparin univercity. 1 Web Design: Lecturer ( m hiwa ahmad aziz)

CSS: The Basics CISC 282 September 20, 2014

CSS: Cascading Style Sheets

CSS Lecture 16 COMPSCI 111/111G SS 2018

Creating and Building Websites

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

CSS: formatting webpages

GIMP WEB 2.0 MENUS WEB 2.0 MENUS: HORIZONTAL NAVIGATION BAR CREATING AN HTML LIST

WEB DEVELOPMENT & DESIGN FOUNDATIONS WITH HTML5

Step 1- Creating a new XHTML Website (2points)

CSE 154 LECTURE 3: MORE CSS

HTMLnotesS15.notebook. January 25, 2015

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

Faculty of Engineering Department of Computer Engineering. CSS Tutorial. Eng. Ahmed J. Alajrami

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

CSS

UNIT -II. Language-History and Versions Introduction JavaScript in Perspective-

Using CSS in Web Site Design

Programmazione Web a.a. 2017/2018 HTML5

Using Dreamweaver CS6

Using Dreamweaver CC. 6 Styles in Websites. Exercise 1 Linked Styles vs Embedded Styles

CSS - Cascading Style Sheets

CSS.

CSS Scripting and Computer Environment - Lecture 09

COMP519 Web Programming Autumn Cascading Style Sheets

CSS Quiz Result. 2) Where in an HTML document is the correct place to refer to an external style sheet?

Cascading Style Sheets

Markup Language SGML: Standard Generalized Markup Language. HyperText Markup Language (HTML) extensible Markup Language (XML) TeX LaTeX

AIM. 10 September

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

CSS Selectors. Web Authoring and Design. Benjamin Kenwright

Using Dreamweaver. 6 Styles in Websites. 1. Linked or Imported Stylesheets. 2. Embedded Styles. 3. Inline Styles

What is a web site? Web editors Introduction to HTML (Hyper Text Markup Language)

Syllabus - July to Sept

The McGill Knowledge Base. Last Updated: February 17, 2016

Web Design and Application Development

MEXICO 2015, ilab XALAPA

Web Design and Development ACS-1809

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

HTML/XML. HTML Continued Introduction to CSS

HTML and CSS MIS Konstantin Bauman. Department of MIS Fox School of Business Temple University

Block & Inline Elements

Assignments (4) Assessment as per Schedule (2)

1. The basic building block of an HTML document is called a(n) a. tag. b. element. c. attribute. d. container. Answer: b Page 5

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

Introduction to Web Design CSS Reference

Introduction to Web Design CSS Reference

CSS Box Model. Cascading Style Sheets

Reading 2.2 Cascading Style Sheets

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

CSc 337 LECTURE 3: CSS

CITS1231 Web Technologies. Introduction to Cascading Style Sheets

Creating and Building Websites

.hedgehog { background-image: url(backyard.jpg); color: #ffff99; height: 6in; width: 12in; } .tube {

How to Control and Override CSS by Including a CSS Link

In the early days of the Web, designers just had the original 91 HTML tags to work with.

Dreamweaver CS6. Level 1. Topics Workspaces Basic HTML Basic CSS

Cascading Style Sheet

Chapter 7 Typography, Style Sheets, and Color. Mrs. Johnson

Motivation (WWW) Markup Languages (defined). 7/15/2012. CISC1600-SummerII2012-Raphael-lec2 1. Agenda

Hypertext Markup Language, or HTML, is a markup

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

Lesson 5 Introduction to Cascading Style Sheets

Html basics Course Outline

Cascading Style Sheets (CSS) Part 1 of 3: Introduction and overview

Cascading Style Sheets (CSS)

Downloads: Google Chrome Browser (Free) - Adobe Brackets (Free) -

Bixby Public Schools Course Essential Elements Grade: Desktop Publishing

Bridges To Computing

CASCADING STYLE SHEETS (CSS) BY: RIHAM ALSMARI, PNU. Lab 4

The Structure of the Web. Jim and Matthew

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

2005 WebGUI Users Conference

BIM222 Internet Programming

Transcription:

CSS Classes and IDs

The Need for Classes and IDs Recall our prior example of using CSS to style three paragraph elements differently: <p>normal text</p> <p style="font-weight:bold;">text in Bold</p> <p style="font-style:italic;">text in Italics</p> <p style="font-weight:bold;font-style:italic;">text in Bold and Italics</p> We had to use the inline style for these paragraphs since we had no other option with the tools we had learned to that point. If we had attempted to style the <p> element in an internal or external style sheet, the formatting rules would have applied to all the <p> elements, not specific ones. With CSS classes and IDs, we have much more power in styling specific elements on the page. Let's see how these work.

Syntax of Classes and IDs CSS style declarations for classes and IDs may be placed in internal and/or external style sheets. Here we're using an internal style <style sheet: type="text/css"> #headline { text-align: center;.summary { font-style: italic; <h1 id="headline">big News!</h1> <p class="summary">this is the text of the story summary. It should be shown in italics.</p> <p>this is the main story text. It has no special styling yet.</p> Styling declarations for IDs begin with the pound sign and for classes with a period. The id and class attribute, respectively, are added to page elements. The style, class, and id attributes are known as global attributes, meaning they can be assigned to nearly any HTML content element.

Classes and IDs Compared At first glance, classes and IDs appear to function identically, but there are important differences between the two: IDs can be applied to one and only one element in a web document. Classes can be applied to an unlimited number of elements, even different types of elements on the same page. If conflicting styles from an ID and class are applied to the same element, the ID will outrank the class in the cascade. Newcomers to CSS often get confused over classes and IDs and the differences between them, so we'll look at several examples. You might recall that we earlier used the id attribute to create a bookmark link. IDs (but not classes) are also used to manage page elements via JavaScript, a powerful scripting language that's beyond the scope of this introductory course.

Example: Multiple Classes, One Element Type Let's look at our earlier example again, but this time let's create three CSS classes to style the three paragraphs differently: <style type="text/css">.style1 { font-weight: bold;.style2 { font-style: italic;.style3 { font-weight: bold; font-style: italic; <p>normal text</p> <p class="style1">text in Bold</p> <p class="style2">text in Italics</p> <p class="style3">text in Bold and Italics</p> By using classes, we're no longer reliant on inline styles to format each instance of an element differently.

Example: One Class, Multiple Element Types A single CSS class may also be applied to different element types on the same page: <style type="text/css">.style1 { color: green; <h2>normal Heading</h2> <h2 class="style1">green Heading</h2> <p>normal text</p> <p class="style1">green text</p> <ul> <li>normal item</li> <li class="style1">green item</li> </ul> Here we're beginning to see the power of CSS classes. By changing the class style declaration in the style sheet, all page elements assigned to that class will be modified.

Example: Multiple Classes, One Element Multiple classes may also be applied to a specific element: <style type="text/css">.style1 { font-weight: bold;.style2 { font-style: italic;.style3 { color: green; <p>normal text</p> <p class="style1 style2 style3">green Text in Bold and Italics</p> We place a space between the different class names. There's no limit to the number of classes that can be applied to an element.

Example: Class and ID, Same Element Both an ID and a class may be applied to a specific element: <style type="text/css"> #style1 { font-weight: bold;.style2 { color: orange; <p>normal text</p> <p id="style1" class="style2">orange Bold Text</p> Remember that if there's a conflict between ID and class, the style from the ID will be applied, as ID takes priority in the CSS cascade. An ID is restricted for use with a single element in a web document. Using the same ID more than once on a page will result in an error. We'll use IDs sparingly in this course, preferring classes for their greater flexibility.

The <span> Element Sometimes we don't want a style to apply to an entire text element, but just a specific subset of text. For these instances, we use the handy <span> element: <style type="text/css">.style1 { color: blue;.style2 { color: green; <p>we can write text in <span class="style1">blue</span> or <span class="style2">green</span> or any other color.</p> Note that the <span> element does nothing on its own. Only once we associate it with a CSS class will the text inside the <span> be affected.

Naming Classes and IDs We should maintain the goal of keeping our content and presentation as separate as possible. Using class and ID names that describe how the element appears departs from this objective. We should try to use class names that describe the meaning of the content, not its appearance: Problematic names.green.underline.center.bigletters Better names.slogan.booktitle.caption.headline There's another hazard of using class names that describe the style. If we later change the class's appearance, we could create a confusing situation, for example a.green class that is styled to appear in blue! Class and ID names must be at least two characters and can contain lowercase letters, numbers, dashes, and underscores. Always start the name with a letter, not a number.