Forum Moderators: open

Message Too Old, No Replies

new line in html code causes a space between images

         

sneaks

8:09 pm on Dec 31, 2006 (gmt 0)

10+ Year Member



is there a way to make a new line in the code that doesnt create a space in the layout? right now i have 6 30px x 30px images that i want on top of each other in a container div. because i am going to add javascript behaviours to the buttons i want each image/link on its own line in the code for organization sake but this causes visual issues as a space is now between each image.

this is something that has annoyed me for sometime and would like to know for future.

thanks & happy new years!
jonathan


<div id="galleryNav" style="width:30px; height: 180px; margin: 10px 0px 0px 0px; float: left;">
<a href="#"><img src="nav/gallery_1_1.gif" name="roGallery1" border="0" id="roGallery1" /></a>
<a href="#"><img src="nav/gallery_2_0.gif" name="roGallery3" border="0" id="roGallery3" /></a>
<a href="#"><img src="nav/gallery_3_0.gif" name="roGallery3" border="0" id="roGallery3" /></a>
<a href="#"><img src="nav/gallery_4_0.gif" name="roGallery4" border="0" id="roGallery4" /></a>
<a href="#"><img src="nav/gallery_5_0.gif" name="roGallery5" border="0" id="roGallery5" /></a>
<a href="#"><img src="nav/gallery_6_0.gif" name="roGallery6" border="0" id="roGallery6" /></a>
</div>

sneaks

8:12 pm on Dec 31, 2006 (gmt 0)

10+ Year Member



another thing, if i wrap each anchor in a <div></div> then it works fine... or if i add a style display:block to the anchor, then everything displays the way it should...

regards
jd

cmarshall

9:25 pm on Dec 31, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You need the <div>. Even if you tweak the CSS, different browsers will react differently. I'd strongly suggest using a <div> anyway, as it gives you a great deal more control.

tedster

9:38 pm on Dec 31, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



if i add a style display:block to the anchor, then everything displays the way it should...

It's a common issue in the way standards mode works, compared to quirks mode. See [webmasterworld.com...] for more.

The deal here is that an <img> element is an inline element by default. That means, in standards mode, that the images are aligned to the text baseline. This leaves space below the baselie for the descenders of any text -- hence the gap that you see.

pinterface

1:19 am on Jan 1, 2007 (gmt 0)

10+ Year Member



Though I hate looking at this style of formatting, it does avoid the whitespace problem:
<div id="galleryNav" style="width:30px; height: 180px; margin: 10px 0px 0px 0px; float: left;"
><a href="#"><img src="nav/gallery_1_1.gif" name="roGallery1" border="0" id="roGallery1" /></a
><a href="#"><img src="nav/gallery_2_0.gif" name="roGallery3" border="0" id="roGallery3" /></a
><a href="#"><img src="nav/gallery_3_0.gif" name="roGallery3" border="0" id="roGallery3" /></a
><a href="#"><img src="nav/gallery_4_0.gif" name="roGallery4" border="0" id="roGallery4" /></a
><a href="#"><img src="nav/gallery_5_0.gif" name="roGallery5" border="0" id="roGallery5" /></a
><a href="#"><img src="nav/gallery_6_0.gif" name="roGallery6" border="0" id="roGallery6" /></a
></div>

penders

4:46 pm on Jan 1, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



is there a way to make a new line in the code that doesnt create a space in the layout?

New-lines / spaces / tabs will always collapse to a single space in the layout, although as tedster points out, this is not really the problem in this instance... you are getting an additional horizontal gap beneath each image.

However, in IE6 those new-lines do 'appear' to be the problem, as pinterface points out, you can remove the new-lines to sort the problem (in IE6) - but this won't work in FF, Opera... (Strict DOCTYPE) (In some ways this (IE6) appears logical to me... the text-node is being removed and therefore there should be no text baseline?!)

You can also sort the problem in FF and Opera by using a Transitional DOCTYPE, and still keep the new-lines in the code.

As illustrated here...
[developer.mozilla.org...]

To illustrate tedsters point you could set:

<img style="vertical-align:bottom;"...

to alter the images default of aligning to the baseline in strict mode. This will work in IE6, FF and Opera.

Experiment... To minimise the effect of the image sitting on the baseline, you could set

font-size:1px;
on your galleryNav - this reduces the space taken up by the (non-existent) 'descenders' and therefore reduces the space between your images. This is enough to sort the problem in IE6, but in FF and Opera there is still a slight gap.

another thing, if i wrap each anchor in a <div></div> then it works fine... or if i add a style display:block to the anchor, then everything displays the way it should...

Only in IE6 (not sure about IE7...?) - in FF and Opera this has no effect in Strict mode. You need to set

display:block
on the img tag for it to work cross-browser.

(I've just realised that most of these points are mentioned in tedsters link also, but hey :)