7

say I have layout like so:

<div id="main">            
    <div id="NormalContent">
        {some content which is a fixed length list}
    </div>
    <div id="FeaturedContent">
        {some content which may contain a long list}
    </div>
</div>

and I want to place FeaturedContent above NormalContent.

Can I do this with CSS? I assume I can with Javascript?

Sam Holder
  • 32,535
  • 13
  • 101
  • 181
  • 2
    There is an obscure feature in CSS3 that would allow this - http://www.w3.org/TR/css3-content/#moving - Sadly that document is over 7 years old, and no browser has, as far as I know, made any attempt to implement this. – Alohci Jan 15 '11 at 12:53
  • 1
    +1 Alohci, that is indeed obscure. to be honest, I'm glad that there aren't a bunch of CSS designers moving content around like that, so I'm not to sad the feature isn't implemented. – davin Jan 15 '11 at 12:55

5 Answers5

18

For a non-jquery answer

var content = document.getElementById('FeaturedContent');
var parent = content.parentNode;
parent.insertBefore(content, parent.firstChild);

Note that there is no need to remove it from the DOM, adding it a second time will move it.

Hemlock
  • 6,130
  • 1
  • 27
  • 37
6

this is a clearer way to move them with 1 line of jQuery:

$('#FeaturedContent').prependTo('#main');
davin
  • 44,863
  • 9
  • 78
  • 78
4

In Javascript you could simply reorder them on the page.

With CSS it would be possible to swap them left and right if they were side by side (with some conditions), but not one above the other.

sevenseacat
  • 24,699
  • 6
  • 63
  • 88
2

If you know exactly the dimension of FeaturedContent & NormalContent, you might be able to get away with using position:relative and playing around w/ their top properties.

i.e.

#NormalContent {
  position:relative;
    top:22px;
}

#FeaturedContent {
  position:relative;
    top:-22px;
}

But to make them flow... hmm... not sure you can do that.

With jQuery you can do something like:

 $("#NormalContent").remove().insertAfter($("#FeaturedContent"));
Jimmy Chandra
  • 6,472
  • 4
  • 26
  • 38
0

This answer seems to work aswel. CSS(3) only, very elegant solution.

CSS have a div that's to the right below the left div. Divs are reverse in HTML

Community
  • 1
  • 1
chocolata
  • 3,258
  • 5
  • 31
  • 60