|
Posted by Andy Dingley on 11/01/14 11:45
Noozer wrote:
> Is there a way to refer to another element when defining they properties of
> an element in CSS?
CSS doesn't have elements. HTML has elements, CSS has statements or
"rules" (roughly the scope of the curly brackets) and declarations
(setting a named property to a value)
http://www.w3.org/TR/CSS21/syndata.html#q7
To get _anywhere_ with sophisticated CSS, you really have to start
understanding how the cascade rules work in selecting the rules that
will be applied to the document. Part of this involves understanding
what's HTML and what's CSS.
What you're asking for looks like some sort of "CSS macro" where your
CSS rules would be able to refer to other CSS rules (NB - not HTML
elements, as you stated). CSS doesn't have this.
What CSS does have is a set of selection, inheritance and cascading,
such that you can bind CSS rules to the document tree.
http://www.w3.org/TR/CSS21/cascade.html
Although it's quite different from what your example described, it's
closer to what you asked for.
A well-designed cascade of CSS rules can avoid much duplication by
using CSS' rule selection. If you want a set of declarations to apply
to particular scopes, then grouping them into rules and picking the
right selector can give you neat, compact CSS. Of course this is much
harder to understand than it sounds! In particular it seems to confuse
anyone with experience to OO coding, as it looks similar but behaves
quite differently.
As a useful technique to learn it, write some horrible CSS and then
validate it. You'll generally get the equivalent cascade displayed to
you afterwards which can often be either illuminating for how the
selectors had worked, or might even be the basis for a refactored
simplified stylesheet.
Navigation:
[Reply to this message]
|