Solved

stretch table to width of screen/image

Posted on 2007-03-28
6
550 Views
Last Modified: 2013-12-24
Ok, I'm trying to do this but just can't figure out how.  I need some experts to help.  On my webpage, I'm displaying a background image that repeats (repeat-x) across the browser.  Now I have a table that I want to stretch 100% to the width of the screen.  But I need to place an image in that cell but because it's an image I don't want to stretch it.  How can I 1.) detect the screen resolution and 2.) stretch the table to that width but not pixelate that image?  Any ideas experts?
0
Comment
Question by:COwebmaster
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 10

Expert Comment

by:Jaax
ID: 18813869
Have your table named as "tbl1"

<table id="tbl1">
....
</table>

In your body tag

<body onload="setTableSize()">

</body>

In your script

<script>
...

      function setTableSize(){
        tbl = document.getElementById("tbl1");
        tbl.style.width = screen.width;
        tbl.style.height = screen.height;
      }

..
</script>
0
 
LVL 6

Expert Comment

by:sankar_ravi
ID: 18814166
Hi,

If you place an image in a cell, that doesn't stretch to the table width. By default if you say width="100%" and height="100%", table will stretch to the screen resolution. Here there is no need to calculate the screen resolution.

If you still need calculations....psb.
<html>
<head>
      <title>Untitled</title>
      <script language="JavaScript">
            var w,h;
            w=screen.width;
            h=screen.height;
            alert(w+"--"+h);
            function check(){
                  var obj=document.getElementById("tb1");
                  alert(obj.width+"---"+obj.height);
                  obj.width=w;
                  obj.height=h;
                  alert(obj.width+"---"+obj.height);
            }
      </script>
</head>

<body onload="check();" bottommargin="0" topmargin="0" leftmargin="0" rightmargin="0">
<table id="tb1" border="1" cellpadding="0" cellspacing="0">
<tr>
<td><img src="images/amitabh1.gif" alt="" border="0"> </td><td>&nbsp;</td>
</tr>
</table>

</body>
</html>
0
 

Author Comment

by:COwebmaster
ID: 18817380
Ok, but my problem is is that my image is 800px wide.  If the user has a width of 1024, then the image needs to swap out with an image that is 1024px wide.  how can I toggle between the various image widths?
0
Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

 
LVL 25

Expert Comment

by:dgrafx
ID: 18831922
so you want the table to be the width of users screen and the hieght of users screen - is that correct?
If yes then as stated above - just use
<table style="width:100%;height:100%;">

Then to get the correct image you'll need a set of images that are named what their size (width) is.
for example you'd have 800.jpg and 1024.jpg and 1280.jpg et cetera.

Also give the table an id and that should be it:
<table style="width:100%;height:100%;" id="tb1">
<tr><td>hello</td></tr>
</table>
<script>
document.getElementById('tb1').style.backgroundImage='url(/images/'+screen.availWidth+'.jpg)';
document.getElementById('tb1').style.backgroundRepeat='no-repeat';
document.getElementById('tb1').style.backgroundPosition='center center';
</script>

good luck ...
0
 
LVL 6

Expert Comment

by:sankar_ravi
ID: 18842035
Hi,

In order to solve the problem of swapping the images...
<html>
<head>
      <title>Untitled</title>
      <script language="JavaScript">
            var w,h;
            w=screen.width;
            h=screen.height;
            alert(w+"--"+h);
            function check(){
                  var obj=document.getElementById("tb1");
                  alert(obj.width+"---"+obj.height);
                  obj.width=w;
                  obj.height=h;
                  alert(obj.width+"---"+obj.height);
                  if(w==800)
                       document.images["celImage"].src="800image.jpg";


            }
      </script>
</head>

<body onload="check();" bottommargin="0" topmargin="0" leftmargin="0" rightmargin="0">
<table id="tb1" border="1" cellpadding="0" cellspacing="0">
<tr>
<td><img name="celImage"  src="images/amitabh1.gif" alt="" border="0"> </td><td>&nbsp;</td>
</tr>
</table>

</body>
</html>
0
 
LVL 6

Accepted Solution

by:
sankar_ravi earned 500 total points
ID: 18842038
sorry for incomplete posting...

  if(w==800)
                       document.images["celImage"].src="800image.jpg";
else if(w==1024)
          document.images["celImage"].src="1024image.jpg";
etc...
Based on the resolution, you can assign an image.

If it's not clear, please let me know,

Thanks
0

Featured Post

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

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

This article discusses four methods for overlaying images in a container on a web page
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
In this tutorial viewers will learn how to style a corner ribbon overlay for an image using CSS Create a new class by typing ".Ribbon":  Define the class' "display:" as "inline-block": Define its "position:" as "relative": Define its "overflow:" as …
In this tutorial viewers will learn how to style a decorative dropcap for the first letter in a paragraph using CSS. In CSS, create a new paragraph class by typing "p.fancy": Then, to style only the first letter of the first sentence, include the ps…
Suggested Courses

615 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