SlideShare a Scribd company logo
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 sheet:
<style type="text/css">
#headline {
text-align: center;
}
.summary {
font-style: italic;
}
</style>
...
<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 XHTML content element.
Classes and IDs Compared
◼ 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.
At first glance, classes and IDs appear to function identically, but there
are important differences between the two:
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;
}
</style>
...
<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;
}
</style>
...
<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;
}
</style>
...
<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;
}
</style>
...
<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:
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.
<style type="text/css">
.style1 {
color: blue;
}
.style2 {
color: green;
}
</style>
...
<p>We can write text in <span
class="style1">blue</span> or
<span class="style2">green</span>
or any other color.</p>
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:
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.
Problematic names
.green
.underline
.center
.bigletters
Better names
.slogan
.booktitle
.caption
.headline

More Related Content

PPT
INFO3775 Chapter 2 Part 2
PPT
Unit 2-CSS & Bootstrap.ppt
PPT
Introduction to CSS in HTML.ppt
PDF
CSS Foundations, pt 1
PPTX
INTRODUCTIONS OF CSS
PPT
4.2 css classes
PPT
DW_lesson2aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.ppt
PPT
CSS Cascading Style Sheet Introduction slides
INFO3775 Chapter 2 Part 2
Unit 2-CSS & Bootstrap.ppt
Introduction to CSS in HTML.ppt
CSS Foundations, pt 1
INTRODUCTIONS OF CSS
4.2 css classes
DW_lesson2aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.ppt
CSS Cascading Style Sheet Introduction slides

Similar to 8--CSS-Classes-and-IDs.pdf for second to college (20)

PPT
DW_lesson2.ppt
PPT
DW_lesson2.ppt
PPT
DW_lesson2.ppt
PPT
DW_lesson2.ppt
PPT
DW_lesson2.ppt
PPT
DW_lesson2 ntro_HTML_CSS_preso ntro_HTML_CSS_preso.ppt
PPTX
Introduction to CSS.pptx
PPSX
CSS tutorial
PPTX
Lecture-6.pptx
PPT
IP - Lecture 6, 7 Chapter-3 (3).ppt
PPTX
Introduction to basics of css, overview, syntax and so on
PDF
Introduction to css
PPTX
Lecture 3CSS part 1.pptx
PPT
Shyam sunder Rajasthan Computer
PPT
CSS Basic and Common Errors
PPT
Advanced Cascading Style Sheets
PPTX
Cascading style sheet, CSS Box model, Table in CSS
PPT
CSS Methodology
PPTX
v5-introduction to html-css-210321161444.pptx
DW_lesson2.ppt
DW_lesson2.ppt
DW_lesson2.ppt
DW_lesson2.ppt
DW_lesson2.ppt
DW_lesson2 ntro_HTML_CSS_preso ntro_HTML_CSS_preso.ppt
Introduction to CSS.pptx
CSS tutorial
Lecture-6.pptx
IP - Lecture 6, 7 Chapter-3 (3).ppt
Introduction to basics of css, overview, syntax and so on
Introduction to css
Lecture 3CSS part 1.pptx
Shyam sunder Rajasthan Computer
CSS Basic and Common Errors
Advanced Cascading Style Sheets
Cascading style sheet, CSS Box model, Table in CSS
CSS Methodology
v5-introduction to html-css-210321161444.pptx
Ad

More from shwan it (12)

PPTX
created appliciatons with Ai programming
PPTX
Ai created application by used Ai program
PDF
CH 1 for computer since write for code .pdf
PDF
Lec2- for c++ code Video Display Devices.pdf
PDF
02_Configure_a_Network_Operating_System.pdf
PDF
11--CSS-Box-Model.pdf for second college
PDF
web design for html to second yea for college
PPTX
Blue Futuristic Illustrative Artificial Intelligence Project Presentation.pptx
PPTX
Lecture 02 - Logic Design(Number Systems).pptx
PDF
CS Lectures 1maher its sciences computer
PDF
Konsbruck.pdf
PDF
html5-tag-cheat-sheet-2019.pdf
created appliciatons with Ai programming
Ai created application by used Ai program
CH 1 for computer since write for code .pdf
Lec2- for c++ code Video Display Devices.pdf
02_Configure_a_Network_Operating_System.pdf
11--CSS-Box-Model.pdf for second college
web design for html to second yea for college
Blue Futuristic Illustrative Artificial Intelligence Project Presentation.pptx
Lecture 02 - Logic Design(Number Systems).pptx
CS Lectures 1maher its sciences computer
Konsbruck.pdf
html5-tag-cheat-sheet-2019.pdf
Ad

