1 / 13

Understanding ID and Class in CSS

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. Objectives. The student will

arch
Télécharger la présentation

Understanding ID and Class in CSS

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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. Objectives • 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.

  3. IDs and Classes • 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 whereidandclasscome 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).

  4. What is an ID? • Every element on your web page can be assigned a unique idattribute. • 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 <ul> that's used to markup a navigation menu might have id="navigation" or id="menu"

  5. When to use an ID • You assign an id attribute to an HTML element when: • You want to stylize that element differently than other elements of the same type. • 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. • 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

  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: <p id="alert">Important! All classes are cancelled today.</p> • 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. What is a Class? • 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.

  8. Class Example • 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 <div> 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 <div> elements that contain each review differently than other <div> 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. 

  9. Class Example • The HTML would look like this: <div class="review"> <h2>The Godfather</h2> <p class="summary">The Godfather (1972) is the greatest movie of all time, according to numerous surveys of movie critics and audiences alike.</p> <p>The Godfather is... (your review here)</p> </div> <div class="review"> <h2>Avatar</h2> <p class="summary">Avatar (2009) is the highest grossing film of all time, earning approximately $2.8 billion.</p> <p>Avatar is... (your review here)</p> </div> • 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. Descendant Selectors • 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.

  12. Descendant Example • Let's say you wanted all <a> 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 <ul> 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 <a> element that's inside of the<ul> element that has id="navigation". 

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

More Related