Develop PHP Website for Learning Web Design Free Online
  Home   |   Forums   |   DevCMS
  Join   |   Log In
 
   

CSS Tutorials - Learn CSS

Tutor at Develop PHP
Style Classes and IDs
By: Adam      Created: Jul 14, 2008      Views: 4659 Tweet This Page  Post page to Facebook  Post page to Facebook

Now that we have our main.css style sheet linked to our index.html page we can add code into our HTML tags to have them styled individually or by group from the style sheet.

class - Used to group and style page tags. Multiple page tags can have the same class name applied.

id - Used to style individual tags on the page. Only one tag on the page can be tied to an id.

Here is an example of using the class and id attributes on different kinds of HTML tags to make all of them have the same style. In the example below we have given the <h2> tag the id attribute to make it behave slightly different from the other tags by applying "mystyle2" from the style sheet to it as well as the class style "mystyle1".

index.html
Learn HTML
<html>
<head>
<title>My Web Page</title>
<link href="style/main.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div class="mystyle1">Here is content in a div tag</div>
<hr class="mystyle1" / >
<p class="mystyle1">Here is content in a p tag</p>
<h2 class="mystyle1" id="mystyle2">Here is content in a H2 tag</h2>
<img src="images/my_pic.jpg" class="mystyle1" />
</body>
</html>

And this is what your style sheet would look like defining mystyle1 and mystyle2.

main.css
Learn HTML
/* We define a class style using a [.] */
.mystyle1 {
background-color:#333366;
color:#00FFFF;
padding: 8px;
letter-spacing:.5em;
border-width: thick;
border-style: solid;
border-color: #990000 #0000FF #996600 #009900;
}
/* We define an ID style using a [#] */
#mystyle2 {
width:350px;
height:150px;
margin-left:100px;
}


InfoYou can create as many styles in your style sheet as you like. Use them or not.


You can add the class or id attributes to most HTML tags. Experiment with any tags you want. Below we have given some examples for reference.

<div class="mystyle1">Text</div>
<p class="mystyle1">Text</p>
<table class="mystyle1">Text</table>
<hr class="mystyle1" / >
<h3 class="mystyle1">Text</h3>
<img class="mystyle1" src="images/my_pic.jpg" />


                                   CSS Attributes and Values Reference Guide


Comment on Style Classes and IDs




Search Tags:
Style   ·  Classes   ·  and   ·  IDs   ·  
 
 
Arbitrary Links and Archives
Home
Active Forums
Members
SIte News
Link To Us
Gear
2009 Forum Archive
Programming Courses
Learn HTML
Learn CSS
Learn PHP
Learn MySQL
Learn Javascript
Learn jQuery
Learn ActionScript 3.0
Learn Java
Learn XML
DevelopPHP Requires Flash Player
Get Adobe Flash player
___________________________________________

Terms of Use  •  Privacy  •  Admin Notes

©2010 developphp.com   |   Powered By FlashBuildingHolder