Solved

What CSS to use for nested ID Selectors on two different pages?

Posted on 2009-05-13
8
556 Views
Last Modified: 2012-05-06
I'm working with the Able Commerce shopping cart. Two different pages both contain the same product list section as shown below. I want two different styles for the ProductList divs on each page. For example, I want #HomePage + #ProductList to be a yellow background while #CategoryPage + #ProductList to be a red background.

I tried using #HomePage + #ProductList and #HomePage > #ProductList styles but that didn't work. Is this possible?

<!-- Home Page -->

<div id="HomePage">

  <div id="ProductList">

  </div>

</div>
 

<!-- Category Page -->

<div id="CategoryPage">

  <div id="ProductList">

  </div>

</div>

Open in new window

0
Comment
Question by:ZekeLA
  • 4
  • 3
8 Comments
 
LVL 12

Accepted Solution

by:
alien109 earned 250 total points
ID: 24379949
To select them independently of each other you should be able to use the following selectors

#HomePage #ProductList {
    <!-- style attributes -->
}

#CategoryPage #ProductList {
    <!-- style attributes -->
}
0
 
LVL 20

Expert Comment

by:ddayx10
ID: 24379982
If I understand what you want the CSS would be:

#HomePage #ProductList{background:#CCCCCC;}
#CategoryPage  #ProductList{background:#FFFCCC;}

Just an example.

dday
0
 
LVL 1

Author Comment

by:ZekeLA
ID: 24380010
Are you sure or have a working example to demonstrate? I tried that and it doesn't work. The joint id selectors don't apply.
0
 
LVL 12

Expert Comment

by:alien109
ID: 24380013
ZekeLA,

You may find this useful in the future on selectors and browser support
http://www.quirksmode.org/css/contents.html
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 1

Author Comment

by:ZekeLA
ID: 24380032
I should clarify my html. The nesting is actually several levels. For example, I really have:

<div id="HomePage">
 <div id="abc">
   <div id="def"
      etc.
     <div id="ProductList"
0
 
LVL 12

Expert Comment

by:alien109
ID: 24380050
As long as ProductList is an ancestor of HomePage and CategoryPage, it should work.

Try the attached code. Works fine for me in FF3, IE7, and IE6. FYI - Never use duplicate ID's, I only did for the purpose of the example. :)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.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" />

<meta http-equiv="Content-Language" content="en-us" />
 

<style type="text/css">
 

#HomePage #ProductList {

    background-color:#f00;

}
 

#CategoryPage #ProductList {

    background-color:#00f;

}
 

</style>
 

</head>

<body>
 

<!-- Home Page -->

<div id="HomePage">

	<div id="ProductList">CONTENT</div>

</div>
 

<!-- Category Page -->

<div id="CategoryPage">

	<div id="ProductList">CONTENT</div>

</div>
 
 

</body>
 

</html>

Open in new window

0
 
LVL 1

Author Comment

by:ZekeLA
ID: 24380136
I was mistaken about the element ids so my problem is actually with the underlying code, not the css. I tested your solutions and it definitely works. My bad. By the way, my issue is with two different pages, not the same page so I wouldn't be using duplicate ids. Thanks.
0
 
LVL 12

Expert Comment

by:alien109
ID: 24380251
Glad you got things sorted. And I assumed you didn't have duplicate IDs, just wanted to clarify that I was only doing it for purposes of demonstration.

Cheers.
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

SSL stands for “Secure Sockets Layer” and an SSL certificate is a critical component to keeping your website safe, secured, and compliant. Any ecommerce website must have an SSL certificate to ensure the safe handling of sensitive information like…
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
In this tutorial viewers will learn how to style a decorative dropcap for the first letter in a paragraph using CSS. In CSS, create a new paragraph class by typing "p.fancy": Then, to style only the first letter of the first sentence, include the ps…
The viewer will get a basic understanding of what section 508 compliance can entail, learn about skip navigation links, alt text, transcripts, and font size controls.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now