我正在使用列表中的席卡,但对齐方式有问题。
这是我拥有的:
这就是我想要的:
代码:
<div class="margin-top-20" fxFlexFill fxLayout="row wrap" fxLayout.xs="column" fxLayout.sm="column" fxLayoutGap="15px grid">
<div fxFlex="20" fxFlex.md="33" *ngFor="let advert of adverts; let i = index" class="padding-fix">
<div fxFlexFill fxLayoutAlign="center stretch">
<mat-card class="ad">
<div fxLayoutAlign="space-between center">
<img mat-card-image src="test" alt="test">
</div>
<mat-card-title>test</mat-card-title>
<mat-card-content>
<p>
test
</p>
</mat-card-content>
<mat-card-actions align="end">
</mat-card-actions>
</mat-card>
</div>
</div>
</div>
我不知道可以将图像居中(必要时调整尺寸)。
编辑::感谢coreuter的回答,我已经快要得到我想要的东西了。
第一个区块的高度与其他区块的高度不同。而且每行末尾都有一些空白(我希望每行5个元素)。
更新的代码:
<div class="margin-top-20" fxLayout="row wrap" fxLayoutAlign="start center" fxLayoutGap="15px">
<mat-card fxFlex="20" (click)="addProduct()" class="mat-card-add-button">
<div>
<span style="font-size:32px;text-align:center">+<br />Add product</span>
</div>
</mat-card>
<mat-card fxFlex="20" *ngFor="let product of products; let i = index" class="product">
<img class="image" mat-card-image src="{{product.picture.uri}}" alt="photo">
<mat-card-title>{{product.name}}</mat-card-title>
<mat-card-content>
<p>
test
</p>
</mat-card-content>
<mat-card-actions align="end">
</mat-card-actions>
</mat-card>
</div>
编辑2:
我认为这几乎是完美的。我在mat-card-content div中尝试了很多内容,但我不知道它是否很好。这是我所拥有的屏幕截图:
您认为可以将按钮的高度设置得比大垫子卡片(最后一个)高吗?另一件事,我看不到每行第一个元素的左边框。
这里是更新的代码:
<div class="margin-top-20" fxFlexFill fxLayout="row wrap" fxLayoutAlign="start start" fxLayoutGap="20px">
<mat-card fxLayoutAlign="center center" fxFlex="0 1 calc(20% - 20px)" (click)="addProduct()" class="product" style="height:413px">
<div>
<span fxLayoutAlign="center center" style="font-size:32px;text-align:center">+<br />Add product</span>
</div>
</mat-card>
<mat-card fxFlexFill fxFlex="0 1 calc(20% - 20px)" *ngFor="let product of products; let i = index" class="product">
<img class="image" mat-card-image src="{{product.picture.uri}}" alt="photo">
<mat-card-title>{{product.name}}</mat-card-title>
<mat-card-content>
<p *ngIf="i == 3">
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam dapibus, leo id pulvinar vestibulum, ligula nisl tincidunt magna, eu volutpat leo neque quis justo. Fusce semper ante id mi porta porta. Pellentesque nec pretium purus. Curabitur lobortis tempus consectetur. Nam ullamcorper gravida erat sed suscipit. Morbi quis porttitor nunc. Suspendisse lacinia a turpis vitae laoreet. Aliquam pellentesque scelerisque urna. Cras vulputate nisi sed elit commodo cursus. Aenean interdum, erat at convallis dictum, urna enim tincidunt nisi, vitae tempor nisi nisi a tellus. Aliquam volutpat dui eget gravida eleifend. Nullam pulvinar justo eget tellus commodo, eget molestie dui convallis. Curabitur at fermentum lorem. Maecenas porttitor sem ut enim efficitur bibendum et vel metus.
</p>
<p *ngIf="i != 3">
test
</p>
</mat-card-content>
<mat-card-actions align="end">
<button mat-icon-button>
<mat-icon>mode_edit</mat-icon>
</button>
<button mat-icon-button>
<mat-icon>delete</mat-icon>
</button>
</mat-card-actions>
</mat-card>
</div>
再次感谢您的帮助,非常感谢!
编辑3 :此版本有效!非常感谢coreuter! (https://stackblitz.com/edit/angular-xprafs)
mat-card-content不能通过“ fxFlex”属性来固定。内容在垫卡之外。 (它正在最后一次堆栈闪电中工作,但对我而言不是)。
<div *ngIf="products.length > 0" style="margin-left:10px;">
<div fxLayout="row wrap" fxLayoutAlign="start stretch" fxLayoutGap="20px">
<mat-card fxLayoutAlign="center center" fxFlex="0 1 calc(20% - 20px)" fxFlex.md="0 1 calc(25% - 20px)" fxFlex.sm="0 1 calc(33% - 20px)" fxHide.xs="true" (click)="addProduct()" class="product mat-card-add-button">
<div fxLayoutAlign="center center" style="font-size:32px;text-align:center">+<br />Add product</div>
</mat-card>
<mat-card fxLayout="column" fxFlex.md="0 1 calc(25% - 20px)" fxFlex="0 1 calc(20% - 20px)" fxFlex.sm="0 1 calc(33% - 20px)" fxFlex.xs="100" *ngFor="let product of products; let i = index" class="product">
<img mat-card-image src="{{product.picture.uri}}" alt="photo">
<mat-card-title>{{product.name}}</mat-card-title>
<mat-card-content fxFlex>
<p *ngIf="i == 3">
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam dapibus, leo id pulvinar vestibulum, ligula nisl tincidunt
magna, eu volutpat leo neque quis justo. Fusce semper ante id mi porta porta. Pellentesque nec pretium purus. Curabitur
lobortis tempus consectetur. Nam ullamcorper gravida erat sed suscipit. Morbi quis porttitor nunc. Suspendisse lacinia
a turpis vitae laoreet. Aliquam pellentesque scelerisque urna. Cras vulputate nisi sed elit commodo cursus. Aenean interdum,
erat at convallis dictum, urna enim tincidunt nisi, vitae tempor nisi nisi a tellus. Aliquam volutpat dui eget gravida
eleifend. Nullam pulvinar justo eget tellus commodo, eget molestie dui convallis. Curabitur at fermentum lorem. Maecenas
porttitor sem ut enim efficitur bibendum et vel metus.
</p>
<p *ngIf="i != 3">
test
</p>
</mat-card-content>
<mat-card-actions fxFlexAlign="end" align="end">
<button mat-icon-button>
<mat-icon>mode_edit</mat-icon>
</button>
<button mat-icon-button>
<mat-icon>delete</mat-icon>
</button>
</mat-card-actions>
</mat-card>
</div>
</div>
css:
.mat-card {
padding: 18px !important; // less padding than per default
}
.mat-card-image {
width: calc(100% + 36px) !important; // update padding
margin: 0 -24px 16px -18px !important; // update padding
}
.mat-tab-label {
font-size: 16px !important;
}
.mat-card-title {
font-size:24px !important;
font-weight: 500 !important;
}
.mat-card-content {
font-size: 14px !important;
min-height: 30px; // <--- to remove !!!
}
.product {
margin-bottom: 25px;
/*min-width: 180px;
text-align: center;*/
}
/* desktop button */
.mat-card-add-button {
border: 1px dashed grey;
box-shadow:none !important;
cursor:pointer;
}
.product img {
height: 250px;
object-fit: contain;
}
答案 0 :(得分:2)
自从我回答了您之前的SO question以来,我将以先前的答案为基础对此问题的解答。请参考更新后的Stackblitz,其中包含不同宽度和高度的图像。
编辑:调整了答案/堆叠闪电,使其包含5个元素。
为了使图像始终保持相同的高度,我在“ <img>
”标签中添加了“图像”类(当然,您可以直接通过以下方式将css应用于img
标签) .product img{...}
)。
<img class="image" mat-card-image src="{{product.picture.url}}" alt="photo">
并应用了以下CSS:
.image{
height: 150px; /* adjust as needed */
object-fit: contain;
}
使用object-fit: contain
,您的图像将始终正确缩放并在可用区域内完全可见。
请记住,object-fit
当前仅是fully supported by the following browsers。
为了在每行中获得5个元素,您必须使用fxLayoutGap
属性调整fxFlex
并计算每个元素的宽度。请按如下方式更改您的代码。
<div class="container" fxLayout="row wrap" fxLayoutAlign="center center" fxLayoutGap="20px">
<!-- Add addProduct-button outside loop -->
<mat-card fxFlex="0 1 calc(20% - 20px)" (click)="addProduct()" class="product">
...
</mat-card>
<!-- loop over the products -->
<mat-card fxFlex="0 1 calc(20% - 20px)" *ngFor="let product of products; let i = index" class="product">
...
</mat-card>
</div>
..并将fxLayoutGap
上设置的20px和fxFlex
内的设置更改为您想要的值。
现在设置了这些值后,您必须应用一个最小宽度值,否则所有元素的宽度都会变小,并且行将不会换行:
.product{
min-width: 180px; /* adjust as desired */
min-height: 250px;
margin-bottom: 20px; /* same as fxLayoutGap for even distribution */
}
要使第一个元素与其他元素的高度相同,必须将.product CSS类的(min-)height
调整为等于最高产品的高度。
由于您尚未标记您的问题尚未回答,因此我修改了您在编辑#2中提供的代码以完成所需的设计:stackblitz
我更改了以下内容:
fxLayoutAlign
更改为"space-evenly stretch"
而不是fxLayoutAlign="start start"
,这会将所有项目均匀地分布在x轴上,并使其拉伸到与行。fxFlexFill
fxFlex
添加到了mat-card-content 关于左侧边框。.我认为您的容器距离左侧浏览器窗口太近。我也更改了我的stackblitz中的容器css。
答案 1 :(得分:0)
.ad{
display:flex;
flex-direction:coloumn;
height:100%;
}
img,mat-card-content{
flex-grow: 1;
}
mat-card-actions{
display: inline-block;
align-self: flex-end;
}
您可以尝试一下。
答案 2 :(得分:0)
对于mat-card
,您可能必须使用固定的大小,并且只需确保图像适合任何可用空间即可。
.margin-top-20>div>div {
display: flex;
width: 100%;
}
.ad {
width: 200px;
height: 250px;
min-height: 250px;
max-width: 200px;
border: thin solid #eee;
}
.ad>div {
width: 100%;
height: 200px;
display: flex;
justify-content: center;
align-items: center;
}
.ad>div>img {
flex: 0 1;
width: 100%;
height: auto;
}
<div class="margin-top-20" fxFlexFill fxLayout="row wrap" fxLayout.xs="column" fxLayout.sm="column" fxLayoutGap="15px grid">
<div fxFlex="20" fxFlex.md="33" *ngFor="let advert of adverts; let i = index" class="padding-fix">
<div fxFlexFill fxLayoutAlign="center stretch">
<mat-card class="ad">
<div fxLayoutAlign="space-between center">
<img mat-card-image src="https://via.placeholder.com/150x50" alt="test">
</div>
<mat-card-title>test</mat-card-title>
<mat-card-content>
<p>
test
</p>
</mat-card-content>
<mat-card-actions align="end">
</mat-card-actions>
</mat-card>
<mat-card class="ad">
<div fxLayoutAlign="space-between center">
<img mat-card-image src="https://via.placeholder.com/50x150" alt="test">
</div>
<mat-card-title>test</mat-card-title>
<mat-card-content>
<p>
test
</p>
</mat-card-content>
<mat-card-actions align="end">
</mat-card-actions>
</mat-card>
<mat-card class="ad">
<div fxLayoutAlign="space-between center">
<img mat-card-image src="https://via.placeholder.com/350x150" alt="test">
</div>
<mat-card-title>test</mat-card-title>
<mat-card-content>
<p>
test
</p>
</mat-card-content>
<mat-card-actions align="end">
</mat-card-actions>
</mat-card>
</div>
</div>
</div>