15

I am having a problem with multiple ng-repeat elements when showing div elements as inline-block. There is a strange gap between elements where one ng-repeat ends and the next one starts, as illustrated by the image:

gap when using ng-repeat

I have created a plunk to demonstrate this behavior:

Why is this happening and how to remove the gap?

4 Answers 4

17

Check this plunk

There is a hack to remove space between inline elements that appears at line-break.

<div class="red" ng-controller="TestCtrl">
  <div class="blue number" ng-repeat="number in array1 track by $index">{{number}}</div><!--
  --><div class="green number" ng-repeat="number in array2 track by $index">{{number}}</div><!--
  --><div class="teal number" ng-repeat="number in array3 track by $index">{{number}}</div>
</div>

You could read more about other hacks here.

Sign up to request clarification or add additional context in comments.

1 Comment

oh thank you so much... i thought i was going to have to trash the whole section because of a few pixels of gap.
4

That is because of the way the .number class in your css is defined. If you change it so that it contains a float: left, you'll get rid of the extra gap.

.number {
  width: 20px;
  height: 20px;
  display: inline-block;
  color: white;
  text-align: center;
  float: left;
}

You can see my updated plunkr here

Comments

4

I know it's quite weird, but the whitespaces (crlf, spaces...) between your inline block divs make the space.

If you remove them, the space goes off. (plunker demo)

I couldn't find any reference for explanation but my guess is that since the characters are between inline blocks, it's interpreted as a space as if block elements were actually inline ones (e.g. spans).

  <body ng-app="testRepeat">
    <div class="red" ng-controller="TestCtrl">
      <div class="blue number" ng-repeat="number in array1 track by $index">{{number}}</div><div class="green number" ng-repeat="number in array2 track by $index">{{number}}</div><div class="teal number" ng-repeat="number in array3 track by $index">{{number}}</div>
    </div>
  </body>

Comments

2

This is caused by the whitespace between the <div> elements. There are various ways to address this like the other answers have suggested. My preferred layout is:

  <div class="blue number" ng-repeat="number in array1 track by $index">{{number}}</div
  ><div class="green number" ng-repeat="number in array2 track by $index">{{number}}</div
  ><div class="teal number" ng-repeat="number in array3 track by $index">{{number}}</div>

But the other suggestions are valid.

Another way to handle this without worrying about the HTML is to set the font size of the container to 0 so that the white space doesn't appear.

http://plnkr.co/edit/wXKl61lyiqdONbChYI9o?p=preview

The downside to this approach is that you can't use percentage sizes for the contained element fonts and you have to set them explicitly.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.