[英]CSS Vertically Aligning Generated Plain-text
Im trying to line up some plain text generated by an ng-for in Angular and I cant seem to get it to work. 我试图在Angular中排列由ng-for生成的一些纯文本,我似乎无法使其工作。
I'm currently getting the result: 我目前正在得到结果:
E | E |
A | A |
D | D |
G | G |
B | B |
E | E |
---7-- --- 7--
21----- 21 -----
----20- ---- 20-
-----18 ----- 18
-5---- -5 ----
0----- 0 -----
-19---- -19 ----
----16- ---- 16
----22- ---- 22-
-----10 ----- 10
--9--- --9 ---
-----5 ----- 5
---14-- --- 14--
----21- ---- 21-
---13-- --- 13--
--4--- --4 ---
-19---- -19 ----
13----- 13 -----
---18-- --- 18--
-----0 ----- 0
--15--- --15 ---
---17-- --- 17--
---4-- --- 4--
----1- ---- 1
11----- 11 -----
--24--- --24 ---
---2-- --- 2--
10----- 10 -----
-21---- -21 ----
23----- 23 -----
And I want the result to look like 我希望结果看起来像
E | E | -21
-21
A | A | --
-
D | D | --
-
G | G | 7-
7-
B | B | --
-
E | E | --
-
So vertically aligned for all rows. 因此垂直对齐所有行。
I've been trying flex but I cant seem to figure out how to line things up. 我一直在尝试flex,但我似乎无法弄清楚如何排队。
Please help! 请帮忙!
.container-1{ display:flex; flex-direction: column; } .box-1{ flex:1 } .container-2{ display:flex; flex-direction: row; }
<div class = "container-1"> <div class="box-1"> <div class="box-1">E |</div> <div class="box-1">A |</div> <div class="box-1">D |</div> <div class="box-1">G |</div> <div class="box-1">B |</div> <div class="box-1">E |</div> </div> <div class = "container-2" *ngFor="let currentTab of tabs;"> <div *ngIf="currentTab.string == 0; then thenBlock else elseBlock"></div> <ng-template class="box-1" #thenBlock>{{currentTab.tab}}</ng-template> <ng-template class="box-1" #elseBlock>-</ng-template> <div *ngIf="currentTab.string == 1; then thenBlock else elseBlock"></div> <ng-template class="box-1" #thenBlock>{{currentTab.tab}}</ng-template> <ng-template class="box-1" #elseBlock>-</ng-template> <div *ngIf="currentTab.string == 2; then thenBlock else elseBlock"></div> <ng-template class="box-1" #thenBlock>{{currentTab.tab}}</ng-template> <ng-template class="box-1" #elseBlock>-</ng-template> <div *ngIf="currentTab.string == 3; then thenBlock else elseBlock"></div> <ng-template class="box-1" #thenBlock>{{currentTab.tab}}</ng-template> <ng-template class="box-1" #elseBlock>-</ng-template> <div *ngIf="currentTab.string == 4; then thenBlock else elseBlock"></div> <ng-template class="box-1" #thenBlock>{{currentTab.tab}}</ng-template> <ng-template class="box-1" #elseBlock>-</ng-template> <div *ngIf="currentTab.string == 5; then thenBlock else elseBlock"></div> <ng-template class="box-1" #thenBlock>{{currentTab.tab}}</ng-template> <ng-template class="box-1" #elseBlock>-</ng-template> </div> </div>
use the [align-items][1]
property of your flex
container and set it to center
. 使用
flex
容器的[align-items][1]
属性并将其设置为center
。
.container-1 {
...
align-items: center;
}
.container-1{ display:flex; flex-direction: column; align-items: center; } .box-1{ flex:1; } .container-2{ display:flex; flex-direction: row; }
<div class = "container-1"> <div class="box-1"> <div class="box-1">E |</div> <div class="box-1">A |</div> <div class="box-1">D |</div> <div class="box-1">G |</div> <div class="box-1">B |</div> <div class="box-1">E |</div> </div> <div class = "container-2" *ngFor="let currentTab of tabs;"> <div *ngIf="currentTab.string == 0; then thenBlock else elseBlock"></div> <ng-template class="box-1" #thenBlock>{{currentTab.tab}}</ng-template> <ng-template class="box-1" #elseBlock>-</ng-template> <div *ngIf="currentTab.string == 1; then thenBlock else elseBlock"></div> <ng-template class="box-1" #thenBlock>{{currentTab.tab}}</ng-template> <ng-template class="box-1" #elseBlock>-</ng-template> <div *ngIf="currentTab.string == 2; then thenBlock else elseBlock"></div> <ng-template class="box-1" #thenBlock>{{currentTab.tab}}</ng-template> <ng-template class="box-1" #elseBlock>-</ng-template> <div *ngIf="currentTab.string == 3; then thenBlock else elseBlock"></div> <ng-template class="box-1" #thenBlock>{{currentTab.tab}}</ng-template> <ng-template class="box-1" #elseBlock>-</ng-template> <div *ngIf="currentTab.string == 4; then thenBlock else elseBlock"></div> <ng-template class="box-1" #thenBlock>{{currentTab.tab}}</ng-template> <ng-template class="box-1" #elseBlock>-</ng-template> <div *ngIf="currentTab.string == 5; then thenBlock else elseBlock"></div> <ng-template class="box-1" #thenBlock>{{currentTab.tab}}</ng-template> <ng-template class="box-1" #elseBlock>-</ng-template> </div> </div>
I'm not a hundred percent sure on what you want to achieve exactly. 我不是百分百肯定你想要达到的目标。
I created borders around each div, so you can see the result. 我在每个div周围创建了边框,因此您可以看到结果。 Please elaborate further on what it should do.
请进一步详细说明应该做些什么。
.main { display: flex; } .main div { border: 1px solid rgba(0, 0, 0, .2); } .container-2 { flex: 1; }
<div class="main"> <div class="container-1"> <div class="box-1">E |</div> <div class="box-1">A |</div> <div class="box-1">D |</div> <div class="box-1">G |</div> <div class="box-1">B |</div> <div class="box-1">E |</div> </div> <div class = "container-2" *ngFor="let currentTab of tabs;"> <div *ngIf="currentTab.string == 0; then thenBlock else elseBlock"></div> <ng-template class="box-1" #thenBlock>{{currentTab.tab}}</ng-template> <ng-template class="box-1" #elseBlock>-</ng-template> <div *ngIf="currentTab.string == 1; then thenBlock else elseBlock"></div> <ng-template class="box-1" #thenBlock>{{currentTab.tab}}</ng-template> <ng-template class="box-1" #elseBlock>-</ng-template> <div *ngIf="currentTab.string == 2; then thenBlock else elseBlock"></div> <ng-template class="box-1" #thenBlock>{{currentTab.tab}}</ng-template> <ng-template class="box-1" #elseBlock>-</ng-template> <div *ngIf="currentTab.string == 3; then thenBlock else elseBlock"></div> <ng-template class="box-1" #thenBlock>{{currentTab.tab}}</ng-template> <ng-template class="box-1" #elseBlock>-</ng-template> <div *ngIf="currentTab.string == 4; then thenBlock else elseBlock"></div> <ng-template class="box-1" #thenBlock>{{currentTab.tab}}</ng-template> <ng-template class="box-1" #elseBlock>-</ng-template> <div *ngIf="currentTab.string == 5; then thenBlock else elseBlock"></div> <ng-template class="box-1" #thenBlock>{{currentTab.tab}}</ng-template> <ng-template class="box-1" #elseBlock>-</ng-template> </div> </div>
I have updated your code for the desired result. 我已更新您的代码以获得所需的结果。 It still has room for lot of improvement.
它仍有很大的改进空间。
.container-1 { display:flex; flex-direction: row; } .box-1 { display: flex; flex-direction: column; width: 4%; } .container-2 { display: flex; flex-direction: column; width: 2%; text-align: center; }
<div class = "container-1"> <div class="box-1"> <div>A |</div> <div>D |</div> <div>G |</div> <div>B |</div> <div>E |</div> <div>E |</div> </div> <div class = "container-2" *ngFor="let currentTab of this.tabs;"> <div *ngIf="currentTab.string == 0; then thenBlock else elseBlock"></div> <ng-template class="box-1" #thenBlock><div>{{currentTab.tab}}</div></ng-template> <ng-template class="box-1" #elseBlock><div>-</div></ng-template> <div *ngIf="currentTab.string == 1; then thenBlock else elseBlock"></div> <ng-template class="box-1" #thenBlock><div>{{currentTab.tab}}</div></ng-template> <ng-template class="box-1" #elseBlock><div>-</div></ng-template> <div *ngIf="currentTab.string == 2; then thenBlock else elseBlock"></div> <ng-template class="box-1" #thenBlock><div>{{currentTab.tab}}</div></ng-template> <ng-template class="box-1" #elseBlock><div>-</div></ng-template> <div *ngIf="currentTab.string == 3; then thenBlock else elseBlock"></div> <ng-template class="box-1" #thenBlock><div>{{currentTab.tab}}</div></ng-template> <ng-template class="box-1" #elseBlock><div>-</div></ng-template> <div *ngIf="currentTab.string == 4; then thenBlock else elseBlock"></div> <ng-template class="box-1" #thenBlock><div>{{currentTab.tab}}</div></ng-template> <ng-template class="box-1" #elseBlock><div>-</div></ng-template> <div *ngIf="currentTab.string == 5; then thenBlock else elseBlock"></div> <ng-template class="box-1" #thenBlock><div>{{currentTab.tab}}</div></ng-template> <ng-template class="box-1" #elseBlock><div>-</div></ng-template> </div> </div>
I hope it'll help you out. 我希望它会帮助你。 Thanks
谢谢
.container-wrapper { display: flex; } .box-1{ white-space: nowrap; }
<div class="container-wrapper"> <div class="container"> <div class="box-1">E |</div> <div class="box-1">A |</div> <div class="box-1">D |</div> <div class="box-1">G |</div> <div class="box-1">B |</div> <div class="box-1">E |</div> </div> <div class="container" *ngFor="let currentTab of tabs;"> <div *ngIf="currentTab.string == 0; then thenBlock else elseBlock"></div> <ng-template class="box-1" #thenBlock>{{currentTab.tab}}</ng-template> <ng-template class="box-1" #elseBlock>-</ng-template> <div *ngIf="currentTab.string == 1; then thenBlock else elseBlock"></div> <ng-template class="box-1" #thenBlock>{{currentTab.tab}}</ng-template> <ng-template class="box-1" #elseBlock>-</ng-template> <div *ngIf="currentTab.string == 2; then thenBlock else elseBlock"></div> <ng-template class="box-1" #thenBlock>{{currentTab.tab}}</ng-template> <ng-template class="box-1" #elseBlock>-</ng-template> <div *ngIf="currentTab.string == 3; then thenBlock else elseBlock"></div> <ng-template class="box-1" #thenBlock>{{currentTab.tab}}</ng-template> <ng-template class="box-1" #elseBlock>-</ng-template> <div *ngIf="currentTab.string == 4; then thenBlock else elseBlock"></div> <ng-template class="box-1" #thenBlock>{{currentTab.tab}}</ng-template> <ng-template class="box-1" #elseBlock>-</ng-template> <div *ngIf="currentTab.string == 5; then thenBlock else elseBlock"></div> <ng-template class="box-1" #thenBlock>{{currentTab.tab}}</ng-template> <ng-template class="box-1" #elseBlock>-</ng-template> </div> </div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.