4.2 css classes


Published on

Published in: Education, Technology
  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

4.2 css classes

  1. 1. CSS Classes
  2. 2. What is a CSS Class? <ul><li>A CSS class provides us with a tool to create custom styles for elements on our web pages. Using classes can save us from typing the same formatting code for numerous elements. Classes enable us to make wide-scale styling changes very quickly. </li></ul><ul><li>In our earlier examples of using CSS, we applied the same formatting to all instances of a particular element. A CSS class allows us to define different styles for the same element type . For example, we can make one paragraph appear in green text and another paragraph show in red text. </li></ul>
  3. 3. Syntax and Use of CSS Classes: Class definitions are placed along with all the other CSS inside the <head> section of the document. <head> <style type=&quot;text/css&quot;> .center { text-align:center; } </style> </head> <p>Paragraph 1</p> <p class=&quot;center&quot; >Paragraph 2</p> A CSS class is defined with a period, followed by the name of the class. Paragraph 1 Paragraph 2 By adding the class=&quot;center&quot; to this paragraph element, we are telling the browser to apply all formatting defined for that class to this particular paragraph.
  4. 4. Syntax and Use of CSS Classes: As before, multiple styles can be defined in a single statement. Remember the semicolon at the end of each line. <head> <style type=&quot;text/css&quot;> .special { text-align:center; color:green; font-style:italic; } </style> </head> <p class=&quot;special&quot; >Paragraph</p> Paragraph
  5. 5. Syntax and Use of CSS Classes: CSS classes can even be combined. The element will reflect the formatting definitions from all the classes being applied. <head> <style type=&quot;text/css&quot;> .center { text-align:center; } .green { color:green; } .italic { font-style:italic; } </style> </head> <p class=&quot;center green italic&quot; >Paragraph</p> Paragraph
  6. 6. Syntax and Use of CSS Classes: Generic classes like these can be applied to any element we choose. <head> <style type=&quot;text/css&quot;> .green { color:green; } </style> </head> <h1 class=&quot;green&quot; >Heading</h1> <p class=&quot;green&quot; >Paragraph</p> <ul> <li>List Item 1</li> <li class=&quot;green&quot; >List Item 2</li> </ul> <ul><li>Heading </li></ul><ul><li>Paragraph </li></ul><ul><li>List Item 1 </li></ul><ul><li>List Item 2 </li></ul>
  7. 7. Syntax and Use of CSS Classes: You can also specify that only particular elements be affected by a class. <head> <style type=&quot;text/css&quot;> p.green { color:green; } </style> </head> <h1 class=&quot;green&quot; >Heading</h1> <p class=&quot;green&quot; >Paragraph</p> <ul> <li>List Item 1</li> <li class=&quot;green&quot; >List Item 2</li> </ul> <ul><li>Heading </li></ul><ul><li>Paragraph </li></ul><ul><li>List Item 1 </li></ul><ul><li>List Item 2 </li></ul>In this example, we have defined the .green class to apply only to the paragraph element. If we try to apply the .green class to other elements, it will have no effect; the browser will simply ignore the class definition.
  8. 8. The <span> element: The <span> tag allows us to apply class styling to a specific section within an element. <head> <style type=&quot;text/css&quot;> .green { color:green; } </style> </head> <p>This is a <span class=&quot;green&quot;> typical </span> paragraph</p> This is a typical paragraph. Note that the <span> tag does nothing on its own. Only when we associate it with a class does it make a change to how the page displays.
  9. 9. Naming CSS Classes: <ul><li>Give some thought to how you name your CSS classes. By using class names that describe how the element will look (.green, .center), we can make things confusing in the future. What if we later change our green text to be red? We would have the .green class display in red! </li></ul><ul><li>A much better approach is to use class names that describe the meaning of the content, not how it will look: </li></ul><ul><li>Problematic names </li></ul><ul><ul><li>.green </li></ul></ul><ul><ul><li>.underline </li></ul></ul><ul><ul><li>.center </li></ul></ul><ul><ul><li>.bigletters </li></ul></ul><ul><li>Better names </li></ul><ul><ul><li>.slogan </li></ul></ul><ul><ul><li>.booktitle </li></ul></ul><ul><ul><li>.caption </li></ul></ul><ul><ul><li>.headline </li></ul></ul>