|
Search This Site
|
Id & Class In CSS
Id and Class is often times associated with the grouping of elements in CSS. Elements are grouped in order to apply styles to them. The id is used when you wish to apply a style to only one specific element per web page. The class on the other hand is used to apply a style to a set of elements. The class can be used numerous times on a single web page. To give you a better understanding of the concepts just defined, the examples below paint a picture. CSS IdWhen styling elements each id should be unique. You will need to specify a name preceded by the number sign ( # ). After you have done this, specify the style ( properties and their values ). On the web page, you will now have to pinpoint where the style should be applied by using the id selector. Let's demonstrate this more clearly. You can create the files shown below and try it for yourself. Name your html and css file anything you desire
so long as you can remember it and reference it. webpage.html <h3> New2HTML.com </h3> <h5> Read it. Learn it. Apply it. </h5>
style.css
#format
{
color: blue;
font-style: italic;
}
webpage.html ( Updated )
<h3> New2HTML.com </h3>
<h5 id="format"> Read it. Learn it. Apply it. </h5>
Note that nothing was done for the main heading so it remains unaltered. Do you think that if id="format" was to be applied it would work ? Perhaps it would, but this is an ERROR and if you were to validate your site (use tools to check it for errors) it would point it out to you. Always remember an id should be
unique and each id should only be used
once per web page. CSS Class A class is used when we wish to apply a style to more than one element.
Unlike the id, a single class can be used multiple times on a web page.
Let's follow up with a example. Observe below: webpage.html <h3> What is New2HTML ? </h3> <h5> A HTML, CSS and web building website. </h5>
style.css
.headings
{
color: purple;
}
webpage.html ( Updated ) <h3 class="headings"> What is New2HTML ? </h3> <h5 class="headings"> A HTML, CSS and web building website. </h5>
That's it for class and id, two of the very important features that we will use throughout the tutorials. Look out for them and continue to practice on your own to grasp this technique. Click the next lesson link below to proceed.  
Previous Lesson - Stylesheet Types
Next Lesson - Div & Span
|
|
© Copyright 2010 | All Rights Reserved
|