Css Multiclassing - Applying more than 1 class to an element

free web hosting
Free Web Hosting, No Ads > CONTRIBUTE > Computers > Programming Languages > CSS (Cascading Style Sheets)

Css Multiclassing - Applying more than 1 class to an element

sonesay
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.

 

 

 


Reply

who?
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.

 

 

 


Reply

jlhaslip
Also worth noting, not all Browsers recognize mu;tiple classes. I believe the earlier IE fails to deals with them according to the Standards.

Reply



Got an Opinion! Express your Views! (no registration):-
Add your Reply/ Opinion/ Views/ Comments/ Suggestion/ Questions/ Queries etc.
Posts with decent grammar & English will be accepted and please refrain from profanities.
For asking a Question, We recommend you to sign-up (for free) so that you can track the topic easily.

Nature of your Post*: Opinion/ Reply/ Comments
Question/Query
Feedback to us.
       
Name   Email
Title/Question*

(Maximum characters: 10,000)
You have characters left.
Confirm Code:

Similar Topics

Keywords : css, multiclassing, applying, 1, class, element

  1. Constant Interface Or Constant Class?
    (1)
  2. My Spy Suggestions For Team Fortress 2 Next Update S
    Spy is my favorite class and I think these suggestions own (5)
    I thought about this and I like it. You may disagree or agree or may like 1 or 2, but still read it
    and tell me what you think. Spy --- Sapper Replacement Electronic Canceler: -------------------
    At a limited distance away, you must look at the sentry, teleporter, or dispenser and you can use
    your electronic canceler. It will freeze the sentry, dispenser, or teleporter for an X amount of
    seconds (I suggest 10) and once it is over, the engineer's building unfreezes. Engineer can
    still repair Now valve can do whatever they want with this but, I like this: You c....
  3. Much Fun In Math Class
    (8)
    You're asking what? Well, let me explain. Today our teacher handed our recent Math Exams back
    to us, so we could go over our mistakes, etc. Well my friend (sitting two desks behind me) was very
    disappointed to receive a very low mark on his exam. Anyways, he decided to rip his entire exam into
    three pieces. After he was done, my other friend (sitting one desk behind me) decided to break the
    terrible news to him: 'You know, our teacher takes the tests in again after this...' My
    friend that ripped up his test, just started laughing at himself. Then he started....
  4. Class Vs. Id?
    (7)
    CODE #random_div { random: random; random: random; } .random_div { random: random;
    random: random; } Ok... well.. above we have a id (the first one) and then the second one is
    a class. Could anyone please explain to me what the difference between the two is? And is one any
    better?....
  5. Law And You Class - Competion
    (0)
    Okay in my law and you class we had to make up funny stories that explain terms such as battery and
    assault. Here are some of my stories. Battery Definition: Unlawful, unprivileged touching of
    another person. Situation: After school on Monday, Mike found Johnny started to beat him up because
    of an argument they had about basketball earlier that day. At the end of the fight, Johnny was
    bruised with a broken nose and two black eyes. Trespass Definition: Wrongful injury or
    interference with the property of another. Situation: One day, LaTwonda and Rasheeda, wanted t....
  6. History Class Project- Prohibition
    Prohibition (0)
    Today in history class we talked about the Roaring 20's and about prohibition. We also watched
    the Simpson's episode about prohibition. Before I start, here is the actual 18th amendment about
    prohibition. 1. After one year from the ratification of this article the manufacture, sale, or
    transportation of intoxicating liquors within, the importation thereof into, or the exportation
    thereof from the United States and all territory subject to the jurisdiction thereof for beverage
    purposes is hereby prohibited. 2. The Congress and the several States shall have con....
  7. Marijuana
    A paper I wrote for english class (3)
    I wrote an essay similar to this for my English class, I was wondering what some people thought
    about it or maybe some nice people can point out some typos and etc since i have no one to proof
    read to me. QUOTE Since the 1930s the DEA has been monitoring and illegalizing the use of
    drugs. Out of these “drugs” many of them have beneficial purposes in various medical
    fields while others were used for religious purposes as well. The Drug Enforcement Agency of the
    United States has been strict and manipulative in order to keep these substances illegal. One o....
  8. P.e. Class
    (2)
    I take gym like most students in High School and they make you wear uniforms and the whole picture
    and what not. Many kids today thogh, including myself, struggle with their weight and how to stay
    fit and how to keep up with other students. My life really snapped into perspective when I
    couldn't run the mile in under 10 minutes. When I was in Middle School I could, but when I
    reached the Hihg School level I was shocked that I had lost control that much of my weight. Today, I
    run 3 Miles with my dog before school and do 100 Sit-Ups before bed and I can run the mile in....
  9. Count Number Of Div's By A Class Name
    (3)
    I have a situation where I need to be able to count the number of div elements in a page and then
    use that number for a loop. I've already tried this but it returns all child div elements.
    ocument.getElementById("char_list").getElementsByTagName("div").length; I need only the child nodes
    directly below the parent div 'char_list'. THe structure is like this The
    problem here is I also have nested div's inside the 'char_node' divs so the first code
    returns them all. I'm trying to just gather the char_node class divs and use i....
  10. Moisturiser Before Applying Makeup Or Even For Everyday Wear
    anyone know a good moisturiser? (4)
    Hey all Well ive used alot of moisturisers like OLAY .. even some from overseas.. anyone ever used
    one that is really good and stops skin from being dry when you apply foundation? Cause you know how
    sometimes you can see dry bits on ur face because the foundation is quite dry or the powder makes ur
    skin look dry? i know there are moisturisers or rinsing lotion for you to use before applying makeup
    any suggestions? or even everyday moisturiser to make ur face less dry.. and not so dehydrated ..
    let me know ....
  11. Php Email Validation
    A PHP data validation class with many functions (1)
    I've been reading through my old php book (PHP 4.1) and came across this data validation class.
    It can check a number of things ranging from telephone numbers , credit card number formats, email
    address and some others. I checked out some of the methods although I didnt expect it to work 100%
    because I've found source code errors thoughout the book and CD. I tested out a few of the
    methods to check and some of them did return expected results but some didnt either so the data
    validation class was not perfect and it didnt really bother me. The cool thing I found....
  12. The Story Of The Student Who Failed Writing Class
    (1)
    One of the students failed in writing class, it is unusual for a student to fail in an easy class
    like writing. When the teacher was asked about the raison he failed in the class he replied:
    honestly felows, the student cannot focus, every time he is given a topic to write about, he goes
    off topic. They then asked him to give them example of topics he wrote about. So the teacher said:
    Here is an example, write about the spring season… Spring is the most beautiful season in the
    year, during this season there are a lot of green pastures which allows the camel to feed....
  13. Help: Disable All Buttons Inside A Div Element
    How do you write a function to disable all buttons given a div id (8)
    I need help to write a function to disable all buttons iside a div ID. if possible the function will
    disable all buttons even the ones inside child divs belonging to the main div.....
  14. Data Structures -- Linked List
    Find the nth last element in linked list. (8)
    Given a linked list, find the 5th last element with Time complexity O(n) and minimal space
    complexity. Note: If you know the answer and if you feel it is simple also please post the
    answers so that others will come to know about the answers. What is a linked list?? /* this is for
    your further reference and reading */ QUOTE In computer science, a linked list is one of the
    fundamental data structures, and can be used to implement other data structures. It consists of a
    sequence of nodes, each containing arbitrary data fields and one or two references ("links") po....
  15. Ib
    I'm applying to it (0)
    Well, I'm starting high school soon. I'm going to be applying for IB and I here that its
    super super hard. But I still want to take it for a good scholarship. Does anyone have any
    recommendations for getting good grades by not doing so much work (from those who have taken this
    program please).....
  16. Do You Ever Skip Class?
    (63)
    well i used to skip almost every class especially algebra but not anymore because i realize that my
    education is a major part of my life and i refuse to fail but my english class is boring as f**k..
    have you ever skipped class? /huh.gif" style="vertical-align:middle" emoid=":huh:" border="0"
    alt="huh.gif" />....
  17. Alright, I'm Taking Computer Programming As A Class In School.
    (10)
    Is there anything I should personally be worried about or anything I really need to know? I'm
    pretty much good with computers and such and pretty much spend my whole life on them, lol.....
  18. Final Spec's Of My Computer Build For My Hardware Class
    (11)
    Well I got two weeks left in the semester and on August 18th I will have built my first computer
    from scratch, now hopefully during the rest of the year I will have build up a series of topics on
    computer hardware and tutorials on building a computer from scratch that most of you will never have
    to buy a pre built system again /biggrin.gif" style="vertical-align:middle" emoid=":D" border="0"
    alt="biggrin.gif" />. With that being said this is the final specs of the computer I will be
    building, after I had a fluke with my floppy drive (had the wrong one). Keyboard &....
  19. Php Template Class I Started
    and a good way to see how to fail template class (1)
    Althought it works, and maybe some users can even use this class for some simpler websites, this is
    not a good way to create class. I cant really explain why, and i'm publishing it so maybe people
    with more experience in template design can help me figure out how to create PHP templates. Looking
    forward for your answers. some of the variables and directories are in croatian, but i don't
    think that matters when you look at with programmers eyes. First my index.php file looks like
    this... CODE <?php     include ("includes/showSiteClass.php"&#....
  20. Eclipse Exporting .jar Files
    Could not find main class. Program will exit. (5)
    I need help! I've been using eclipse for a while making little applets, and now I've
    decided to look at some tutorials on how to make simple applications. I've found a pretty good
    tutorial that tells you how to make a helloWorld app with swt and jFace, and also has an example
    file explorer that you can make. They both work perfectly from inside eclipse, but when I try and
    export either one into a .jar file, they don't work anymore. It comes up with a message saying
    "Could not find main class. Program will exit." In the little wizard it has for....
  21. Need Help With My Speech Class
    (2)
    Tell me If it makes sence please i need to know for my speech class report... Sample Outline
    Persuasive Policy Speech Claim: There is a need to put into affect a policy making phone use while
    driving illegal. Preview INTRODUCTION I. Story of 4 year old Hannah. A little girl who
    liked playing in her front yard, and making mud pies. Hannah, who was always a happy child, was
    killed when a driver, made a wild turn onto the sidewalk and hit her, killing her instantly. The
    driver was not drunk, rather on a cell phone. II. There is a need to put into affect a p....
  22. What Is Your Idea About My Php Class?
    (4)
    I posted 5 code of PHP. now i should know that "ARE THEY USEFUL?" and i want to know that "WHAT I
    SHOULD DO TO HAVE A BETTER CLASSROOM?"....
  23. Php Education Class (first Code)
    (0)
    Hi I want to educate some PHP codes that i think they will be useful for all of you! My 1st
    code is this: CODE <?php class calculator {          /**      * Variable for holding all
    the numbers to add      *      * @var array      */     private $numbers = array();
             /**      * Variable holding all the digits after the point      *      * @var array      */
        private $afterPoint = array();          /**      * Maximum number of digits after
    the point      * that a number has      *      * @var int      */     private $....
  24. A Girl In My English Class Is Simply Amazing...
    (10)
    well, this girl in my english class, shes amazing. shes an athelete, shes beautiful, and just a
    great person. and all of those qualities make me want her badly. of course there are the girls that
    are just plain hott, but shes different, shes got everything. and i like that. but im not so sure
    she likes me, i mean, we talk and all. but i dont get to see her much, and i want to hang out with
    her in the summer, but im going to be gone and working a lot, so that sucks badly. so i kind of want
    to at least secure a friendship and maybe have her as my gf. and from what i hear, ....
  25. My Life After High School
    an essay for english class (2)
    Melody and Musical Centricity Introduction Wouldn’t it be nice if everyone was
    musically talented? Well, for those who are, they have the potential to make it big, to make a
    living off of their skills, to become nationally known. The likelihood is very slim and very few
    people make it to stardom. The window is small and it takes every ounce of passion to make it, and
    I think that I have that passion. I will not leave it to chance, however, for I will still go to
    college and have a part-time job to support my band. I would like to live in a nice, sunny p....
  26. What Are Parametric Equations?
    Some actually USEFUL I learned in math class (2)
    If you remember stuff from your algreba class, you should recall that an equation that is used on a
    graph is usually in the form of y = x, or x = y, or something that includes both x and y.
    Parametric equations invovle a third variable, T, which is generally used to represent time. It
    takes two equations to graph a parametric equation, x = T and y = T. The x equation defines the x
    coordinate of the point you are plotting, and the y equation defines the y coordinate of the point.
    The variable T is often referred to as time because parametric equations make it very ea....
  27. English Class Speech
    what do you think? (7)
    ok the topic is pro capital punishment: “Have you ever thought about how many criminals escape
    punishment, and yet, the victims never have a chance to do that? Are crime victims in the United
    States today the forgotten people of our time? Do they receive full measure of justice?.” These
    are just a few of the questions posed by the late director of the FBI J. Edgar Hoover. Criminals
    on death row have the chance to make a will and prepare for their death, while some victims can
    never do it. My opponents may argue that the death penalty has no deterrent effect, but....
  28. Advice On Impressing Someone In My Class
    in post (8)
    Ya well basically i need help with impressing/asking out someone, i have plety of oppurtunities in
    my 4th hour class she sits behind me in my 5th hour class she sits to the right of me and in 6th
    hour she sits diagonal to me so i have oppurtunities to talk to her but i dont know what to do,last
    time i had a crush on someone i never built up courage to ask her out or talk to her much and i
    ended up just like hating myself whenever i saw her...not like hitting myself lol jus mentally and i
    ended up just hating it whenever i saw her for 2 years and i finally just let it go ....
  29. Applying For A Job...
    ...with no experience... (14)
    Is it possible to get my application accepted even though I've never had a job before? I'm
    only 15, so dont think I'm a bum. This is my first time looking for a job. I got applications
    from A&P and Dunkin' Donuts, but I have no experience, and I'm reasonably young. Do you
    think there's a chance of my application getting accepted? Also, if it does get accepted what
    happens? I know it goes on file, but would I get interviewed too? I know I seem like a complete
    dumbass, but this is my first time looking for a job, so I just want to know everything ....
  30. Read This Before Applying For Hosting!
    HIGHLY IMPORTANT! (56)
    DISCARD THIS PAGE : WE HAVE CREATED A FORM GENERATOR HERE
    http://www.trap17.com/forums/click-here-de...ting-t9222.html READ THE FOLLOWING INSTRUCTIONS
    CAREFULLY BEFORE APPLYING Once you have the necessary Hosting credits ( check at :
    http://www.trap17.com/forums/ ), You can request here by making a new topic and putting in the
    following details. BEFORE ACCEPTING YOUR APPLICATION, WE GO THROUGH YOUR EACH AND EVERY
    POSTS! SEE TO IT THAT YOU HAVE GOOD QUALITY POSTS. BUILDING A GOOD COMMUNITY IS OUR FIRST
    PRIORITY! Its very easy once you get st....

    1. Looking for css, multiclassing, applying, 1, class, element

*RANDOM STUFF*





*SIMILAR VIDEOS*
Searching Video's for css, multiclassing, applying, 1, class, element

*MORE FROM TRAP17.COM*
Similar
Constant Interface Or Constant Class?
My Spy Suggestions For Team Fortress 2 Next Update S - Spy is my favorite class and I think these suggestions own
Much Fun In Math Class
Class Vs. Id?
Law And You Class - Competion
History Class Project- Prohibition - Prohibition
Marijuana - A paper I wrote for english class
P.e. Class
Count Number Of Div's By A Class Name
Moisturiser Before Applying Makeup Or Even For Everyday Wear - anyone know a good moisturiser?
Php Email Validation - A PHP data validation class with many functions
The Story Of The Student Who Failed Writing Class
Help: Disable All Buttons Inside A Div Element - How do you write a function to disable all buttons given a div id
Data Structures -- Linked List - Find the nth last element in linked list.
Ib - I'm applying to it
Do You Ever Skip Class?
Alright, I'm Taking Computer Programming As A Class In School.
Final Spec's Of My Computer Build For My Hardware Class
Php Template Class I Started - and a good way to see how to fail template class
Eclipse Exporting .jar Files - Could not find main class. Program will exit.
Need Help With My Speech Class
What Is Your Idea About My Php Class?
Php Education Class (first Code)
A Girl In My English Class Is Simply Amazing...
My Life After High School - an essay for english class
What Are Parametric Equations? - Some actually USEFUL I learned in math class
English Class Speech - what do you think?
Advice On Impressing Someone In My Class - in post
Applying For A Job... - ...with no experience...
Read This Before Applying For Hosting! - HIGHLY IMPORTANT!
advertisement



Css Multiclassing - Applying more than 1 class to an element



 

 

 

 

ADD REPLY / Got an Opinion! a humble request :-) RAPID SEARCH! Free Hosting [X]
Express your Opinions, Thoughts or Contribute more info. to help others.
Ask your Doubts & Queries to get answers, So that "Together We can help others!"
Register FREE for AD-FREE forum, Create your own topics, Ask Questions, track topics, setup subscriptions & notifications and Get a Free Website w/ Email and FTP.
500MB Space *No Ads*, CPanel, FTP, PHP, MySQL, EMails - 100% FREE