3 Replies - 1751 Views - Last Post: 08 February 2011 - 01:20 AM

#1 cupidvogel   User is offline

  • D.I.C Addict

Reputation: 31
  • View blog
  • Posts: 593
  • Joined: 25-November 10

Class vs ID

Posted 07 February 2011 - 08:14 AM

First, Let me provide three codes- HTML, CSS and jQuery:

HTML:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title>A Christmas Carol</title>
    
    <link rel="stylesheet" href="send.css" type="text/css" />
    <script src="jquery.js" type="text/javascript"></script>
    <script src="send.js" type="text/javascript"></script>
  </head>
  <body>
      <div id="switcher">
        <h3>Style Switcher</h3>
        <div class="button" id="switcher-default">
          Default
        </div>
        <div class="button" id="switcher-narrow">
          Narrow Column
        </div>
        <div class="button" id="switcher-large">
          Large Print
        </div>
</div>
</body>
</html>



CSS:

#switcher {
  float: right;
  background-color: #ddd;
  border: 1px solid #000;
  margin: 10px;
  padding: 10px;
  font-size: .9em;
}
#switcher h3 {
  margin: 0;
}
#switcher .button {
  width: 100px;
  float: left;
  text-align: center;
  margin: 10px;
  padding: 10px;
  background-color: #fff;
  border-top: 3px solid #888;
  border-left: 3px solid #888;
  border-bottom: 3px solid #444;
  border-right: 3px solid #444;
}
 .hov {
  cursor: pointer;
  background-color: red;
}



jQuery:

$(function() {
$(".button").hover(function() { $(this).addClass("hov"); }, function() { $(this).removeClass("hov"); });
});



I have a div with id switcher in my HTML page. Inside, there are 3 buttons with class button attached to them. I have attached an external stylesheet where I have defined the style .hov {background: red; cursor:pointer; }. The code is supposed to change the cursor to pointer and give the button a red background when mouse hovers over it. However, only the cursor is changing to pointer, background color is remaining unchanged. Also when I modify the CSS selector from .hov to #switcher .hov , the code works as expected, i.e. both pointer changes to cursor and background turns red when hovered. Can you tell me why is this happening? There are no other elements in the page (like say, image) with class button, so .hov is supposed to work just as fine as #switcher .hov. The selector should work fine without the additional specification, but it doesn't. Can anyone tell me why?

Is This A Good Question/Topic? 1
  • +

Replies To: Class vs ID

#2 Martyr2   User is offline

  • Programming Theoretician
  • member icon

Reputation: 5612
  • View blog
  • Posts: 14,686
  • Joined: 18-April 07

Re: Class vs ID

Posted 07 February 2011 - 02:59 PM

Well as you already mentioned, when you leave it .hov it applies the style for the pointer, but not the colored background. This right here tells you that the style itself is being applied properly. The only conclusion left is that the background-color is being overriden. This is exactly what is happening. CSS works on specificity, meaning the style definition which is more specific styles the element.

In CSS, an ID is more specific than a class. This is because in a page only one element can have the ID #switcher. Since you have defined a color for #switcher already, the .hov class definition is not strong (specific) enough to overwrite the background-color from #switcher.

When you change it to #switcher .hov, you are even MORE specific than #switcher. You are saying I want .hov elements specified INSIDE of #switcher. Thus it is more specific and its color definition overrides that of #switcher's and you see the red.

The reason the cursor is working is because #switcher doesn't define a cursor, so there it is just adding on the style, no conflicts with that of #switcher. Define a cursor in #switcher and you will see it no longer works in .hov.

It is all about which elements are more specific here.

I hope I have answered your question. :)
Was This Post Helpful? 3
  • +
  • -

#3 cupidvogel   User is offline

  • D.I.C Addict

Reputation: 31
  • View blog
  • Posts: 593
  • Joined: 25-November 10

Re: Class vs ID

Posted 07 February 2011 - 03:11 PM

Jesus Christ! That's one heck of an answer! Concept totally cleared! Should have noted that the #switcher div has a background-property set already. Thank you so much! I wish I could give you more than 1 reputation!
Was This Post Helpful? 1
  • +
  • -

#4 cupidvogel   User is offline

  • D.I.C Addict

Reputation: 31
  • View blog
  • Posts: 593
  • Joined: 25-November 10

Re: Class vs ID

Posted 08 February 2011 - 01:20 AM

@Martyr2, I just noticed another thing, in the #switcher .button selector for designing the buttons, if I use .button selector instead, it works fine all the same. However, it shouldn't, for the #switcher div has already a background-color assigned to it (#ddd), and because in .button I am assigning a color to the buttons as well (#fff), these two would collide bang-on, and since #switcher has a higher selectivity than .button, it should win, leaving the background-color of the buttons same as the #switcher. However, it isn't happening, and replacing #switcher .button by .button is causing no change at all. Can you explain this?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1