Use CSS to reorder DIVS?

I have a set of divs.  What I want is to be able to reorder them dynamically.  For instance, one client may want to display 1, 2, 3, 4 (vertical) and another might want to show the page with modulels in different order, like 3, 2, 4, 1.  Each client could have a different layout.  

They will also be able to select different font styles and sizes, so the divs won't have the same size for each client.  I'm cosing for IE (using ASP).  Is there some way to do this without a lot of code-behind or similar?  Ideally it would be purely CSS.
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

Steve BinkConnect With a Mentor Commented:
Another idea, based in javascript.  This means you should not have to reload the page to reorder the divs, but this is not a purely CSS solution.

Create a javascript array of the heights of each of the 4 divs.  When a reorder is completed you should have an array of the new order.  For each div in the neworder, set its top property to be (top of first div + height of every div before it).  For example, when the page starts, maybe it is array(1,2,3,4).  After reorder will look like:

var baseheight=100;  // the absolute top of the very first div
var heights=(220,180,60,40);
var neworder = (2,1,4,3);  // set by the user through your interface
for (w=0;w<neworder.length;w++) {
  thisdiv = neworder[w] - 1; // index starts at 0
  newheight = 0;
  for(z=0;z<w;z++) {
    newheight += heights[neworder[z]];  //get the height for the div pointed to by neworder[z]
  // now newheight hold the combined height of previous divs
  // add the baseheight to get the new top value
  newtop = baseheight + newheight;
  // set the properties on div[w] now
  document.getElementById('div' + w);

This is rough, and will need love to work properly, but should give you the general idea.  The arrays for original heights can be constructed after the page has been loaded, or maybe even database driven.
CSS is client side. not server side.

Is the user going to choose what order they would like the divs in? or is it random?

The way i can think of doing this is to use dynamic CSS, i.e the CSS is written out using server side code.


your CSS file can be a server side code file, like styles.asp and in there you can conditionally exclude or write out CSS


<link rel="stylesheet" type="text/css" title="alt" media="screen" href="style.asp?order=1" />

you could write out the link rel with a random number or a number option chosen by the user.


<link rel="stylesheet" type="text/css" title="alt" media="screen" href="style.asp?order=<%=anyRandomNumber%>" />

in the file you can then use server side loops, conditional statements, switch case etc. and output your CSS. but for this to work, you must set the MIME content type of the file to "text/css", this can be done in asp as
<% Response.ContentType = "text/css" %>

ideally put the MIME content type statement as the first line. then in your style.asp you could do something like this;

so your styles.asp CSS file could look something like this;

<% Response.ContentType = "text/css" %>
body {
if request("color") = "" then
color: red;
color: <%=request("color")%>;
end if

'check querystring value
if request.querystring("order") = 1 then

'put one order of divs here

elseif request.querystring("order") = 2 then

'put different order of divs here


'put another order here

end if
jdhibberdAuthor Commented:
It took a bit of massaging, but the javascript is working like a charm.  Thanks for your help.
All Courses

From novice to tech pro — start learning today.