Solved

stretch table to width of screen/image

Posted on 2007-03-28
6
539 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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

When it comes to showing a 404 error page to your visitors, you do not want that generic page to show, and you especially do not want your hosting provider’s ad error page to show either. In this article, I will show you how to enable the custom 40…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
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 tutorial viewers will learn how to customize the background color and font color of highlighted text using the ::selection element in CSS Begin by defining the selected text as an element in CSS by typing "::selection": Style the ::selection…

744 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

10 Experts available now in Live!

Get 1:1 Help Now