Let's say I have a unknown page with some content positioned relative, 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..
Let's say I have a unknown page with some content positioned relative, 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..
Share Improve this question edited Aug 20, 2011 at 23:16 ckittel 6,6464 gold badges42 silver badges71 bronze badges asked Aug 20, 2011 at 23:05 Marek SeberaMarek Sebera 40.7k37 gold badges164 silver badges249 bronze badges 4-
1
Try a top margin of 50px on the
body
element.... – Ray Toal Commented Aug 20, 2011 at 23:14 -
and content will be placed next to
<body>
element? – Marek Sebera Commented Aug 20, 2011 at 23:18 - 2 (Moving the body down is bad practice leave the body as margin:0) You should enclose your page content with new div tags Id it and move it down via top:50px;. – maximumcallstack Commented Aug 20, 2011 at 23:24
-
Be careful about your
Validity has lower priority than functionality, you know..
, you're going to spur a debate, where everything is possible and able to get there with having valid code. – vol7ron Commented Aug 20, 2011 at 23:54
2 Answers
Reset to default 5Add 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 remend 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);