Reputation: 5986
Is there any way to do this? I don't want the content to go below the screen without it being scrollable. (This is for people who may have smaller screens than most)
Upvotes: 0
Views: 800
Reputation: 7011
NB: Levi Putna is right in pointing out that this behaviour only occurs if the
<div>
is empty.
Two options:
min-height
and min-width
the <body>
.height
on the <body>
using JavaScript when the window is resized.Setting min-height
and min-width
:
For example, if the absolutely positioned <div>
is 400 × 300 pixels:
body {
min-height: 400px;
min-width: 300px;
}
Limitations:
min-height
works in practically every other browser out there.<div>
, expect by overcompensating.Dynamically setting height
(when window is resized):
Given an absolutely positioned <div>
of 400 × 300 pixels, positioned at [0,0]:
var $win = $(window);
$win.resize(function(){
var height = Math.max(400, $win.height());
var width = Math.max(300, $win.width());
$('body').css({
height: height,
width: width
});
});
Or, if the <div>
's dimensions or position are variable:
var $win = $(window);
var $div = $('#thediv'); // the absolutely positioned div
$win.resize(function(){
var offset = $div.offset();
var height = $div.outerHeight() + offset.top;
var width = $div.outerWidth() + offset.left;
height = Math.max(height, $win.height());
width = Math.max(width, $win.width());
$('body').css({
height: height,
width: width
});
});
By using outerHeight
and outerWidth
, padding and border are included. By using offset
(instead of the CSS top
and left
, the position is taken relative to the document, not to the offset parent.
Upvotes: 2
Reputation: 3073
From what I understand this is what you are doing:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<div id="scroll-div" style="position:absolute; height:2000px;"></div>
</body>
</html>
and expecting to get a scrollbar? From what I understand the browser won’t render the div or at least the scroll bar until you add some content.
If you simply add some content to the div (scroll-div) you will get the scroll bar.
Upvotes: 3
Reputation: 1165
Use $(window).resize(function(){...});
if you are using jquery or simply <body onresize="myResize()" >
for native javascript.
Upvotes: 1