Presentation is loading. Please wait.

Presentation is loading. Please wait.

Understanding ID and Class in CSS Web Design – Sec 4-9 Part or all of this lesson was adapted from the University of Washington’s “Web Design & Development.

Similar presentations


Presentation on theme: "Understanding ID and Class in CSS Web Design – Sec 4-9 Part or all of this lesson was adapted from the University of Washington’s “Web Design & Development."— Presentation transcript:

1 Understanding ID and Class in CSS Web Design – Sec 4-9 Part or all of this lesson was adapted from the University of Washington’s “Web Design & Development I” Course materials

2  The student will  Be able to identify how ID attributes are used in CSS to enable styling of individual elements.  Be able to identify how Class attributes are used in CSS to enable styling of groups of elements.  Have added ID and Class attributes to certain elements within a web page. Objectives

3  You have added style to various elements in your portfolio page, but the styles you've added have affected all elements of a particular type.  For example, when you added style to the div element that affected all div elements equally.  What if you want to stylize some div elements one way, and other div elements a different way? That's where id and class come in. In this lesson you will learn how ID and Class attributes can be used to stylize individual elements (id) or groups of elements (class). IDs and Classes

4  Every element on your web page can be assigned a unique id attribute.  This can be any text you like, but it has to be unique (only one item can have this label).  It's a good practice to assign labels that describe the function of the element.  For example, a that's used to markup a navigation menu might have id="navigation" or id="menu" What is an ID?

5  You assign an id attribute to an HTML element when: 1.You want to stylize that element differently than other elements of the same type. 2.You want to be able to link to a particular element within a web page. In fact, you already added id="main" to one of the div elements on your portfolio page when you created a "skip to main content" link. 3.You want to be able to directly access that element using Javascript. You'll learn more about that in the lesson on Javascripts.  Today we’re just looking at reason 1 When to use an ID

6 ID Example  Let's say you have a paragraph that serves as an alert on a page. You don't want it to look like all the other paragraphs on the page. You want it to stand out so people are sure to notice it. You could add id="alert" to that paragraph, like this: Important! All classes are cancelled today.  To add style to an element with an id, you preface the id with a # symbol in your CSS: #alert { color: black; font-weight: bold; background: #FFFF66; /* yellow */ }

7  A class allows you to stylize a group of elements the same way.  Unlike ID, you can use the same class name for multiple items. What is a Class?

8  You're creating an entertainment web page that includes several movie reviews, in addition to other content. The entire content of each review (a heading with the movie title, plus several paragraphs) is wrapped in a in order to keep it all together in one box. The first paragraph of each review is a short summary describing the movie, then all remaining paragraphs contain the content of your critique of the movie. You may want to stylize the elements that contain each review differently than other elements on the page, so reviews all have a distinct look, but consistent with one another. You could accomplish this by assigning class="review" to each of these reviews. Also, you might want the summary paragraph to have a distinctive look, different from all other paragraphs. You could accomplish this by assigning class="summary" to each summary paragraph. Class Example

9  The HTML would look like this: The Godfather The Godfather (1972) is the greatest movie of all time, according to numerous surveys of movie critics and audiences alike. The Godfather is... (your review here) Avatar Avatar (2009) is the highest grossing film of all time, earning approximately $2.8 billion. Avatar is... (your review here)  The CSS code might look like this: div.review { background-color: #E6EBF5; border: 1px solid #003399; } p.summary { font-style: italic; }

10 Class Example - Result

11  In addition to assigning styles to a class of elements, you can also assign styles to descendants of those elements.  A descendant is an element that is nested inside another element. Descendant Selectors

12  Let's say you wanted all elements (links) on a page to be red and underlined. Here's how you could accomplish that in CSS: a { color: red; text-decoration: underline; }  However, let's also say you wanted a different style for links that are part of your navigation menu. You want them to be white text on a dark blue background, with no underline. Assuming these links are contained within a with id="navigation", you could stylize those using descendant selectors in CSS, like so: ul#navigation a { color: white; background-color: #003399; /* dark blue */ text-decoration: none; }  the selector ul#navigation a tells the browser to apply the style definition to every element that's inside of the element that has id="navigation". Descendant Example

13  Download the instructions for today.  When you are done, in your index.html, every tag will have either an id or class attribute and the tags for the reflections you haven’t written will also have a class attribute.  You also need to update two tags in all your other html files. Rest of Today


Download ppt "Understanding ID and Class in CSS Web Design – Sec 4-9 Part or all of this lesson was adapted from the University of Washington’s “Web Design & Development."

Similar presentations


Ads by Google