Web Design/CSS Classes

< Web Design

What are Classes in CSS?

To make a class, in between <style> & </style> type the name of the class following the tag name.

<style>
p.first { color: blue; }
</style>

Then in the HTML tag add the class

<p class="first">This is some text</p>

The last code will set the text color to blue for all of the 'p' elements with a class value of 'first'. It's also possible to apply the desired style to all of the elements with an specific class value no matter which element the class attribute is at. To do that, the '*' character is used.

<style>
*.first { color: blue; }
</style>


Example 1

The code below should make the:

Type the code below into your code editor and save the file as cssclass.html


<html>
<head>

<style type = "text/css">
p.first{ color: blue; }
p.second{ color: red; }
</style>

</head>

<body>

<p>This is a normal paragraph.</p>

<p class="first">This is a paragraph that uses the p.first CSS code!</p>

<p class="second">This is a paragraph that uses the p.second CSS code!</p>


Example 2

The code below should make the:

Type the code below into your code editor and save the file as cssclass2.html


<html>
<head>
<style>

h2 { color: red; font-size: 20px; } 
p.firstpara { background-color: gray; }
p.secondpara { background-color: red; }
p.thirdpara { 
	background: purple;
	color: white;
}

</style>
</head>
<body>

<h2>CSS Classes</h2>
<p class="firstpara">This is the p.firstpara paragraph</p>

<p class="secondpara">This is the p.secondpara paragraph</p>

<p class="thirdpara">This is the p.thirdpara paragraph</p>

<p>This has no CSS formatting</p>

</body>
</html>


References

http://www.tizag.com/cssT/class.php

This article is issued from Wikiversity - version of the Tuesday, May 03, 2011. The text is available under the Creative Commons Attribution/Share Alike but additional terms may apply for the media files.