0

In my website, I have the following structure on some html page:

<html>
     <head><!-- All the usual information here, including the link to the css file --></head>
     <body>
           <div id="splash">
                 <!-- The good stuff -->
           </div><!--End of Splash-->
     </body>
</html>

Now that #splash div only appears on that one html page, and I need the css affecting that page's html {} to be a little different. Is the below notation going to do what I need?

html>body#splash {/* CSS that only affects the html that contains div #splash */}
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
gersande
  • 465
  • 1
  • 8
  • 25
  • possible duplicate of [Is there a css selector for selecting an element futherup in the html?](http://stackoverflow.com/questions/16280632/is-there-a-css-selector-for-selecting-an-element-futherup-in-the-html) – Quentin Aug 26 '13 at 22:25

5 Answers5

3

I think you're looking for a parent selector; sadly this doesn't exist in CSS, so you're out of luck.

This is, from what I understand, mostly for performance reasons -- Jonathan Snook's article goes into a little more detail.

It's either time to change your page generation so that a class or ID gets added to the html element on your "splash" page, or resort to JavaScript, such as the jQuery cssParentSelector library that arkanciscan mentions.

Community
  • 1
  • 1
Matt Gibson
  • 37,886
  • 9
  • 99
  • 128
  • Thank you, indeed you are right the word I was looking for was 'parent' - I had not used child selectors before and I thought they meant something else. – gersande Aug 26 '13 at 22:29
2

Since IDs are unique, you can just do:

#splash {
    /* stuff */
}

About your question:

html>body#splash

Will not work; try

html>body #splash

The > is basically the same this as a space, but selects only direct children.

(I'm assuming you're trying to select #splash; that's how I read your question. If you want to select body if #splash exists, then... well, you can't.)

tckmn
  • 57,719
  • 27
  • 114
  • 156
2

Like others have said, you can't do this is CSS... yet! In the next version of CSS you'll be able to add an exclamation point to any part of a compound selector to indicate which element of the selector your rules should apply to. This will be called a "subject" (http://www.w3.org/TR/selectors4/#subject)

If you want to use this today you can try this polyfill https://github.com/Idered/cssParentSelector

Jesse Hattabaugh
  • 7,876
  • 8
  • 34
  • 38
1

Child selectors are indicated by >: http://www.w3.org/TR/CSS2/selector.html#child-selectors

It means you are selecting a first-level descendent.

Your code needs this space:

  html>body #splash

This reads: "Select the element with the id of splash that is a descendent of body which is a child of html."

Your code is this:

 html>body#splash

Your current code reads: "Select the body element with the id of splash that is a child of html.

Mister Epic
  • 16,295
  • 13
  • 76
  • 147
1

As has been said, this can't be done in current CSS.

But just to give you a suggestion: you could add a unique ID or a class to the HTML in question; if you know which one it is ahead of time, just add it statically; if not, you'll have to actually look (with JS) if there's a div like you describe in the body, you can't do it with CSS alone.

Marconius
  • 683
  • 1
  • 5
  • 13