6

I'm performing an ng-repeat of labels

<span class="label label-primary" style="margin-right:5px;" ng-repeat="a in data.tags">{{a.tagName}}</span>

If the output is many labels, it goes off the screen, instead of wrapping responsively according to the col-md size i have it in.

see example: http://www.bootply.com/119412

What gives?

Thanks!

user2827377
  • 1,381
  • 2
  • 16
  • 26

4 Answers4

10

Changing display:inline to inline-block fixed this.

<span class="label label-primary" style="margin-right:5px; display:inline-block" ng-repeat="a in data.tags">{{a.tagName}}</span>
user2827377
  • 1,381
  • 2
  • 16
  • 26
2

I tried using display: inline-block but it changed the look of the label padding in Bootstrap 3.

enter image description here

So I investigated a little further. The issue is that there is no space between the labels, so there is nothing to wrap on. An easy fix is to enclose the label in another span and put a space before or after the inner span.

<span ng-repeat="a in data.tags"> 
    <span class="label label-primary" style="margin-right:5px;">{{a.tagName}}</span> 
</span>

The whitespace due to formatting in front of the inner space suffices for a wrapping point. If you were to put it all in one line, you'd need to add a conspicuous space.

<!--                             _ conspicuous -->
<span ng-repeat="a in data.tags"> <span class="label label-primary" style="margin-right:5px;">{{a.tagName}}</span></span>

Without the space above, the labels will not wrap.

I also tried various ::after { content: ' '; white-space: normal } strategies. Putting that on the label itself did trigger wrapping, but it cuts off the end of the label that wraps. It still required an outer span to be visually correct. Although at least with a class, it is explicit that the container's purpose is to trigger wrapping:

<style>
.wrappable::after { content: ' ' } /* content: '' wraps too */
</style>
<span class="wrappable" ng-repeat="a in data.tags"><span class="label label-primary" style="margin-right:5px;">{{a.tagName}}</span></span>

Reasons

It's a bit annoying that you have to change the structure of your document to get this obviously expected result. However, I believe this is a consequence of the labels themselves being white-space: no-wrap. (There is good reason for labels to be set this way... otherwise, words inside a label could break across lines.) The browser sees a bunch of no-wrap elements with no breaking space between them. So it makes a logical conclusion that the elements should not be broken up at all. The alternative to make labels render "correctly" is to make labels a multi-element structure (which is what the above is doing) to encapsulate the non-wrappable element in a wrappable one. But this is not ideal because the container element would be redundant in many cases.

Kasey Speakman
  • 4,511
  • 2
  • 32
  • 41
0

The answer worked for me. I went ahead, and added it to my label-spaced class as labels within a repeat doesn't have proper spacing. See my class below:

.label-spaced {
    margin-right:5px;
    margin-bottom:5px !important;
    display:inline-block;
}
Charles Naccio
  • 328
  • 2
  • 6
0

I'm a bit late to the game, but this worked for me (only tested in Firefox):

<span class="label label-default" ng-repeat-start="tag in select.selectedTags">
  {{tag}} <i class="fa fa-times fa-fw cursor-pointer"></i>
</span>
<bwr ng-repeat-end />
Aaron Tyler
  • 161
  • 1
  • 2