Solved

stretch table to width of screen/image

Posted on 2007-03-28
6
543 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
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.

 
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

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

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

Browsers only know CSS so your awesome SASS code needs to be translated into normal CSS. Here I'll try to explain what you should aim for in order to take full advantage of SASS.
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…
In this tutorial viewers will learn how add a scalable full-width header using CSS3. Create a new HTML document with an internal stylesheet. Set a tiled background.:  Create a new div and name it Header. Position it with position:absolute at the top…
In this Micro Tutorial viewers will learn how to create navigation buttons that change on rollover, using CSS (Continuation of the CSS Image Sprite tutorial) Create a parent ID for all the list items       - Specify position: absolute and display: block…

776 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