Forum Moderators: not2easy
<div class="dwMenu"><a href="index.div.html">HOME </a> <a href="potrait/index.html">LANDSCAPE</a> <a href="potrait/index.html">PORTRAIT </a></div>
CSS:
div.dwMenu {
padding-top: 5px;
padding-bottom: 5px;
}
.dwLinks a:link {
font-weight: bold;
color: #FFFFFF;
text-decoration: none;
}
.dwLinks a:visited {
font-weight: bold;
color: #FFFFFF;
text-decoration: none;
}
.dwLinks a:hover {
font-weight: bold;
color: #FF0000;
text-decoration: none;
}
So what is the cleanest way to do this?
I want to assign a particular class to a div, and I also want to use an alternate link set as given above. So how do I specify a class of css rules plus allow that specific link set to work in that class only. It's like I have two classes that I can't combine--or can I? The link set is it's own class, per above example, and so is the div class. How do I get the links to work inside that div class only? I'm thinking it's something like div.dwMenu dwLinks
using the above example?
I'm going to take a guess and say, is what you want this: a particular
div that has a class attached, giving it, say, a red background colour. But, this particular div is special, and not only do I want it to have a red background colour, I want it to have a background image assigned to it's top left-hand corner. div.dwMenu { background-colour: #f00; }
div.dwMenu.special { background: url('/images/black-dot.png') no-repeat 0 0; } This means you can combine classes, like so:
<div class="dwMenu special"> ... </div> Please note, sometimes IE 6 will have a problem displaying this style of selector (
selector.class.class or selector#id.class,) and unless your specificity is very strict, you probably can remove the .dwMenu on div.dwMenu.special so it looks like this: div.dwMenu { background-colour: #f00; }
div.special { background: url('/images/black-dot.png') no-repeat 0 0; } Otherwise, the other thing I thought you might have meant was:
I want to a particular
div with a class, to display a set of links in a certain way. Except, one of them, the "Home" link in particular, I want to display in BIG text: div.dwMenu a { font-size: 1em; }
div.dwMenu a#home { font-size: 1.5em; } <div class="dwMenu">
<a href="#" id="home">Home</a>
<a href="#">Other link</a>
<a href="#">Other link</a>
</div> Like that :)
Let us know if that's not quite what you're after :)
[edited by: Setek at 8:09 am (utc) on Jan. 16, 2007]
CSS is as follows:
div.dwMenu {
padding-top: 5px;
padding-bottom: 5px;
}
.dwLinks a:link {
font-weight: bold;
color: #FFFFFF;
text-decoration: none;
}
.dwLinks a:visited {
font-weight: bold;
color: #FFFFFF;
text-decoration: none;
}
.dwLinks a:hover {
font-weight: bold;
color: #FF0000;
text-decoration: none;
}
So what I wanted to do is use the class "dwMenu" and use the alternate linkset class "dwLinks" in the same div tag:
<div class="dwMenu"> <span class="dwLinks"><a href="index.div.html"> HOME </a> <a href="potrait/index.html">LANDSCAPE</a> <a href="potrait/index.html">PORTRAIT </a></span></div>
Now using the span withing the div works, but is there a more eloquent way of doing this? This way requieres me to stop and manually insert span tags, instead of simply adding a class.
Thanks for helping.
div.dwMenu { background-colour: #f00; }
div.dwMenu.special { background: url('/images/black-dot.png') no-repeat 0 0; }This means you can combine classes, like so:
<div class="dwMenu special"> ... </div>
Please note, sometimes IE 6 will have a problem displaying this style of selector (selector.class.class or selector#id.class,) and unless your specificity is very strict, you probably can remove the .dwMenu on div.dwMenu.special so it looks like this:
div.dwMenu { background-colour: #f00; }
div.special { background: url('/images/black-dot.png') no-repeat 0 0; }
Thus, you can combine dwMenu and dwLinks like so:
CSS:
div.dwMenu { ... }
div.dwMenu.dwLinks { ... } or
div.dwMenu.dwLinks can just be div.dwLinks HTML:
<div class="dwMenu dwLinks"> ... </div>
div.dwMenu { ... }
div.dwLinks { ... } With this:
<div class="dwMenu dwLinks"> ... </div> ... will work in everything. It is the cleanest, most compliant.
Using a
<span> is unnecessary. Using added specificity, to have very closely-knit rules, is a little buggy in IE 6. So I would avoid using
div.dwMenu.dwLinks, but <div class="dwMenu dwLinks"> ... </div> will work regardless. It's when you have, say,
div.dwLinks defined above div.dwMenu in your stylesheet, but you actually want the value of, say, font-weight: bold; from div.dwLinks (which gets overridden in div.dwMenu with font-weight: normal;,) that an added specificity is required - which is a perfect situation why there's a use for div.dwMenu.dwLinks :) If you don't know much about specificity, then you probably don't really have to worry about it for now, just know that the method I used at the top of this reply works.
This is the syntax for rules:
selector { property: value; } so:
dwMenu { property: value; } .. is basically saying "for every element named
dwMenu do this." And, without being proper XML/XHTML (using custom element names) this is incorrect. Rather than:
selector.class-name { property: value; } .. which is saying "for every element named
selector with the class named class-name do this." So this:
selector#id { property: value; } means an ID. selector#id.class-name { property: value; } means a selector, with an ID, which also has a class of this. selector.class-name.second-class-name { property: value; } means a selector, with a class, which also has a second class of this. We are saying:
div.dwMenu { property: value; } Which means "for every element named
div with the class named dwMenu do this." :)
So why could you not just name a div tag with a selelctor that has no prefix, .name:
css would be .name
<div class="name"> </div>
Why would that not work? But I do see the importance of div.name because then you can have two items named the same thing, but they will only apply to specific tags. So you could have paragraphs ooking different inside div's only. Is that right?
So why could you not just name a div tag with a selelctor that has no prefix, .name:
You can! I was merely saying you cannot have
name, unless the element is named name, but you can have .name :) Why would that not work?
It will.
But I do see the importance of div.name because then you can have two items named the same thing, but they will only apply to specific tags. So you could have paragraphs ooking different inside div's only. Is that right?
Yes, this is right - the reason you would do this is if you had differing styles with the same name, you can easily change the element type to display differently.
Also, it's all about specificity. You can easily identify that
div#shell div#menu, for instance, is a div with that specific ID and it has to be inside a div with the specific ID of "shell". At quick glance of the CSS, one can easily understand exactly where the object is in the structure of the (X)HTML document :)
So if you had a div with teh class of "contianer" you could apply additional clases to, so you would have css such as:
div.container
div.container p
and the div you wanted to apply both those styles to would be
<div class="contianer contianer p">?
If this is corect, you could also, I assume, do this css:
div.container
div.p
and then apply it like so:
<div class="contianer p">?
I was wondering too, if I have two or three classes to apply in a true "cascading" order, if each preceeding tag overides teh last if tehr are two classes that conflict:
.div Contaner
.div Pricing
so now both have a Text-... tag, but one is aligned left and other aligned center. Does the class last stated in the class= line take precedent? For instance, if "Container" has text aligned center and Pricing has text aligned left, would the text be left aligned if you had:
class="Container Pricing"
?
If so then I've had anotehr break through with css. I can name all my "main Content" divs the same thing,a dn then just add on additional styles as I go, instead of having differnt names for each continaer. This allows me to keept eh main continaer spacing, for instance, standard throughout the site. I hope I have this correct.
The problem I'm having is that even though a class is applied to a div that the table is in, the styles don't always cascade into the table's TDs. If I can use divs and css to align the form elements, I would just rebuild it.
.div Contaner
.div Pricingso now both have a Text-... tag, but one is aligned left and other aligned center. Does the class last stated in the class= line take precedent? For instance, if "Container" has text aligned center and Pricing has text aligned left, would the text be left aligned if you had:
class="Container Pricing"
?
Your syntax is a little incorrect. It should be:
div.Container { text-align: left; }
div.Pricing { text-align: center; } In which case, in the situation of:
<div class="Container Pricing"> ... </div> ..
div.Pricing' value for text-align will win. Why? Because the specificity of the rules are exactly the same (1 point for the selector div, 10 points for the class, equals 11 points,) which means the rule that wins is the one that comes last (since it will paint the first rule first, then paint the second rule.) The only other problem is that when using tables when necessary, like aligning form elements, it's a whole diferent sotry.
You don't need to use tables for that instance :) If you're talking "Name: [input box]" style of thing, all lined up prettily, you can just use
label. You can set up your form as a proper list this way - but this is off-topic, if you want to ask questions about how to style form controls, it's better if you make another post. P.S.: you don't need a tonne of
divs either. That's what we call "divitis" :)
My real css is like you have it:
div.name
That was just a typo.
OK, but that didn't work unless the class in the css came after the other one. In other words, if the class that had centered text came last in the style sheet, it would overide the aligned center text. Is this right, or is something else wrong? The thing is, after I changed the position in the style sheet, it worked.
So what do you eman by "divitis"? Do you mean that I could use something like
.name and just use that as the class for divs, or what? I don't want divitis! I'mm actually goign to do this site as cleanlya s I can time permitting, and with all css and NO tables. I'm never going to use tables again.
OK, but that didn't work unless the class in the css came after the other one. In other words, if the class that had centered text came last in the style sheet, it would overide the aligned center text. Is this right, or is something else wrong? The thing is, after I changed the position in the style sheet, it worked.
This is what I meant by last - it had a greater line number in the stylesheet (farther down) than the other.
So what do you eman by "divitis"? Do you mean that I could use something like
.name and just use that as the class for divs, or what? I don't want divitis! I'mm actually goign to do this site as cleanlya s I can time permitting, and with all css and NO tables. I'm never going to use tables again.
What I meant by divitis is the overuse of
divs when not necessary. Some people switching from tabled layouts to CSS-P have it; a tendency to wrap anything and everything in a div, even when it might semantically not be good to do so. The part about wrapping each form control in a
div is the part I was talking about - lots of divs, a lot less of necessity :) P.S.: tables are great!
.. which is to say, when you have a set of data that happens to be tabular, tables are definitely the semantic way they should be structured.
Just as you shouldn't use tables to define the presentation of data that isn't tabular data, so should you not use other controls to define the presentation of data that is tabular data.
Everything has its place: tables,
divs, cites, addresss, etc. It's how one uses them that can be the problem.