tags:

views:

281

answers:

6

my HTML page contains a div with an id called "main", and i am including another HTML page in to my HTML page, which contains a div with the same id("main"). i need to append a name space to the id("main") of my HTML page.

I am including header,footer from another site and including content from my site to that HTML page. If that other sites uses the same id as my site is using, here the conflict is occurring.

In this HTML page two css files are including. If both of them containing same class with a different style here there is a problem.

I need to apply my css file to my code and another one to remaining code. can any body solve my problem?

A: 

Use the name of the second div in the ID of the second div. E.g if the second page is called signup.html , use main_signup

Click Upvote
+2  A: 

Is there anything about the containing elements of the divs that will set them apart from each other. If there is you can prefix your css declaration with the containing element and specify a unique style for both divs.

Eg:

#main
{Style}
.container #main
{Another style}

As people have mentioned, if you have control over the HTML it would be wise to specify a different class for both elements or a unique ID for each of them.

Sam152
you beat me to it. The auto update feature on answers is nice.
NTulip
It's not correct to have the same ids in HTML doc, is it?
Sergey
Technically not, but it seems that john has limited control of what the elements are called.
Sam152
+3  A: 

Two things:

  • It sounds like you need a class, not an ID.
  • Use embedded stylesheets if you are looking to apply per-page differences between styles.

Using the same ID in this case would definitely be a case of living with broken windows. If you have any control over the situation, you should update the HTML instead of inviting even more confusion later.

altCognito
You don't need embedded stylesheets to get per-page differences! You just put an ID on the body tag.
AmbroseChapel
Not my preference as it can clutter css files. We had this discussion at work just the other day. It may break down to just that: preference. Here's a bunch of people arguing abou it: http://www.webmasterworld.com/css/3115364.htm I see both sides, it is nice to have all of your rules in one place.
altCognito
+2  A: 

A page with a duplicate id is not valid. Even if you manage to target the separate elements by combining the id with classes, it's not guaranteed to work with all browsers.

To make a valid page the elements need to have a unique id.

Guffa
A: 

In short, there's no such thing as namespaces in CSS. Use one of the work-arounds the other answers propose :)

cwap
A: 

I agree with everyone so far, in that there is no native CSS namespace capability and that if you have control (or the desire/capacity) to edit your HTML then that's the best approach.

One way to automate the process would be to write a script (PERL, PHP, .NET, etc.) to parse the HTML you're pulling in and replace any existing ID's or classes with a modified version.

i.e. <tag id="theID" class="theClass anotherClass"> becomes <tag id="NAMESPACEtheID" class="NAMESPACEtheClass NAMESPACEanotherClass">

You could accomplish this with some creative regular expressions, although it would get trickier if there's Javascript code that would need to be modified as well.

As far as performance goes, this would be a big hit depending on the size of the file and you'd probably want to cache the result, or rewrite the original file.

Let me know if you decide to go that route, the idea actually sounds a bit intriguing and could have a place in my toolbox!

PHPexperts.ca