Equal distance between Div's using Angular 5 and Flex Layout

断了今生、忘了曾经 提交于 2019-12-25 14:41:11

问题


I am currently using Angular 5 that runs a loop and displays several containers as a list (Thousands of results).

<div class="temp-list">
      <div>
            <ng-container #activityCards *ngFor="let activity of (activityCollection.activities | filters: filtersObject); let i = index;">
                <div class="activity-card-container" *ngIf="checkIsVisible(i)" [ngClass.gt-md]="{'display-inline': true, 'large-activity': i < 3, 'medium-activity': i > 2 && i < 7, 'small-activity': i > 6 }" >
                    <activity-card [activity]="activity"></activity-card>
                </div>
            </ng-container>
      </div>
</div>

You can also see that I am using a Flex Layout ngClass that adds a class to the container based on the index of the element. The first three containers are large(33.33%), the next 4 are medium(25%), and the rest are small(20%). I would like 24px of space between each element, however; I only want that space between containers, not at the left or right ends.



Here is the CSS I currently have.

.temp-list {
    font-size: 0;
}
.activity-card-container {
    position: relative;
    box-sizing: border-box;
}
.display-inline {
    display: inline-block;
    padding: 0 24px 24px 0;
}
.large-activity {
    width: 33.333%;
}
.large-activity:nth-child(3) {
    padding-right: 0;
}
.medium-activity {
    width: 25%;
}
.medium-activity:nth-child(7) {
    padding-right: 0;
}
.small-activity {
    width: 20%;
}
.small-activity:nth-child(5n + 12) {
    padding-right: 0;
}

As you can see I added 24px of padding to the right and bottom of each container. I then select the last element of each row and disregard the padding. The problem with this solution is that the last element is always 24px wider than the previous containers. I need them all to the same width. Any thoughts?


回答1:


Instead of using padding to create a space between items, use Flexbox and space-between, and then, with CSS Calc, subtract the wanted space from the item's width.

The assumed main container should be the .activity-card-container's parent.

Stack snippet

    .temp-list {
        font-size: 0;
    }
    .temp-list > div {                   /* assumed the main parent container  */
        font-size: 0;
        border: 1px solid red;           /* for this demo  */

        display: flex;
        flex-wrap: wrap;
        justify-content: space-between;
    }
    .activity-card-container {
        position: relative;
        box-sizing: border-box;
        background: lightgray;           /* for this demo  */
        height: 70px;                    /* for this demo  */
    }
    .large-activity {
        width: calc(33.333% - 16px);     /* 2 gaps * 24px = 48px / 3 items = 16px */
    }
    .medium-activity {
        margin-top: 24px;
        width: calc(25% - 18px);         /* 3 gaps * 24px = 72px / 4 items = 18px */
    }
    .small-activity {
        margin-top: 24px;
        width: calc(20% - 19.6px);       /* 4 gaps * 24px = 98px / 5 items = 19.6px */
    }
<div class="temp-list">
  <div>
    <div class="activity-card-container large-activity">
      activity-card
    </div>
    <div class="activity-card-container large-activity">
      activity-card
    </div>
    <div class="activity-card-container large-activity">
      activity-card
    </div>

    <div class="activity-card-container medium-activity">
      activity-card
    </div>
    <div class="activity-card-container medium-activity">
      activity-card
    </div>
    <div class="activity-card-container medium-activity">
      activity-card
    </div>
    <div class="activity-card-container medium-activity">
      activity-card
    </div>
    
    <div class="activity-card-container small-activity">
      activity-card
    </div>
    <div class="activity-card-container small-activity">
      activity-card
    </div>
    <div class="activity-card-container small-activity">
      activity-card
    </div>
    <div class="activity-card-container small-activity">
      activity-card
    </div>
    <div class="activity-card-container small-activity">
      activity-card
    </div>
</div>
</div>


来源:https://stackoverflow.com/questions/47537544/equal-distance-between-divs-using-angular-5-and-flex-layout

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!