Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSS Classes.

Similar presentations


Presentation on theme: "CSS Classes."— Presentation transcript:

1 CSS Classes

2 What is a CSS Class? 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. 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.

3 Syntax and Use of CSS Classes:
<head> <style type="text/css"> .center { text-align:center; } </style> </head> Class definitions are placed along with all the other CSS inside the <head> section of the document. A CSS class is defined with a period, followed by the name of the class. <p>Paragraph 1</p> <p class="center">Paragraph 2</p> By adding the class="center" to this paragraph element, we are telling the browser to apply all formatting defined for that class to this particular paragraph. Paragraph 1 Paragraph 2

4 Syntax and Use of CSS Classes:
<head> <style type="text/css"> .special { text-align:center; color:green; font-style:italic; } </style> </head> As before, multiple styles can be defined in a single statement. Remember the semicolon at the end of each line. <p class="special">Paragraph</p> Paragraph

5 Syntax and Use of CSS Classes:
<head> <style type="text/css"> .center { text-align:center; } .green { color:green; .italic { font-style:italic; </style> </head> CSS classes can even be combined. The element will reflect the formatting definitions from all the classes being applied. <p class="center green italic">Paragraph</p> Paragraph

6 Syntax and Use of CSS Classes:
<head> <style type="text/css"> .green { color:green; } </style> </head> <h1 class="green">Heading</h1> <p class="green">Paragraph</p> <ul> <li>List Item 1</li> <li class="green">List Item 2</li> </ul> Generic classes like these can be applied to any element we choose. Heading Paragraph List Item 1 List Item 2

7 Syntax and Use of CSS Classes:
<head> <style type="text/css"> p.green { color:green; } </style> </head> You can also specify that only particular elements be affected by a class. In this example, we have defined the .green class to apply only to the paragraph element. <h1 class="green">Heading</h1> <p class="green">Paragraph</p> <ul> <li>List Item 1</li> <li class="green">List Item 2</li> </ul> If we try to apply the .green class to other elements, it will have no effect; the browser will simply ignore the class definition. Heading Paragraph List Item 1 List Item 2

8 The <span> element:
<head> <style type="text/css"> .green { color:green; } </style> </head> The <span> tag allows us to apply class styling to a specific section within an element. 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. <p>This is a <span class="green"> typical</span> paragraph</p> This is a typical paragraph.

9 Naming CSS Classes: 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! A much better approach is to use class names that describe the meaning of the content, not how it will look: Problematic names .green .underline .center .bigletters Better names .slogan .booktitle .caption .headline


Download ppt "CSS Classes."

Similar presentations


Ads by Google