tags:

views:

113

answers:

5

Hi all, just wondering if there is a better way to write this CSS? It repeats quite a lot, this is a simple example, does it have to be this way?

<style type="text/css">
    div#leftBlock
    {
        position:absolute;      
        z-index: 1;
    }
    div#rightBlock
    {
        position:absolute;
        z-index: 1;
    }
    div#centerBlock
    {
        position:absolute;
        z-index: 2;
    }
    div#animateblock
    {
        position:absolute;
        z-index: 3;
    }
</style>

...

<div id="leftBlock" onclick="leftClick()"></div>
<div id="rightBlock" onclick="rightClick()"></div>
<div id="centerBlock"  onclick="centerClick()"></div>
<div id="animateBlock"></div>
+3  A: 

Use CSS classes. For example:

.pa { position:absolute; }

div#leftBlock
{   
    z-index: 1;
}
div#rightBlock
{
    z-index: 1;
}
div#centerBlock
{
    z-index: 2;
}
div#animateblock
{
    z-index: 3;
}

Then your html will be

<div id="leftBlock" class="pa" onclick="leftClick()"></div>
<div id="rightBlock" class="pa" onclick="rightClick()"></div>
<div id="centerBlock"  class="pa" onclick="centerClick()"></div>
<div id="animateBlock"class="pa" ></div>
theycallmemorty
HTML classes. CSS class selectors. CSS doesn't have classes.
David Dorward
I would change `.pa` to `.layout-block` since giving your elements classnames like `.pa` or `.position-absolute` is essentially the same as using inline CSS and therefore mixing presentation and content. After all, you may eventually want to redesign your layout and not have your layout blocks positioned absolutely.
Lèse majesté
This seems to add a lot of unnecessary classes to the divs. You increase html and did not significantly reduce css.
Scott
Thanks for the ideas though, but this solution seems to add too much stuff on the page than it's worth, although for more complex examples it would work well.
Tom Gullen
A: 
    div 
    { 
        position:absolute;       
    } 
div#leftBlock, div#rightBlock 
    { 
        z-index: 1; 
    } 
 div#centerBlock   
    {   
         z-index: 2;   
    }   
 div#animateblock 
    { 
        z-index: 3; 
    } 

EDIT: just using id's

#leftBlock, #rightBlock, #centerBlock, #animateblock  
{
  position:absolute;
}
#leftBlock, #rightBlock  
{  
   z-index: 1;  
}  
#centerBlock    
{    
   z-index: 2;    
}    
#animateblock  
{  
   z-index: 3;  
}
Mark Schultheiss
-1 - This would be assuming a lot that all divs are to be `position: absolute`
Scott
Yes, I don't need it to apply to all the divs on the page just these 4.
Tom Gullen
Mark Schultheiss
Additional note: you can drop the "div" at the start if you wish to only use the id's
Mark Schultheiss
+3  A: 

This perhaps:

#leftBlock, #rightBlock
{
    position:absolute;      
    z-index: 1;
}
#centerBlock
{
    position:absolute;
    z-index: 2;
}
#animateblock
{
    position:absolute;
    z-index: 3;
}

Edit added this: the most minimalist way would be to code the html like so (drop the ids):

<div class="layout" onclick="leftClick()"></div>
<div class="layout" onclick="rightClick()"></div>
<div class="layout centerBlock" onclick="centerClick()"></div>
<div class="layout animated" ></div>

and the css like so:

.layout {position: absolute; z-index: 1;}
.centerBlock {z-index: 2;}
.animated {z-index: 3;}

But that depends on your needs, as you may need the ids for something else.

Scott
Looks good thanks!
Tom Gullen
Don't do this. This is no different to doing: <div style="position:absolute"> - naming classes as you intend to style them defies the point of CSS entirely - Lèse majesté has explained this well below.
lucideer
@lucideer - it depends on his needs. My class names in my example could be anything more semantic he may desire. The point is, if he is going for absolute minimum css code, then the multi-class way is the way to go (whatever the class names). And, in my opinion, it is quite a bit different than inline styling, as the styling is still controlled in the css.
Scott
@Scott - I'm not criticising the multi-class method - it's **structurally** the best one that's been suggested here sofar. And I'm definitely for minimising code as much as humanly possible. However my point still stands - you say he could use more semantic class-names but you've not recommended any such in your answer. If you recommend it - you should say it.
lucideer
@lucideer - how could I recommend it? I don't know the relation of these blocks other than one is on left, right, center, and animated, so what *single* class name to use for in place of "abs" for his situation is a mystery to me. I suppose I could, based off his original ids, come up with something more semantic for the extra z-index classes. I'll edit for that.
Scott
A: 

Use a generic block class to apply the positioning, combine the left and right block declarations, and there is no need to say "div" each time when you have unique IDs on each element. (In fact it not only is it not needed, but it will make your CSS slower.)

CSS:

.block { position:absolute; }
#leftBlock, #rightBlock { z-index: 1; }
#centerBlock { z-index: 2; }
#animateblock { z-index: 3; }

And HTML:

<div id="leftBlock" class="block" onclick="leftClick()"></div>
<div id="rightBlock" class="block" onclick="rightClick()"></div>
<div id="centerBlock"  class="block" onclick="centerClick()"></div>
<div id="animateBlock"class="block"></div>
Ian Storm Taylor
I will add that selecting by id "#" results in slightly more efficient rendering. Qualifying by tag is very inefficient.
Plan B
@Plan B: could you elaborate on that? Are you saying that `div, span {...}` is less efficient to render than `#foo, #bar, #tofu {...}`?
Lèse majesté
That's exactly what he's saying. IDs are unique so it's easy for the engine to find them quickly, so no need to qualify them with tags as well (since they're unique after all...).
Ian Storm Taylor
@Lèse majesté: Yes. Essentially the browser constructs a "tree" of the entire dom as the HTML document is read. When CSS is applied, the client will search the tree for the appropriate nodes to apply the styles to. So, when you say `div#something { ... }` you're saying "First, find all `div` elements. Then, look for `#something`." Since ID's are unique per the standard (as Ian says), when you say `#something` you're saying "Find #something" -- it will not have to search the entire `div` tree, it will simply search the index of IDs it has cached. Reference: http://bit.ly/4TdqS
Plan B
Plan B makes a good enough point, but I do think it's worth noting that if using element selectors is actually negatively affecting performance, you've probably got other, more serious issues with your CSS. As the 2nd paragraph of that linked article says *"the fewer rules the engine has to evaluate the better"* - as long as your CSS is reasonably sized, any performance "hit" due to element selectors should be in the region of millionths of a second (or less?).
lucideer
@Plan B good call.
Lèse majesté
@lucideer Absolutely. Personally, I'll take the performance hit if it means slightly more readable/semantic code. I was merely bringing it up as a point of consideration. I was going to follow up with something along the lines of what you stated. Thanks :)
Plan B
+1  A: 

Most of the above methods are great (except for the ones that apply style-related class-names) - if you wanted to shorten your HTML a little more (rather than applying both an ID and a class to each <div/>, you could also do:

<div class="something"> </div>
<div class="something"> </div>
<div class="something somename"> </div>
<div class="something somethingrelevant"> </div>
.something{ position:absolute; z-index:1; }
.somename{ z-index:2; }
.somethingrelevant{ z-index:3; }

(the class-names I've added are odd looking because I don't know what these <div/>s you have contain - its recommended that you always use class-names that relate to your content, not to your styles)

lucideer