Custom Classes Creating a Class Obviously the pseudo-classes available from CSS are not enough. Fortunately, you can create your own classes. A custom CSS class is one that holds one or more definitions of styles. To create a class, in the style section of the head code of a webpage or in a CSS file, start with a period and a name for the class. The name must start with a letter, followed by letters, digits, underscores, and/or dashes in any order of your choice. Here is an example: .presentation
{
}
If the name is in more than one word, you can combine them into one. Here are example: <html>
<head>
<style>
.maintitle {
}
An alternative is to use a dash between two words. Here are example: .main-title {
}
Remember that you can use uppercase letters in the name. They can be helpful to show the portions of a multi-part name. Here is an example: .MainTitle
{
}
Othewise, you can adopt a naming convension of your choice. In the same way, you can create other classes and as many styles as you want. Inside the curly brackets of a class, create the desired styles. Here is an example: .main-title { font-family: Georgia; color: maroon; font-size: 24pt } Applying a Style to an Element After creating a style, you can apply it to an HTML tag on the page. To do this, in the start tag, add an attribute named class and assign the name, without the period, of the created style to it. Here are examples: <!DOCTYPE html> <html> <head> <title>Energy</title> <style> li { font-family: Garamond; color: blue; font-size: 12pt; } .MainTitle { font-family: Georgia; color: red; font-size: 24pt; } .presentation { font-family: Times; color: black; font-size: 14px; } </style> <head> <body> <p class="MainTitle">Chemistry: Periodic Table</p> <p class="presentation">In chemistry, the periodic table is used to organize the chemical elements based on their atomic numbers, their electron arrangement, and other properties. Based on the atomic numbers, the first 12 elements are:</p> <ul> <li>Hydrogen</li> <li>Helium</li> <li>Lithium</li> <li>Beryllium</li> <li>Boron</li> <li>Carbon</li> <li>Nitrogen</li> <li>Oxygen</li> <li>Fluorine</li> <li>Neon</li> <li>Sodium</li> <li>Magnesium</li> </ul> <p class="presentation">Based on their properties, these 12 elements can be presented as follows:</p> <div align="center"> <table border=5> <tr> <td>Atomic Number</td> <td>Element</td> <td>Symbol</td> <td>Atomic Mass</td> </tr> <tr> <td>1</td> <td>Hydrogen</td> <td>H</td> <td>1.0079</td> </tr> <tr> <td>2</td> <td>Helium</td> <td>He</td> <td>4.002682</td> </tr> <tr> <td>3</td> <td>Lithium</td> <td>Li</td> <td>6.941</td> </tr> <tr> <td>4</td> <td>Berylium</td> <td>Be</td> <td>9.0122</td> </tr> <tr> <td>5</td> <td>Boron</td> <td>B</td> <td>10.811</td> </tr> <tr> <td>6</td> <td>Carbon</td> <td>C</td> <td>12.011</td> </tr> <tr> <td>7</td> <td>Nitrogen</td> <td>N</td> <td>14.0067</td> </tr> <tr> <td>8</td> <td>Oxygen</td> <td>O</td> <td>15.9994</td> </tr> <tr> <td>9</td> <td>Fluorin</td> <td>F</td> <td>18.998403</td> </tr> <tr> <td>10</td> <td>Neon</td> <td>Ne</td> <td>20.1797</td> </tr> <tr> <td>11</td> <td>Sodium</td> <td>Na</td> <td>22.989769</td> </tr> <tr> <td>12</td> <td>Magnesium</td> <td>Mg</td> <td>24.305</td> </tr> </table> <div> </body> </html> This would produce:
Classes and Links You can create a special class for all links or specific links in your webpage. To do this, after a and the name of the pseudo-class, add a period and the name of the custom class (no space). Then define the characteristics of the class. Here is an example: a:link.references { color: Red; font-size: 10pt; font-family: Courier New; } To apply the link, in the start tag of the a element, assign only the name of the class to the class attribute. Here is an example: <!DOCTYPE html> <html> <head> <title>Energy</title> <style> li { font-family: Garamond; color: blue; font-size: 12pt; } .MainTitle { font-family: Georgia; color: red; font-size: 24pt; } .presentation { font-family: Times; color: black; font-size: 14px; } a:link { color: Brown; font-size: 10pt; font-family: Verdana; } a:active { color: Cyan; font-size: 10pt; font-family: Verdana; } a:visited { color: DarkBlue; font-size: 10pt; font-family: Verdana; } a:hover { font-size: 10pt; color: DeepPink; font-family: Verdana; } a:link.references { color: Red; font-size: 10pt; font-family: Courier New; } a:active.references { color: Bisque; font-size: 10pt; font-family: Courier New; } a:visited.references { color: Blue; font-size: 10pt; font-family: Courier New; } a:hover.references { color: Maroon; font-size: 10pt; font-family: Courier New; } </style> <head> <body> <p class="MainTitle">Chemistry: Periodic Table</p> <p class="presentation">In chemistry, the periodic table is used to organize the chemical elements based on their atomic numbers, their electron arrangement, and other properties. Based on the atomic numbers, the first 12 elements are:</p> <ul> <li><a href="hydrogen.htm">Hydrogen</a></li> <li><a href="helium.htm">Helium</a></li> <li><a href="lithium.htm">Lithium</a></li> <li><a href="berylium.htm">Beryllium</a></li> <li><a href="boron.htm">Boron</a></li> <li><a href="carbon.htm">Carbon</a></li> <li><a href="nitrogen.htm">Nitrogen</a></li> <li><a href="oxygen.htm">Oxygen</a></li> <li><a href="fluorine.htm">Fluorine</a></li> <li><a href="neon.htm">Neon</a></li> <li><a href="sodium.htm">Sodium</a></li> <li><a href="magnesium.htm">Magnesium</a></li> </ul> <p class="presentation">Based on their properties, these 12 elements can be presented as follows:</p> <div align="center"> <table border=5> <tr> <td>Atomic Number</td> <td>Element</td> <td>Symbol</td> <td>Atomic Mass</td> </tr> <tr> <td>1</td> <td>Hydrogen</td> <td>H</td> <td>1.0079</td> </tr> <tr> <td>2</td> <td>Helium</td> <td>He</td> <td>4.002682</td> </tr> <tr> <td>3</td> <td>Lithium</td> <td>Li</td> <td>6.941</td> </tr> <tr> <td>4</td> <td>Berylium</td> <td>Be</td> <td>9.0122</td> </tr> <tr> <td>5</td> <td>Boron</td> <td>B</td> <td>10.811</td> </tr> <tr> <td>6</td> <td>Carbon</td> <td>C</td> <td>12.011</td> </tr> <tr> <td>7</td> <td>Nitrogen</td> <td>N</td> <td>14.0067</td> </tr> <tr> <td>8</td> <td>Oxygen</td> <td>O</td> <td>15.9994</td> </tr> <tr> <td>9</td> <td>Fluorin</td> <td>F</td> <td>18.998403</td> </tr> <tr> <td>10</td> <td>Neon</td> <td>Ne</td> <td>20.1797</td> </tr> <tr> <td>11</td> <td>Sodium</td> <td>Na</td> <td>22.989769</td> </tr> <tr> <td>12</td> <td>Magnesium</td> <td>Mg</td> <td>24.305</td> </tr> </table> <div> <p><a class="references" href="aboutus.htm">About Us</a> | <a href="resources.htm" class="references">Resources</a> | <a href="forum.htm" class="references">Forum</a></p> </body> </html> This would produce: In the same way, you can create different classes for different link portions. |