Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

CSS Namespace and Inheritance

Status
Not open for further replies.

Genimuse

Programmer
May 15, 2003
1,797
US
I don't quite understand how the CSS namespace works, nor how things are inherited. Here's my situation:

In the top right corner of this intranet page there will be, say, three icons that link to commonly used pages in the site, but will be customized (on the server side via ASP) for each user. In effect there are 3 "slots" where icons (with links) can appear, and any icon can appear in any slot.

I define the slots like this:
Code:
#icon1 {
	position: absolute;
	right:130px;
	top:10px;
	width:60px;
	height:50px;
	}

#icon2 {
	position: absolute;
	right:70px;
	top:10px;
	width:60px;
	height:50px;
	}
That kind of thing. Then I have these mouseover (hover) icons that change shape when the user moves over them, icons that fit in the "slots," like this:
Code:
#phone a {
	display: block;
	padding-top: 50px; height: 0px !important;
	height /**/: 50px; overflow: hidden;
}

#phone {
	width: 59px;
	background-image: url(gfx/iconPhone.gif);
}
#phone a {
	background-image: url(gfx/iconPhone.gif);
}

#phone a:hover {
	background-position: 0 -50px;
}

Each one individually works, but I want to combine them into the same space.

Because I don't understand how to have both things be true (due, I think, to a lack of understanding of the namespace and inheritance), I currently am forced to have a TON of styles, one for every icon in each possible slot, like this:
Code:
/* PHONE in position 1 */
#iconphone1 {
	position: absolute;
	right:130px;
	top:10px;
	width:60px;
	height:50px;
	}

#iconphone1 a {
	display: block;
	padding-top: 50px; height: 0px !important;
	height /**/: 50px; overflow: hidden;
}

#iconphone1 {
	width: 59px;
	background-image: url(gfx/iconPhone.gif);
}
#iconphone1 a {
	background-image: url(gfx/iconPhone.gif);
}

#iconphone1 a:hover {
	background-position: 0 -50px;
}
And then there's "iconphone2" and "iconphone3," and of course "iconcalendar1" and "iconcalendar2," etc., ad naseum.

I assume there's some kind of thing I can do if my stylesheets are designed correctly where I can reference something like "icon1.phone" or "icon2.phone" or "icon1.calendar," etc., but I'm not sure how to do it.

I apologize in advance if this is a really dumb question, as I'm only just learning CSS and know I've got a lot to learn.
 
I'm very familiar with doing them in Javascript (I've done it dozens... no, probably hundreds of times), but I disagree that it's necessarily the best thing to use. CSS requires a lot less code and works beautifully, and is super-easy to modify. I'm very happy with it overall.

Because this project is an intranet I know which browsers will be used and so don't have to worry about degradation for older browsers, but using unordered lists as a menu rollover degrades beautifully to older browsers and even text browsers.

Javascript is great, but I don't see how it's inherently better than CSS for this purpose.
 
CSS requires a lot less code
if there is no solution to your first post, then javascript requires a lot less code

AFAIK, there is no solution to your first post

--Chessbot
 
If there's no solution then I can see how it might be less code.

Though thinking about how to do it with Javascript, I'm not sure it would be that much smaller (depending on the number of icons), but I would be able to encapsulate the rollover code at least.

Stil, I'm hoping for a CSS solution.
 
The solution turns out to be really simple.

Define the icon "slots" using id (like div#icon1) and the icon items using class (like div.phone), and then apply the id and class to the div in the HTML. Works like a champ, very cool.
 
Genimuse,

that's the way to go, and is what i would have suggested had you not figured it out already - common shared styles in classes, item-specific styles in id's

additionally you can piggyback classes for "multiple inheritance" of sorts

.foo {
color:red;
}
.bar {
font-weight:bold;
}

<span class="foo bar">i have foo and bar class attributes</span>

=========================================================
-jeff
try { succeed(); } catch(E) { tryAgain(); } finally { rtfm(); }
 
Ah, excellent, I didn't know that, either. Thanks very much.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top