Solved

Percent height of a DIV with an image inside, Mozilla/Netscape issue

Posted on 2004-03-26
11
402 Views
Last Modified: 2012-05-04
I'm trying to create a DIV with an image inside it that fills 70%x70% of the browser window and is scrollable.  Here's a dumbed down version of what I have so far:

<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/
DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<style type="text/css">
body {
        margin: 0;
        padding: 0;
        height: 100%;
        width:100%
</style>
</head>
<body>
<div id="theimage" style="
        margin-left: auto;
        margin-right: auto;
        width: 70%;
        height: 70%;
        overflow: auto;
        border: 2px solid black;
">
<img src="bigimage.jpg">
</div>
</body>
</html>

Everything looks great in Internet Explorer but in Firefox/Mozilla/Netscape, the DIV's height always ends up being the same as the image itself.  If I set the height to a value in pixels then the image scrolls vertically just like it should.

Is there some hack to get this working in the non-IE browsers?

Thanks a lot,
J
0
Comment
Question by:jkeebler
  • 4
  • 2
  • 2
  • +1
11 Comments
 
LVL 17

Expert Comment

by:dorward
ID: 10694426
html { width: 100%; height: 100%; margin: 0; padding: 0; } might do the job.
0
 

Author Comment

by:jkeebler
ID: 10694878
Didn't seem to do anything.  Thanks for the attempt, dorward.
0
 
LVL 53

Accepted Solution

by:
COBOLdinosaur earned 125 total points
ID: 10695298
Height on body, html, and even on documentElement are not valid or logical.  100% implies 100 % of content, but content and screen height are unrelated.  Mozilla based browser are doing the rendering correctly.  Ie is not because it is non-standard, and encourages bad coding that does not work in any other browser.

If what you want is 70% of screen height, then you have to set it dynamically at load time.
That means using Javascript.  Users with JavaScript disabled will see the fill height div.

<body onload="document.getElementById('theimage').style.height=self.screen.availHeight*.7">

or you can leave it the way it is and support IE only with it until a later orelease of IE does it to standanrd and breaks you pages, like IE6 broke a lot IE5.5 only "enhanced" page, and IE 5.5 broke IE 5 pages.

Cd&
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:jkeebler
ID: 10695387
Adding height: 100% to the body style was the only solution I found out there on Google.  I'll try that JS and get back to you.

Is this a more correct way to do what I want to do then?  I tried iframes but the browser resizes the content to fit the frame.

0
 

Author Comment

by:jkeebler
ID: 10695842
Thanks for the attempt.  I just had to change 4 things in your code to get it working:
1) it has to come after the DIV
2) instead of availHeight, I had to use innerHeight to get the window height, not the screen
3) it needed to explicitly say that the value we're setting is in pixels.  
4) had to call itself again when the browser is resized.

Here's what seems to work under Firefox and Konqueror browsers.  I haven't tested under IE yet.  I placed it just after the </div> tag.

<script>
<!--//
function resizeDiv() {
        document.getElementById("theimage").style.height = window.innerHeight*0.7 + "px";
}
window.onresize = resizeDiv;
resizeDiv();
//-->
</script>

Boy I hate all these issues with XHTML between the browsers.  I thought it was bag in HTML <4 but this is much worse.

Any
0
 
LVL 53

Expert Comment

by:COBOLdinosaur
ID: 10696097
window.innerHeight won't work in IE.

For IE use document.body.clientHeight
IE in strict mode (caused by the doctype) will not work with either, it will require:

document.documentElement.clientHeight

Cd&
0
 
LVL 17

Expert Comment

by:dorward
ID: 10696148
Why are you using XHTML anyway?

<script>
<!--
This is actually commented out if you are using XHTML!
-->
</script>

http://hixie.ch/advocacy/xhtml
0
 

Author Comment

by:jkeebler
ID: 10696294
I'm trying to be all XHTML compliant, etc.   Who knows, someone may want to view my website on a PDA or something :-)

I'll add in that IE code COBOL.  Have any links to IE emulators for Linux so I don't have to switch to Window$ to test this?

Thanks.
0
 
LVL 10

Expert Comment

by:Havin_it
ID: 10703146
J, It's a long shot i know, but have you tried WINE?
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Help in good tutorials for PHP, HTML and CSS 6 40
Insert Button on a table 16 37
really easy css change help please 4 18
Wordpress Hacked 1 26
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
Not sure what the best email signature size is? Are you worried about email signature image size? Follow this best practice guide.
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

860 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question