Social Icons

Pages

Monday, 27 August 2012

Use Multiple CSS Classes on a Single Element

One of the lesser known tricks with CSS is the fact that you don't have to limit your elements to just one class. If you need to set multiple classes on an element, you add them simply by separating them with a space in your attribute. For example: let's assume we have two custom classes in the style sheet named maintext and center respectively:

.maintext {
   font-family:   "Courier New", Courier, mono;
   font-size:     14px;
   color:         #FFCC99
}
.center {
   text-align:    center;
}
 
Then what we could do is apply both classes to the same element with the following syntax:

<div id="Layer2" class="maintext center">

Standards based browsers allow you to apply two or more classes to an element! 

2 comments:

  1. So if we specify the font-size in both the classes which one will it pick up overriding the other? For example if we write

    .maintext {
    font-family: ""Courier New", Courier, mono;
    font-size:12px;
    }

    .center {
    text align: center;
    font-size:9px;
    }

    which font size would it consider out of the two classes and which one would it override?

    ReplyDelete
    Replies
    1. Font Size of first class will get override by second class.
      For eg:
      if you write class="maintext center" than final font size will be 9px.
      if you write class="center maintext" than final font size will be 12px.

      I hope i have solved your question.
      Let me know if you have any other concerns.

      Thanks.

      Delete

 

Contact Us

info@intellozene.com

Our Website:
www.intellozene.com