Results 1 to 6 of 6

Thread: Class and ID in one CSS statement?

  1. #1
    Join Date
    Oct 2007
    Posts
    53
    Thanks
    13
    Thanked 1 Time in 1 Post

    Default Class and ID in one CSS statement?

    Can one specify styles to apply to an ID, but only if that ID is also a given class?

    Ex:

    .class & #id {
    myproperty:value;
    myproperty:value;
    }

    thanks for any help!
    Last edited by AmenKa; 11-15-2008 at 08:22 PM.

  2. #2
    Join Date
    Feb 2008
    Location
    Cebu City Philippines
    Posts
    1,160
    Thanks
    17
    Thanked 277 Times in 275 Posts

    Default

    ID should be unique in every page. So, why not just use that ID instead? It's confusing. Could you show your markup.

    Anyway:
    HTML Code:
    <style type="text/css">
    .test#content{
    border:1px solid red;
    }
    </style>
    <div id="content" class="test">
    Dummy
    </div>
    Learn how to code at 02geek

    The more you learn, the more you'll realize there's much more to learn
    Ray.ph!

  3. #3
    Join Date
    Jul 2006
    Location
    just north of Boston, MA
    Posts
    1,806
    Thanks
    13
    Thanked 72 Times in 72 Posts

    Default

    HTML Code:
    <div class="testClass">
       <p id="testId">something</p>
    </div>
    Code:
    .testClass > #testId {
         property:value;
    }
    what will force the styles to only apply to the #id if, this id is contained within some class.

    ID should be unique in every page. So, why not just use that ID instead? It's confusing. Could you show your markup.
    You are correct about one id per unique page, however, consider this. If I have two pages, both of which I have uniquely identified tag id. They both contain the same data, however in one of the instances, you wish to adjust the style to appropriately fit that page. This would be a circumstance, where you do not want to adjust every single id, but for this particular instance you want to have a "one-off" and you do not deem it fit to create a new unique id.

    Now on that note, I am going to assume that this is probably not the case in this situation.

  4. #4
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Quote Originally Posted by AmenKa View Post
    Can one specify styles to apply to an ID, but only if that ID is also a given class?

    Ex:

    .class & #id {
    myproperty:value;
    myproperty:value;
    }

    thanks for any help!
    To be certain of helping with this we would need to see an example of the markup you wish to apply this to, one example where the rules should apply, and one example where they should not.

    However, it would generally be (as hinted at already) better to have style applied only to an id. Or if you really want this kind of dual styling and want to use it in more than one spot on a page, to use two classes rather than a class and an id.

    Also @boogyman:

    BTW this type of syntax:

    Code:
    .testClass > #testId {
         property:value;
    }
    though valid, may not work in some browsers. For the markup that you suggested be used with it, this would work just as well and would not be a problem in any browser that has css support:

    Code:
    .testClass #testId {
         property:value;
    }
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  5. #5
    Join Date
    Oct 2007
    Posts
    53
    Thanks
    13
    Thanked 1 Time in 1 Post

    Default

    I doubt that I will use this at all, I was mainly curious...

    If I do decide to use it, it would probably be because an element's ID is gathered from a table (So two IDs can be the same if you are assigning IDs from more than one table), and I would like there to make it unique compared to another element from the same table even though they share the class...

    More likely if I were to use it, it would be to allow someone else to do that with my code... I like things to be consistent.

    <style>
    .class #id {
    margin:10px;
    }
    </style>
    <div class="class">
    <a href="target" id="id">Link</a>
    </div>

    Would that apply the margin to the link? Or would it need to be...

    <style>
    .class #id {
    margin:10px;
    }
    </style>
    <div>
    <a href="target" class="class" id="id">Link</a>
    </div>

    The way I read it, I think both ways would work?

  6. #6
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    The first one would work. The second would not, unless you did it like so with no space:

    Code:
    <style type="text/css">
    .class#id {
    margin:10px; 
    }
    </style>
    <div>
    <a href="target" class="class" id="id">Link</a>
    </div>
    The space between selectors indicates it applies to the second item when contained by the first. With no space it means that they are both the same item.

    Note: I added the required type attribute out of habit.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  7. The Following User Says Thank You to jscheuer1 For This Useful Post:

    AmenKa (11-15-2008)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •