9

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?

2
  • 2
    There is an obscure feature in CSS3 that would allow this - 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. Commented Jan 15, 2011 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. Commented Jan 15, 2011 at 12:55

5 Answers 5

20

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.

Sign up to request clarification or add additional context in comments.

Comments

6

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

$('#FeaturedContent').prependTo('#main');

Comments

5

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.

2 Comments

but not one above the other........unless the element are fixed/absolute/relative positioned. upvote
How and How?...
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"));

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.