Recently uploaded (20)

PDF
Test slideshare presentation for blog post
PPT
Machine printing techniques and plangi dyeing
PPTX
EDP Competencies-types, process, explanation
PDF
Integrated-2D-and-3D-Animation-Bridging-Dimensions-for-Impactful-Storytelling...
PPTX
AD Bungalow Case studies Sem 2.pptxvwewev
PPTX
rapid fire quiz in your house is your india.pptx
PDF
Skskkxiixijsjsnwkwkaksixindndndjdjdjsjjssk
PPTX
LITERATURE CASE STUDY DESIGN SEMESTER 5.pptx
PDF
Design Thinking - Module 1 - Introduction To Design Thinking - Dr. Rohan Dasg...
PDF
Urban Design Final Project-Context
PPTX
HPE Aruba-master-icon-library_052722.pptx
PPTX
CLASS_11_BUSINESS_STUDIES_PPT_CHAPTER_1_Business_Trade_Commerce.pptx
PDF
Pongal 2026 Sponsorship Presentation - Bhopal Tamil Sangam
PPTX
Causes of Flooding by Slidesgo sdnl;asnjdl;asj.pptx
PPT
robotS AND ROBOTICSOF HUMANS AND MACHINES
PDF
Urban Design Final Project-Site Analysis
PPTX
Complete Guide to Microsoft PowerPoint 2019 – Features, Tools, and Tips"
PPT
pump pump is a mechanism that is used to transfer a liquid from one place to ...
PPTX
An introduction to AI in research and reference management
PDF
Chalkpiece Annual Report from 2019 To 2025
Test slideshare presentation for blog post
Machine printing techniques and plangi dyeing
EDP Competencies-types, process, explanation
Integrated-2D-and-3D-Animation-Bridging-Dimensions-for-Impactful-Storytelling...
AD Bungalow Case studies Sem 2.pptxvwewev
rapid fire quiz in your house is your india.pptx
Skskkxiixijsjsnwkwkaksixindndndjdjdjsjjssk
LITERATURE CASE STUDY DESIGN SEMESTER 5.pptx
Design Thinking - Module 1 - Introduction To Design Thinking - Dr. Rohan Dasg...
Urban Design Final Project-Context
HPE Aruba-master-icon-library_052722.pptx
CLASS_11_BUSINESS_STUDIES_PPT_CHAPTER_1_Business_Trade_Commerce.pptx
Pongal 2026 Sponsorship Presentation - Bhopal Tamil Sangam
Causes of Flooding by Slidesgo sdnl;asnjdl;asj.pptx
robotS AND ROBOTICSOF HUMANS AND MACHINES
Urban Design Final Project-Site Analysis
Complete Guide to Microsoft PowerPoint 2019 – Features, Tools, and Tips"
pump pump is a mechanism that is used to transfer a liquid from one place to ...
An introduction to AI in research and reference management
Chalkpiece Annual Report from 2019 To 2025

8--CSS-Classes-and-IDs.pdf for second to college

  • 2. 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.
  • 3. 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 sheet: <style type="text/css"> #headline { text-align: center; } .summary { font-style: italic; } </style> ... <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 XHTML content element.
  • 4. Classes and IDs Compared ◼ 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. At first glance, classes and IDs appear to function identically, but there are important differences between the two: 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.
  • 5. 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; } </style> ... <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.
  • 6. 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; } </style> ... <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.
  • 7. 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; } </style> ... <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.
  • 8. 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; } </style> ... <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.
  • 9. 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: 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. <style type="text/css"> .style1 { color: blue; } .style2 { color: green; } </style> ... <p>We can write text in <span class="style1">blue</span> or <span class="style2">green</span> or any other color.</p>
  • 10. 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: 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. Problematic names .green .underline .center .bigletters Better names .slogan .booktitle .caption .headline