Solved

stretch table to width of screen/image

Posted on 2007-03-28
6
542 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
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
Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

CSS is a visual language used to classify objects and define rules about how they should be displayed. CSS skills aren’t restricted to developers anymore, there is a big benefit to having a basic understanding of the language, regardless of your occ…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
In this tutorial viewers will learn how to style rounded corners for elements in CSS using the border-radius property Begin with a normal styled element such as a div: To style all four corners of the div to be the same degree of roundness, use the …
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

910 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now