turning off inheritance in CSS

D

don

I'm trying to insert a table within a table but do not want the nested table
to inherit anything form the first one
how do I turn off inheritance ... so that my table tags will behave normally
 
M

Mark Parnell

Previously in alt.html said:
I'm trying to insert a table within a table
Why?

but do not want the nested table to inherit anything form the first one

Put a class on the first one, so the styles only apply to it.
 
J

Jukka K. Korpela

Mark Parnell said:

Presumably in order to implement an excessively complicated layout idea
in an unnecessarily complicated way. Once in a blue moon, though,
there's a case where a tabular structure contains an element with an
internal tabular structure.
Put a class on the first one, so the styles only apply to it.

No, that won't stop inheritance. Consider a simple case:


<style type="text/css">
table.outer { color: red; background: white; }
</style>
...
<table border="1" class="outer">
<tr><td>foo</td></tr>
<tr><td>
<table border="1">
<tr><td>bar</td></tr>
</table>
</table>

Both "foo" and "bar" will appear in red, since the inner table's cell
inherits the color property from its parent (tr), which inherits the
property from its parent, etc., until we get to the outer table element
that has the property explicitly set. Naturally assuming that no other
style sheet sets the property for the inner cell or any element that is
between it and the outer table in nesting.

And this is a clue to the answer: There is no way to turn off
inheritance in CSS, since it's a defined part of CSS. Some properties
are inherited by definition, when an element hasn't got a property set
otherwise; some properties are not, but this is part of CSS definition
which you cannot change.

What you _can_ do is to set properties to elements so that inheritance
will not be applied.

That means setting any desired properties for the _inner_ table, or for
elements inside it, e.g.
table table { color: black; background: white; }

There's no way to make the color the same as the element enclosing the
outer table, except by explicitly setting it to a particular value.
That is, there is no inheritance that skips nesting levels.
 
E

Els

Jukka said:
<style type="text/css">
table.outer { color: red; background: white; }
</style>
...
<table border="1" class="outer">
<tr><td>foo</td></tr>
<tr><td>
<table border="1">
<tr><td>bar</td></tr>
</table>
</table>

Both "foo" and "bar" will appear in red, since the inner table's cell
inherits the color property from its parent (tr), which inherits the
property from its parent, etc., until we get to the outer table element
that has the property explicitly set. Naturally assuming that no other
style sheet sets the property for the inner cell or any element that is
between it and the outer table in nesting.

Same goes for font-sizes unfortunately, which wouldn't be a problem if
/all/ browsers handled it the same way.
And this is a clue to the answer: There is no way to turn off
inheritance in CSS, since it's a defined part of CSS. Some properties
are inherited by definition, when an element hasn't got a property set
otherwise; some properties are not, but this is part of CSS definition
which you cannot change.

If I have a page with a body font-size of 90%, most browsers will give
the same size to the content of table cells, but IE5 (and probably
others) won't. So, in order to set a 90% font-size on both body and
table cells in all browses, I must define 100% for body, and then 90%
for each separate element, making sure I don't set it for both td and
a parent of td. Nested tables become impossible to get right afaics.
(other than with filter-hacks to set different styles for IE5 than for
other browsers)
What you _can_ do is to set properties to elements so that inheritance
will not be applied.

That means setting any desired properties for the _inner_ table, or for
elements inside it, e.g.
table table { color: black; background: white; }

There's no way to make the color the same as the element enclosing the
outer table, except by explicitly setting it to a particular value.

Which is easy for colours, but impossible for font-size :-(
 
D

David Dorward

Els said:
If I have a page with a body font-size of 90%

So whatever font size the user has picked, its too big?
http://css.nu/articles/font-analogy.html
, most browsers will give
the same size to the content of table cells, but IE5 (and probably
others) won't. So, in order to set a 90% font-size on both body and
table cells in all browses, I must define 100% for body, and then 90%
for each separate element, making sure I don't set it for both td and
a parent of td.

Setting font-size: 100% on table should work around that bug in IE.
Nested tables become impossible to get right afaics.

Data suited to be represented with nested tables is so rare as to make this
a non-issue (and is solved with the above code anyway).
 
E

Els

David said:
So whatever font size the user has picked, its too big?
http://css.nu/articles/font-analogy.html

I know what you mean, I have 100% on my own site and it's good imo.
But my current client insists on 85% (not Verdana though), and as it's
still readable on large monitors and different browsers with different
fonts (sans-serif), I've given up on convincing them that 100% is
better. There will be a 'how to change font-size' instruction in 100%
on the site too, so I don't see it as a big problem.
Setting font-size: 100% on table should work around that bug in IE.

/testing that...

Great, thanks! :)
I had tried it on td, which doesn't work. On table it does the job.
This will save me time on future projects :)
Data suited to be represented with nested tables is so rare as to make this
a non-issue (and is solved with the above code anyway).

Yup, true. It wasn't the nested tables for me that were the problem,
but tables by themselves. It's much easier to set one parent element
to a font-size than all the separate child elements.

Thanks again :)
 
D

David Dorward

But my current client insists on 85% (not Verdana though), and as it's
still readable on large monitors and different browsers with different
fonts (sans-serif)

Ah ha. Is it still readable for Joe X who likes 10px fonts (and thus sets
his browser default to 10px) but finds 8px fonts rather more difficult to
cope with?
, I've given up on convincing them that 100% is
better. There will be a 'how to change font-size' instruction in 100%
on the site too, so I don't see it as a big problem.

They'd better hope that their site offers something unique. I'd be more
likely to go and find a competitor then go through the effort of font
zooming everytime I visited the site.
 
L

Lauri Raittila

Ah ha. Is it still readable for Joe X who likes 10px fonts (and thus sets
his browser default to 10px) but finds 8px fonts rather more difficult to
cope with?

No. I did that, when I had low res. An, it has nothing to do with size,
8px just will go unreadable. (take screenshot and look it as 200% -
don't help much...)
They'd better hope that their site offers something unique. I'd be more
likely to go and find a competitor then go through the effort of font
zooming everytime I visited the site.

OTOH, if set on body, I will never notice difference. Of course that is
just me, havin body {font-size:inherit !important} in my stylesheet.
(yes, I do set font size for html element)
 
E

Els

David said:
Ah ha. Is it still readable for Joe X who likes 10px fonts (and thus sets
his browser default to 10px) but finds 8px fonts rather more difficult to
cope with?

Joe X encounters lots of sites with 75% Verdana, so he's clever, he's
set his minimum font-size too. :p
(I do get your point, but still client is king (to a certain extend))
They'd better hope that their site offers something unique. I'd be more
likely to go and find a competitor then go through the effort of font
zooming everytime I visited the site.

It's not a competitor type of organization, so that's not an argument
I can use to convince them.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,577
Members
45,054
Latest member
LucyCarper

Latest Threads

Top