tags:

views:

40

answers:

3

Hello,

I have 2 nested divs, both have

#x{
width:400px;
height:400px;
background-color:#fff;
color:#000
}

#y{
width:200px;
height:200px;
background-color:#000;
color:#ccc;
}


<div id="y"><div id="x">Here lies a x value</div></div>

I want the #x and #y to have individual css properties, but that is not the case, #x overrides the #y values

Any help appreciated.

Thanks Jean

A: 
#y{
width:400px;<-- add px.
height:400px;
background-color:#fff;
color:#000
}

#x{ <--changed to X, it was y
width:200px;
height:200px;
background-color:#000;
color:#ccc;
}

Also, when you id a div to x it will take the properties you detup in #x.

Kyle Sevenoaks
I know it will take the properties of #x, but I want the divs to have individual properties, is that possible?
Jean
Yes, just set it up in the CSS, that's what it's for. You have it right, if you want to have more different styles, eg `border` then tell `#x` or `#y` to have a border and it will, but the other won't. You need to tell nested divs each and every different thing you want, or as far as I know it will inherit from its parent on most properties.
Kyle Sevenoaks
A: 

Specificity! As your two declarations have the same specificity, the one that comes last in the CSS is the one that is honoured. To increase the specificity of the inner target, try:

#y #x{
width:400;
height:400px;
background-color:#fff;
color:#000
}

#y{
width:200;
height:200px;
background-color:#000;
color:#ccc;
}

Here's a cracking article on the subject.

graphicdivine
I doubt that specifity has to do *anything* with it. The declaration for one div with ID `x` is *never* applied to another div with ID `y` (not counting the case of inheritance, as is the case with the `color` property, but the question IMHO is targeted to the dimensions)
Boldewyn
@Boldewyn you are so right. I'll get my coat.
graphicdivine
@graphicdivin its not targeted to border or dimensions, but color also. Assume I have opacity in #y and my div setting is as <div id=y><div id=x></div></div>. But I dont want a opacity change in #x
Jean
@Jean: In that case, follow either graphicdivine's approach or change the order of the CSS declarations. Latter ones overwrite earlier ones with the same specifity.
Boldewyn
The -1 is new, isn't it? I wonder why, since the answer at least explains, why the `color` is different.
Boldewyn
A: 

They do have individual properties. It is just that the nested x div is in front of the y div, so the y div is obscured. Try adding overflow: hidden; to #y and you will see that it constrains #x to it's footprint.

kingjeffrey
@kingjeffrey not working
Jean
The white div background blends in with the white page background. Try applying a border to #x to see its bounds. Or change the page background to yellow. You could also apply a top and left margin to #x to see some of #y exposed. Here is a sample: http://test.kingdesk.com/nested-widths.html As you will see, #y maintains it's styling (behind #x)
kingjeffrey