tags:

views:

188

answers:

4

I'm trying to find a way to place divs side by side without using floats or position:absolute. The divs will have a set width, and may have floated elements inside of them. This is for a CMS where the user will be able to drag content elements to organize them. They do not have to be divs, I just don't know of any other type of HTML tag that would work better.

Basically the end result is for a CMS in which the user can organize content elements by dragging them. Unfortunately with floats, if you want to do anything that involves putting divs underneath each other, everything will go down to below the tallest div above it, even if it could fit underneath something else. i.e. 3 elements, 2 of which should be stacked on the left with a third one on the right that has a height somewhere in between the two.

Inline-block is out as it isn't supported by IE (although I'd love to be a dick and just have chrome frame required...) and doesn't work for this purpose anyway.

+2  A: 

I'm a little confused that you mention dragging elements, but your title states you do not want to use position:absolute as a solution... most scripts I am aware of use that for the dragging process, so why would you not use it for the positioning of it to place them side-by-side?

Scott
Position-absolute is used while they're dragging the elements, but after it's placed, it is no longer used. Right now I use the jqueryUI sortable function to do it, but I'm overhauling it to be more robust.The reason I can't use position-absoulte is because the divs might have floated elements inside of them which causes those elements to be all over the place.
sgrif
But, given your requirements of stacking shorter items next to taller items (as you mentioned), it seems reasonable to me to just leave everything always positioned absolutely, whether it is being dragged or not. Why is it that you do not want to leave them positioned absolutely after a drag event? If it works for the mobile element, it seems like not a big issue to leave it for the immobile element.
Scott
I know float behavior varies by browser, and I have not specifically attempted what you are trying to do, but generally speaking, I believe floats should remain in place based off the container they are in, so I'm not sure how having floated elements in absolutely positioned divs is going to be a problem with keeping them in place within that particular div. They should not be interacting with each other between divs, because each absolute element should be outside the flow of the page.
Scott
Just tested this to double check myself. For some reason I thought that floats ignored their container if the container was set to position: absolute. I think when I came to that conclusion I was also messing with display: inline which caused that... Either way, yeah, it looks like position: absolute is the way to do it.
sgrif
That's good to know.
Scott
A: 

The only option I can think of that doesn't use the techniques you've mentioned (position:absolute, display: inline-block, and float) is to use a table.

<table>
    <tr>
        <td><div id="div1">...content...</div></td>
        <td><div id="div2">...content...</div></td>
    </tr>
</table>

It's possible that you could use:

<div id="container">
    <div id="div1">...content...</div>
    <div id="div2">...content...</div>
</div>

with css:

#container {display: table; } /* you might need another child div with 'display: table-row' but I can't remember off-hand */
#div1 {display: table-cell; width: 50%; /* other css */}
#div2 {display: table-cell; /* width: 50%; other css */}

This is the best I can think of, and I dislike using tables for non-tabular purposes. But to each their own. =/

David Thomas
Unfortunately using it as a table creates the same problems I mentioned with floats. It works fine if you're just creating columns, but if you end up having some divs on the next line, things don't organize like the user would think they should.
sgrif
A: 

Are you just looking for a way to drag/drop and organize content? Have you seen JQuery UI's "Sortable"?

http://jqueryui.com/demos/sortable/

a.feng
No, I'm not looking for a way to drag/drop. That's actually what I was using originally, but I'm overhauling it now to be more robust since just having them be sortable gets wonky in 3d if your objects have a set width, and there's no way to handle things like extra whitespace, etc. I'm overhauling it to a system that just uses jquery ui's draggable, with some tweaks to have it snap to align with edges like photoshop, and be able to place divs inside other divs with floats.
sgrif
+1  A: 

Do you have fixed number of columns ie elements horizontally arranged side by side ? If yes one thing i can think of is having those many floated unordered lists and each element will be an li When an element is dragged and dropped inside the same ul, its index in the ul is changed. When its dragged across uls,its removed from this list and appended to the other and arranged as in case 1. Just have an idea right now... will have to try it

naiquevin
No, I do not have a fixed number of columns, and any columns that are created by the user do not necessarily span the whole document. The system focuses on free floating blocks rather than columns.
sgrif