Welcome Guest ( Log In | Register)



 
Reply to this topicStart new topic
> Css Multiclassing, Applying more than 1 class to an element
sonesay
post Mar 1 2008, 02:06 AM
Post #1


|||[ n00b King ]|||
*********

Group: [HOSTED]
Posts: 642
Joined: 20-June 07
From: Auckland
Member No.: 45,102



There may be situations where you would like parts of a same style to be used through out your website but also need things a bit differently across certain elements. A combination of #ID and .Class selectors can usually do the job but there will be cases where a combination of of multiple classing would be better for the job.

To try and make this situation more clear for you to understand lets take a look at an example. We have a website that has many sections in it that we need consistent styling too. A news section, forms, portfolio items.. How we need the styling done for these 3 items is each will be contained within a border and have a background color fill.

we could solve this problem by using a combination of #ID and .Class selectors.
CODE

.news_container {
border: solid 1px #aaaaaa;
background-color: #eeeeee;
width:80%;
}

.forms {
border: solid 1px #aaaaaa;
background-color: #eeeeee;
width: 400px;
}
.item {
border: solid 1px #aaaaaa;
background-color: #eeeeee;
width:300px;
}
#add_form{
width:500px;
color:0000ff;
}
#delete_form{
width:500px;
color:#ff0000;
}

Here by using a combination we can control how our elements will get displayed to some extent without too much repeating code, But there is still repeating code across .news_container, .forms, .item with the background and border declaration's. Now if we wanted to change the color of the border and fill we could have to change it in 3 places. This may not be much of an issue if we didn't have too many sections but it can be annoying and a pain if you have to change it in 10 places if your site is very large.

Now you might think why not just create one class for the border and file style across multiple elements and apply it that and then use the ID to controls the widths. This would work if we only had 1 element of each so the using ID's would be valid. Remember we can only have 1 unique ID per element. As for our news and portfolio items there can be many so that wont work alone.

Here is where multi classing can help. You can break down what is consistent across your elements and give that a class of its own. In our example its the border and fill we want. Next identifiy what other classes you'll need again in our example its the news_container class with a width specific to news and a portfolio item class with its own width. We can then apply the base class of the element with the specific class to it(Multiclassing).


Heres the solution (Multiclassing)
CODE

.box{
border: solid 1px #aaaaaa;
background-color: #eeeeee;
}
.news_container {
width:80%;
}

.forms {
width: 400px;
}
.item {
width:300px;
}
#add_form{
width:500px;
color:0000ff;
}
#delete_form{
width:500px;
color:#ff0000;
}


We can use it in this fashion to address our problem
CODE


<div class='news_container box'>
</div>

<div class='forms box'>
</div>

<div class='item box'>
</div>



Now all our elements are making use of the base class .box which has the consistent styling while also getting the specific styles associate with their kind of element. Now when you need to change the style of the border and fill you only have to do it in one place. This will help you cut down on your CSS code and make maintaining code much easier. The only thing you will need to do is think about what you need thats consistent across the different types of elements on your page and break them down into classes.

Note. You need spacing between classes.

I just learned this last night and cleaned up my own CSS code a bit with it. I hope it helps if you didn't know about it like I did.

Peace
Sone.
Go to the top of the page
 
+Quote Post
who?
post Jul 9 2008, 11:19 PM
Post #2


Newbie [Level 1]
*

Group: Members
Posts: 12
Joined: 10-April 06
Member No.: 21,606



As you discovered that recently (not so recently, but I'm adding this info anyway biggrin.gif), multiclassing follows a certain hierarchy. For example, if you define the same property in several classes and you apply those classes to an element, the value of that property to be applied will be the one defined in the last class on the class attribute.

In this Practical Example, we will use the "background-color" property defined over three different classes with three different values, and we'll apply those classes to the same element (in this case, a table cell):
CSS:
CODE
.bg_red {
background-color: #FF0000;
}
.bg_green {
background-color: #00FF00;
}
.bg_blue {
background-color: #0000FF;
}


HMTL:
CODE
<table width="600px" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="150px" class="bg_red"><strong>Red</strong></td>
<td width="150px" class="bg_green"><strong>Green</strong></td>
<td width="150px" class="bg_blue"><strong>Blue</strong></td>
<td width="150px" class="bg_red bg_green bg_blue"><strong>All classes</strong></td>
</tr>
</table>


This will produce:


Or, if you want to see the whole HTML, download it here.
Go to the top of the page
 
+Quote Post
jlhaslip
post Jul 10 2008, 04:11 AM
Post #3


A computer once beat me at chess, but it was no match for me at kick boxing.
Group Icon

Group: [MODERATOR]
Posts: 3,882
Joined: 24-July 05
From: In Trouble Again... still?
Member No.: 9,787
Spam Patrol



Also worth noting, not all Browsers recognize mu;tiple classes. I believe the earlier IE fails to deals with them according to the Standards.
Go to the top of the page
 
+Quote Post

Reply to this topicStart new topic

Collapse

> Similar Topics

Topics Topics
  1. Read This Before Applying For Hosting!(55)
  2. Applying For A Job...(14)
  3. English Class Speech(7)
  4. What Are Parametric Equations?(2)
  5. My Life After High School(2)
  6. A Girl In My English Class Is Simply Amazing...(10)
  7. Php Education Class (first Code)(0)
  8. What Is Your Idea About My Php Class?(4)
  9. Need Help With My Speech Class(2)
  10. Eclipse Exporting .jar Files(4)
  11. Php Template Class I Started(1)
  12. Final Spec's Of My Computer Build For My Hardware Class(11)
  13. Alright, I'm Taking Computer Programming As A Class In School.(6)
  14. Do You Ever Skip Class?(58)
  15. Ib(0)
  1. Data Structures -- Linked List(7)
  2. Help: Disable All Buttons Inside A Div Element(8)
  3. The Story Of The Student Who Failed Writing Class(1)
  4. Php Email Validation(1)
  5. Moisturiser Before Applying Makeup Or Even For Everyday Wear(4)
  6. Count Number Of Div's By A Class Name(3)
  7. P.e. Class(2)
  8. Marijuana(3)
  9. History Class Project- Prohibition(0)
  10. Law And You Class - Competion(0)
  11. Class Vs. Id?(4)
  12. Much Fun In Math Class(8)
  13. My Spy Suggestions For Team Fortress 2 Next Update S(2)


 



- Lo-Fi Version Time is now: 25th July 2008 - 08:07 PM