tags:

views:

163

answers:

6

I a template, I have five DIV elements underneath each other on a HTML page. (Logo, top menu, sub menu, content, footer).

I want the user to be able to set the alignment for each DIV in a CMS: left, center, right. Those settings I used to translate to

margin-left: auto; margin-right: 0px; // right
margin-right: auto; margin-left: 0px; //left 
margin-left: auto; margin-right: auto; // center

for each DIV. Now however, I want the user to be able to specify a real margin for each side as well. That, of course, makes the margin attribute unusable for positioning.

I do not want to use float for the positioning as this will bring additional layout troubles. I would have to surround the DIVs with overflow: auto wrappers and so on.

My current idea is to have two nested DIVs. The outer one takes care of the positioning (margin:auto), while the inner one sets the user-specified margins.

I fear however that this will bring me additional woes when variable widths come into play, or inner padding.

Is there a simpler way than that to align a block element left, right, or center?

IE7 used to listen to <div align=center> and center block elements surrounded by it. Something like that (but valid and cross-browser) would be exactly what I'm looking for.

A: 

If you want to add a margin on the page, encapsulate all your other divs in the user-modifiable one.

warren
Nope, the margin is a setting that can be made for each DIV individually.
Pekka
right, but if you encase them all inside something else, it should be based off the enclosing div, no? Or, at least that's what I do on sites I've managed before
warren
my understanding is that margins are based-off the enclosing element, and should therefore be based off an enclosing div if everything else is tucked inside it
warren
A: 

If you won't care about any border, paddding may be an option?

Bas
Unfortunately, no... padding is a cross-browser nightmare and I fear that even more than I fear float. :)
Pekka
+1  A: 

You might look into a CSS Grids component to handle the flexibility and cross browser compatibility you're looking for. I implemented a similar feature into a CMS once using YUI Grids CSS. There are several other CSS Grids solutions out there, like the 960 Grid System and blueprint, but I don't have any personal experience with those.

It's not exactly simpler, but it is easier when dealing with cross browser compatibility.

Jason
I am really not keen to add a grid system to what is already quite a big thing, but you may be right.
Pekka
+2  A: 

I position DIVs in <table> elements and then I hide from the angry crowd of CSS purists with torches and pitchforks. :)

Carl Smotricz
:) :) Even better: One could put nice <div>s into the source code, but change them to <table>s using JQuery later. Then obfuscate the JQuery, get a torch and pitchfork of your own and join the crowd. ;)
Pekka
I ended up doing it some other way - I don't remember right now - but I'm accepting this for the laughs :)
Pekka
A: 
#topmenu, #submenu, #content, #footer {
  clear: both;
}

should allow to float any of your five div to left or right. edit: and other divs should remain in place.

Felipe Alsacreations
A: 

"Now however, I want the user to be able to specify a real margin for each side as well. That, of course, makes the margin attribute unusable for positioning."

You could use a dynamic style sheet w/ php. You can pass the user specified variables to the style sheet... something like this:

<?php

header("Content-type: text/css");

$left = $_POST['left'];
$right = $_POST['right'];

?>

div#right {
     margin-left: auto; 
     margin-right: <? echo $right ?>px; // right
}

div#left {
     margin-right: auto; 
     margin-left: <? echo $left ?>px; //left 
}
luckykind