473,406 Members | 2,707 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Inheritance and specificity

Jim
More of a request for opinions than a direct question, but here
goes.....

When I'm creating a style sheet for any site, the first thing I do is
to analyse the design visuals and work out my standard styles, i.e. h1,
h2, p, cite etc etc. These tags will then be used as is or over-riden
at a more specific level.

I then split the page up in to #header, #nav, #content and #footer
(assuming a standard sort of build). Within the #content I then start
to define my main content. I might have for example a small image
gallery which I'd then define using:

#content #gallery
{
/* code */
}

So first question. The selectors #content and #gallery are always divs,
so should I be using div#content div#gallery? #content is unique and
will always (as far as I can see) be a div so is it really worthwhile
specifying the div in the selector?

Second question. As I said, #gallery is a descendant of #content, but
is it dangerous to define it that way? For example, what if I suddenly
decided for some bizarre reason that a #gallery could go in the #nav -
it'd mean either duplicating code or removing #content from the
selector. So is it even worth defining #gallery within #content rather
than on it's own?

Third question. Those anal designers are always wanting pixel
perfection which often requires tiny positional changes to individual
pages, currently I do this by putting an id in the body tag and then
picking out the element I need to change with the appropriate selector.
Is this a common method? What do you guys use?

Fourth question. Naming conventions, kind of. Often I'll have an image
which has a very attractive multi-border frame around it with dotted
lines and the works. This often requires 2, 3 or even 4 divs simply to
create this effect - other than being semantically inelegant, I find
myself having no other choice than to name them something like so:

<div class="photo_container_a">
<div class="photo_container_b">
<div class="photo_container_c">

</div>
</div>
</div>

Does anyone know of a more elegant naming style under these conditions?
Would you favour a more complex background image rather than doing it
with several divs?

Ok, well that's all for now. I'd really appreciate anyone chipping in
with their thoughts.

Thanks,

Jim.

Aug 1 '06 #1
4 1125
Rik
Jim wrote:
#content #gallery
{
/* code */
}

So first question. The selectors #content and #gallery are always
divs, so should I be using div#content div#gallery?
Not necessary, so don't.
Second question. As I said, #gallery is a descendant of #content, but
is it dangerous to define it that way? For example, what if I suddenly
decided for some bizarre reason that a #gallery could go in the #nav -
it'd mean either duplicating code or removing #content from the
selector. So is it even worth defining #gallery within #content rather
than on it's own?
As it is alway unique, I'd suggest to define it on it's own. It is indeed
more flexible. It's a matter of taste though.
Third question. Those anal designers are always wanting pixel
perfection which often requires tiny positional changes to individual
pages, currently I do this by putting an id in the body tag and then
picking out the element I need to change with the appropriate
selector. Is this a common method? What do you guys use?
I tend to use non-pixel specific layout. Designers who want indentical
pixel-specific placing in all browsers should limit their activities to
printed media and images only, and stay away from the web.

There are differences in pages that have nothing to do with being
pixel-specific though. As my sites are almost always dynamically built, it
depends on what I use: for small differences I tend to use a class on the
body tag (which micht as well be an id indeed), for bigger differences I
specify different stylesheets in the head element.
Fourth question. Naming conventions, kind of. Often I'll have an image
which has a very attractive multi-border frame around it with dotted
lines and the works. This often requires 2, 3 or even 4 divs simply to
create this effect - other than being semantically inelegant, I find
myself having no other choice than to name them something like so:

<div class="photo_container_a">
<div class="photo_container_b">
<div class="photo_container_c">

</div>
</div>
</div>

Does anyone know of a more elegant naming style under these
conditions? Would you favour a more complex background image rather
than doing it with several divs?
About the naming: name the classes to what they actually represent, not how
they look. IN this case I'm not sure what's right. I tend to create my
images on the fly (and caching them) with the desired shadoweffects etc. in
PHP. Saves a lot of HTML/CSS trickery.

Grtz,
--
Rik Wasmus
Aug 2 '06 #2
Jim wrote:
I then split the page up in to #header, #nav, #content and #footer
I'd use .header, .nav, .content and .footer instead.

If you use an id for CSS, then it's extremely specific. This makes it
hard to sub-class(sic) it later on, if you want to over-ride it with a
more-specific class value.

So first question. The selectors #content and #gallery are always divs,
so should I be using div#content div#gallery?
Go with the least specific selector that still works. Over-specific
selectors get troublesome and "sticky" later (see above).

Second question. As I said, #gallery is a descendant of #content, but
is it dangerous to define it that way?
Good practice if these were classes, rather than id.

Superfluous (and tending towards bad) if they're ids. You can't
duplicate ids on a page, so why would you need to repeat the outer id
value in the CSS selector? Just the inner id should be all you need.

