1

I have a fixed width div, containing a bunch of links, each one has text with non breaking spaces between the words.

What I am trying to achieve, is that when a link hits the edge of the div the whole link moves down a line.

I was just playing about in JSFiddle,and initially I had one long unformatted (no tags on new lines, etc) piece of html, and was observing the final link not moving onto a new line.

I then decided to reformat, so put new tags on lines, and after doing so, the final tag now moves onto a new line when it gets restricted by the width of the div.

I am actually generating the html dynamically from a script, and at present it just produces a single concatenated string for me to then render. Here is the HTML I am using and a working fiddle showing the results of both approaches:

http://jsfiddle.net/8gdez8ur/2/

<div style="width: 350px; overflow: hidden;"><a href="#" action="zoom" style="font-size:11px;padding-right:2px;">Zoom</a><a action="streetview" href="#" style="font-size:11px;padding-right:2px;"> Street&nbsp;View</a><a action="addplacemarker" href="#" style="font-size:11px;padding-right:2px;">Add&nbsp;Placemarker</a><a action="route" href="#" style="font-size:11px;padding-right:2px;">ETA&nbsp;To</a><a action="reportproblem" href="#" style="font-size:11px;padding-right:2px;">Incorrect&nbsp;Location&nbsp;Details</a><a action="sendmessage" href="#" style="font-size:11px;">Send&nbsp;Message</a></div>
<hr>
<div style="width: 350px; overflow: hidden;">
<a href="#" action="zoom" style="font-size:11px; padding-right:2px;">Zoom</a>
<a action="streetview" href="#" style="font-size:11px; padding-right:2px;">Street&nbsp;View</a>
<a action="addplacemarker" href="#" style="font-size:11px; padding-right:2px;">Add&nbsp;Placemarker</a>
<a action="route" href="#" style="font-size:11px; padding-right:2px;">ETA&nbsp;To</a>
<a action="reportproblem" href="#" style="font-size:11px; padding-right:2px;">Incorrect&nbsp;Location&nbsp;Details</a>
<a action="sendmessage" href="#" style="font-size:11px; padding-right:2px;">Send&nbsp;Message</a>
</div>

Why is there a difference in behaviour here? Thanks

mindparse
  • 6,115
  • 27
  • 90
  • 191

4 Answers4

3

I believe the reason this is happening is because the lack of white space between your inline elements which causes it to render effectively as one long string. Try setting your anchor tags to be inline-block.

a{
    display: inline-block;
}

Here is the fiddle

tribe84
  • 5,432
  • 5
  • 28
  • 30
1

Seems to be related to New line between anchors in HTML source creates empty space in browser

However to fix it for what I believe the OP wanted, is to add whitespace after each anchor, eg add &nbsp; if possible.

Community
  • 1
  • 1
Morvael
  • 3,478
  • 3
  • 36
  • 53
1

Line breaks in your code (second example) generate whitespace, thus it's rendered the way you want it. The first example would be in one line, but there is a leading space in your second link.

By adding

div a { display: inline-block; }

to your first example, you can get the effect you want.

Paul
  • 8,974
  • 3
  • 28
  • 48
1

Although using inline-block is a way of fixing the problem, the source of the issue in your original post is in the following snippet of code:

<a action="streetview" href="#" style="font-size:11px;padding-right:2px;"> Street&nbsp;View</a>

If you look carefully, you have a single space between ">" and "Street", which is enough to cause the line break after the first a element.

If you remove the space, then the line does indeed stay on one line and is clipped because of the overflow: hidden.

div {
    width: 350px;
    overflow: hidden;
    border: 1px dotted blue;   
}
div a {
    font-size: 11px;
    padding-right: 2px;
}
<div><a href="#" action="zoom" >Zoom</a><a action="streetview" href="#" > Street&nbsp;View</a><a action="addplacemarker" href="#" >Add&nbsp;Placemarker</a><a action="route" href="#" >ETA&nbsp;To</a><a action="reportproblem" href="#" >Incorrect&nbsp;Location&nbsp;Details</a><a action="sendmessage" href="#">Send&nbsp;Message</a></div>
<hr>
<div><a href="#" action="zoom" >Zoom</a><a action="streetview" href="#" >Street&nbsp;View</a><a action="addplacemarker" href="#" >Add&nbsp;Placemarker</a><a action="route" href="#" >ETA&nbsp;To</a><a action="reportproblem" href="#" >Incorrect&nbsp;Location&nbsp;Details</a><a action="sendmessage" href="#">Send&nbsp;Message</a></div>
<hr>
<div>
<a href="#" action="zoom" >Zoom</a>
<a action="streetview" href="#" >Street&nbsp;View</a>
<a action="addplacemarker" href="#" >Add&nbsp;Placemarker</a>
<a action="route" href="#" >ETA&nbsp;To</a>
<a action="reportproblem" href="#" >Incorrect&nbsp;Location&nbsp;Details</a>
<a action="sendmessage" href="#" >Send&nbsp;Message</a>
</div>
Marc Audet
  • 46,011
  • 11
  • 63
  • 83