开发者

How to move all content (relative,absolute,...) down?

开发者 https://www.devze.com 2023-03-29 19:08 出处:网络
Let\'s say I have a unknown page with some content positioned r开发者_Go百科elative, some absolute.

Let's say I have a unknown page with some content positioned r开发者_Go百科elative, some absolute. It has some scripts and there can be also lot of styles.

Is there any universal solution how to make on "every" page content moved down by 50px (for example), to make an empty space on top of page?

Using JS/CSS only. Validity has lower priority than functionality, you know..


Add a wrapper div and put a margin/padding on that.

You'll have to add positioning to the wrapper (absolute, relative, or fixed), anything but static, so that its children will position to it.

JSFiddle Example


Browsers behave differently when adding CSS to the body tag, which is why I don't recommend modifying that.


I suspect you want to do this for the "alerts" that you see at the top of websites now (like stackoverflow). The way I normally do this is to add a div with id message as the first div after the body tag.

If you can't do this in the html, then just use JS to insert div#message as the first div in the DOM when the body tag opens. This way, whatever space the div takes, everything else will be moved down.

If you have absolute positioned elements, you should have all you pages content is a div wrapper which should have position set to relative. That way when the wrapper is shift down so will all the content be. Even if you can't modify the html, I'm sure there is such a wrapper in your html, just use CSS to change it's position property.

In jQuery you can just use prepend to add a div as the first child of body.

$('body').prepend("<div id='message'></div>");

In pure javascript something like (untested):

var myDiv = document.createElement('div');
myDiv.setAttribute("id","message");
insertBefore(myDiv,document.body.firstChild);
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号