Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2009-05-13
8
Medium Priority
?
580 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 1000 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

CTAs encourage people to do something specific to show interest in your company, product or service. Keep reading to learn why CTAs should always be thought of as extremely important, albeit small, sections of websites.
Do online retailers and e-commerce sites even need to bother with the season? Is it lucrative to update and prepare your site for Black Friday and Cyber Monday shoppers? We'll find out now.
In this tutorial viewers will learn how to embed custom externally-hosted Google Fonts using the Google Font API in CSS Go to the Google Fonts website at google.com/fonts: Browse or search based on font properties or name to find a suitable font for…
The is a quite short video tutorial. In this video, I'm going to show you how to create self-host WordPress blog with free hosting service.
Suggested Courses

824 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