They _can_ be useful with ids in a multi-page context with an external
stylesheet. Although you can't repeat ids on a page, you _can_ repeat
them within a site. It's sometimes useful to use pairs of ids to
distinguish between the same element on different pages.

#foo-thing, #bar-thing { font-weight: normal; }

#the-foo-page #foo-thing { font-weight: bold; }

#the-bar-page #bar-thing { font-weight: bold; }

Aug 2 '06 #3
Jim

Andy Dingley wrote:
Jim wrote:
I then split the page up in to #header, #nav, #content and #footer

I'd use .header, .nav, .content and .footer instead.

If you use an id for CSS, then it's extremely specific. This makes it
hard to sub-class(sic) it later on, if you want to over-ride it with a
more-specific class value.
I'm not sure I agree with you on this one. The fact that they are ids
tells anyone looking at the code that these are the unique page
"placeholders". To me, making them classes would be suggesting that
they may be used elsewhere in the code which won't be the case.

Can you elaborate on what you mean by "This makes it hard to
sub-class(sic) it later on"?

Ta,

Jim.

Aug 3 '06 #4

Jim wrote:
Andy Dingley wrote:
Jim wrote:
I then split the page up in to #header, #nav, #content and #footer
I'd use .header, .nav, .content and .footer instead.
I'm not sure I agree with you on this one. The fact that they are ids
tells anyone looking at the code that these are the unique page
"placeholders".
Fine - so use them. If you're intending to access them from DHTML, then
certainly do so. OTOH, needing to access something as broad and blunt
as "content" via DHTML is unusual -- nav-menu is commoner though.
My point is about their use in CSS selectors, not really about HTML.
They're better (from CSS's point of view) as classes, so I'd put them
in the HTML as classes. Quite commonly you'll have id and class on the
same element (even with the same value).
To me, making them classes would be suggesting that
they may be used elsewhere in the code which won't be the case.
That's one reason - but this is another.
If you use an id for CSS, then it's extremely specific. This makes it
hard to sub-class(sic) it later on, if you want to over-ride it with a
more-specific class value.
Can you elaborate on what you mean by "This makes it hard to
sub-class(sic) it later on"?
<html>
<title>Using Class vs. id for CSS with major page components</title>

<style type="text/css" >
..nav-menu,
#nav-menu { background-color: green; }

..fluffy { background-color: pink; }
</style>

<body>

<h2>Basic colour scheme</h2>
<ul class="nav-menu" >
<li>One</li>
<li>Two</li>
<li>Three</li>
<li>Four</li>
</ul>
<h2>This won't work (change colour)</h2>
<ul id="nav-menu" class="fluffy" >
<li>One</li>
<li>Two</li>
<li>Three</li>
<li>Four</li>
</ul>
<h2>This will work (changes colour to pink)</h2>
<ul class="nav-menu fluffy" >
<li>One</li>
<li>Two</li>
<li>Three</li>
<li>Four</li>
</ul>

</html>

Aug 3 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: AIM | last post by:
Error in msvc in building inheritance.obj to build hello.pyd Hello, I am trying to build the boost 1.31.0 sample extension hello.cpp. I can not compile the file inheritance.cpp because the two...
2
by: Graham Banks | last post by:
Does using multiple inheritance introduce any more performance overhead than single inheritance?
5
by: Harlan Messinger | last post by:
If I want to give special priority to property values I want to declare for a particular class, should .myclass.myclass.myclass.myclass.myclass.myclass.myclass.myclass.myclass { /* declarations...
4
by: JKop | last post by:
I'm starting to think that whenever you derive one class from another, that you should use virtual inheritance *all* the time, unless you have an explicit reason not to. I'm even thinking that...
22
by: Matthew Louden | last post by:
I want to know why C# doesnt support multiple inheritance? But why we can inherit multiple interfaces instead? I know this is the rule, but I dont understand why. Can anyone give me some concrete...
5
by: eiji | last post by:
Hi folks, I hope this is not "off topic"! :-) Consider the next code: /* Declarations of types that could become platform-dependent */ #define MyChar char #define MyInt int
8
by: sajid | last post by:
The CSS 2.1 Specification describes how to sort a list of selectors in order of specificity, but it doesn't provide a method to calculate the specificity of a single selector in isolation. I've...
10
by: paul.denlinger | last post by:
Hi-- I'm working my way through the book, Beginning CSS Web Development by Simon Collison. I ran into a problem on pages 17-18 (Chapter 2), I have copied and put in three rules re IDs and the...
6
by: Bart Simpson | last post by:
I remember reading on parashift recently, that "Composition is for code reuse, inheritance is for flexibility" see (http://www.parashift.com/c++-faq-lite/smalltalk.html#faq-30.4) This confused...
1
by: PaulShimm | last post by:
I have this simple div with a label in: <div id="TestDiv"> <label>TEST CSS</label> </div> I am applying the following CSS: div#TestDiv *
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.