Learn How to Do-It-Yourself!

How to Get Rid of Whitespace Gaps between ASP.NET Images & Table Cells in Internet Explorer

Web page design problem with Internet Explorer.

ASP.NET web page with gaps between rows where asp.net Image server controls are used within cells of a html table.
The ASP.NET HTML markup from the web page that has multiple gaps between images. The table row gaps are caused because the ending </div> is not directly next to the closing asp:Image tag. As you can see the </div> tags are on a separate line, which causes the gap to occur when viewing the web page in Internet Explorer.
Web page rendered in Internet Explorer with only one Table Row white space gap.
The ASP.NET HTML markup from the web page that has one gap between table rows. This is occurring because the ending </TD> tag at line 16, is not directly next to the ending <asp:image /> tag on row 15. There cannot be any white space between the tags.
By enclosing an asp.net image control with a html DIV or P tag you can eliminate the table row gap space when viewing the web page in IE.
Example Surf Shop without any white space gaps in between the table rows. Essentially, if you see white spaces (gaps between table rows) in your web page when viewing with Internet Explorer, it probably means you have white space between an image tag and a closing DIV, P or TD tag.

This is one of those simple web page design things that can drive a web developer absolutely crazy.  You create a web page that uses multiple images and place them in a table which is divided into multiple columns and rows. In each cell you place an image using either a standard html image tag, or an asp:Image or asp:HyperLink control in ASP.NET. After completing the html design you test it out in Firefox and Internet Explorer 6.0. Your first test in Firefox renders the page exactly as you’ve designed it. Next you test the appearance of the page in Internet Explorer and find out that it doesn't look like it’s supposed to.  Instead of the images connecting seamlessly, there is a gap between the rows that have images in the table cells.  See this screenshot for a view of a sample problem web page with the table row whitespace gap problem.

After making sure the Table has it’s cellpadding, cellspacing and border all set to 0, you then check to make sure that each cell has it’s valign=”top”, and that there is no height and width settings that you don’t want to have.  You now retest the page and still get a space above the asp:image control in IE.  What in the world could be the problem? 

Well this is exactly what I was asking myself, and racked my brain trying to figure out what exactly was causing the design error in IE.  Low and behold, I found the answer to this problem was pretty darn simple. Unfortunately, I spent a whole lot of time trying to figure out what was causing the problem.  Anyway, here’s a few different ways to help solve this Internet Explorer table cell/row - asp:image white space gap issue.

1. If there are no other html tags inside of a table column cell besides an html image or asp:Image tag make sure the ending html cell TD tag is directly next to the ending image tag. There should be no space between the two tags, and the ending </TD> tag cannot be on a separate line. For instance, the correct html line of code might look like this:

<TD>
<asp:Image ID="Image1" runat="server" ImageUrl="images/header.gif" /></TD>

You don’t want it to look like this:

<TD>
<asp:Image ID="Image1" runat="server" ImageUrl="images/header.gif" />
  </TD>

I’ve also noticed this problem occur when using a regular html <img> tag, when the ending cell tag </TD> is on a separate line. For instance this will also cause the gap to occur:

 <TD>
 <IMG src="images/header.gif" >
   </TD>

Instead the html should should look like:
<TD>
 <IMG src="images/header.gif"></TD>

2.  You can also use an html DIV or P tag to wrap the asp:Image tag, but you must make sure the closing DIV tag is right next to the end of the asp:Image server control tag. There should be no space between the end of the asp:Image tag and the ending DIV tag. See this html markup screenshot. For instance, the html should look like this:

<div><asp:Image ID="Image1" runat="server" ImageUrl="images/header.gif" /></div>

The ending </DIV> or </ P> tag cannot be on a separate line or you will get a visible gap between the table cells when the page is viewed in Internet Explorer. (See Figure 2.) For example, make sure the html does not look like this:

<div><asp:Image ID="Image1" runat="server" ImageUrl="images/header.gif" />
   </div>


Hopefully, these examples save you some valuable design time.

Keep in mind, I have yet to test this image cell/row div gap issue out in Internet Explorer 7.0, so Microsoft may or may not have changed how IE 7 renders a web page when there is white space between an image tag and a closing tag.


_______________________________________
Save this  Email this  |  Digg this!  |  del.icio.us  |  RSS 

9 Comments

Thanks by Mike on 9/12/2006
I done pulled my hair out. I can't beleive your fix is the only one I found. After about 3 or 4 Google's and finally using about 10 keywords found your article on page 3 of the Google results.

You would think this would be all over the place.

But hey thanks
Glad this helped by kennard on 9/12/2006
Hey Mike, glad this article was of use to you. I agree with you, and find it kinda odd that there is very little info out there on this problem. I know I wasn't able to find any results talking about the ie cell issue before writing this article.
OH MY GOD!!!!!!!!!! by Matt on 9/26/2006
6 hours of stripping out code until I had only one table left and this is the problem that has been causing so much grief. Thanks for the solution really appreciate it.
I feel your pain by kennard on 9/26/2006
Matt, I feel your pain. Before figuring out the solution to this problem I probably spent at least that much time screwing around with html code to get things to work right. I think I even scrapped a few layouts too!
Bum by Chris Price on 9/28/2006
Well i knew how to fix this, however i am using XSL to translate XML to HTML, the MSXML transfrom method is reformatting the HTML and put the white space in.... help :(
Happy!!!!!! by Dave on 12/12/2006
Like the others I was totaly stumped and scrapped a few layouts of my own.
Was tempted to put a disclaimer on the page saying IE sucked and it would only work with firefox.
Thanks again.
Half way there, Thx! by SD on 1/17/2007
Like others, I spent a lot of time on this. Your article helped me halfway. I still can't solve this problem in code behind. Any more insight would be much appreciated...

I am having problems with GridViews. 1 solved, 1 Not Solved...
I am creating a GridView in aspx with a <ItemTemplate><asp:Image> field, I removed the spaces in the aspx file and worked like a charm. I create another GridView dynamically in code behind like gvGridView.Columns.Add(AImageField), the space is still there between the images. I tried the padding and height properties but no luck.

Any Ideas? Thx!
Hmmm by kennard on 1/17/2007
Hey SD, I'm really not sure how to solve your problem, but you might want to try getting rid of the <asp:ImageField and instead dynamically generate a <asp:TemplateColumn with the <asp:Image inside it.
Gr8 by Sanjeev on 2/3/2007
I found it very useful. Thanks a lot. I used to put </td> always at the next line to make page readable but that sucked me for 4 hours ;-(
Thanks !

Add your comment

by Anonymous - Already have an account? Login now!
Your Name:  

Title:  

Comment:  
Join now for free to keep track of all your comments and have them appear instantly.

Sponsored Links

Search for keywords: