对齐垫卡内容(图像、文本和按钮) [英] Align mat-cards content (image, text and buttons)

查看:24
本文介绍了对齐垫卡内容(图像、文本和按钮)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用列表中的 mat-card,但对齐有问题.

这是我所拥有的:

这是我想要的:

代码:

<div fxFlex=20"fxFlex.md=33"*ngFor=让广告的广告;让 i = 索引"class="padding-fix"><div fxFlexFill fxLayoutAlign=中心拉伸"><mat-card class="ad"><div fxLayoutAlign="space-between center"><img mat-card-image src="test";alt=测试">

<mat-card-title>测试</mat-card-title><mat-card-content><p>测试</p></mat-card-content><mat-card-actions align="end"></mat-card-actions></mat-card>

我不明白我可以将图像居中(必要时调整其大小).

感谢coreuter的回答,我快要得到我想要的了.

第一个块与其他块的高度不同.我在每一行的末尾都有一些空白区域(我想要每行 5 个元素).

更新后的代码:

<mat-card fxFlex=20"(点击)=addProduct()";class="mat-card-add-button"><div><span style="font-size:32px;text-align:center">+<br/>添加产品</span>

</mat-card><mat-card fxFlex=20"*ngFor=让产品的产品;让 i = 索引"类别=产品"><img类=图像"mat-card-image src="{{product.picture.uri}}";alt=照片"><mat-card-title>{{product.name}}</mat-card-title><mat-card-content><p>测试</p></mat-card-content><mat-card-actions align="end"></mat-card-actions></mat-card>

编辑 2:

我认为这几乎是完美的.我尝试了 mat-card-content div 里面的大内容,不知道好不好.这是我所拥有的屏幕截图:

您认为按钮与大mat-card(最后一个)的高度相同吗?另一件事,我看不到每行第一个元素的border-left.

这里是更新的代码:

<mat-card fxLayoutAlign="center center"fxFlex=0 1 calc(20% - 20px)"(点击)=addProduct()";类=产品"样式=高度:413px"><div><span fxLayoutAlign="center center"style="font-size:32px;text-align:center">+<br/>添加产品</span>

</mat-card><mat-card fxFlexFill fxFlex=0 1 calc(20% - 20px)";*ngFor=让产品的产品;让 i = 索引"类别=产品"><img类=图像"mat-card-image src="{{product.picture.uri}}";alt=照片"><mat-card-title>{{product.name}}</mat-card-title><mat-card-content><p *ngIf=i == 3">Lorem ipsum dolor 坐 amet,consectetur adipiscing 精英.Etiam dapibus,leo id pulvinar 前庭,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 年代 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 atellus.Aliquam volutpat dui eget gravida eleifend.Nullam pulvinar justo eget Tellus commodo, eget moestie dui convallis.Curabitur 在fermentum lorem.Maecenas porttitor sem ut enim efficitur bibendum et vel metus.</p><p *ngIf=i != 3">测试</p></mat-card-content><mat-card-actions align="end"><按钮垫图标按钮><mat-icon>mode_edit</mat-icon><按钮垫图标按钮><mat-icon>删除</mat-icon></mat-card-actions></mat-card>

再次感谢您的帮助,非常感谢!

EDIT 3:此版本有效!非常感谢coreuter!

.mat-card {填充:18px!重要;/* 填充比默认值少 */}.mat-card-image {宽度:计算(100% + 36px)!重要;/* 更新填充 */边距:0 -24px 16px -18px !重要;/* 更新填充 */}.mat-tab-label {字体大小:16px !重要;}.mat-card-title {字体大小:24px !重要;字体粗细:500 !重要;}.mat-card-content {字体大小:14px !重要;最小高度:30px;/* <--- 删除!!!*/}.产品 {底边距:25px;/*最小宽度:180px;文本对齐:居中;*/}/* 桌面按钮 */.mat-card-add-button {边框:1px 灰色虚线;框阴影:无!重要;游标:指针;}.product img {高度:250px;适合对象:包含;}

<div *ngIf="products.length > 0" style="margin-left:10px;"><div fxLayout="row wrap" fxLayoutAlign="开始拉伸" 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/>添加产品</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 坐 amet,consectetur adipiscing 精英.Etiam dapibus, leo id pulvinar 前庭, ligula nisl tinciduntmagna, eu volutpat leo neque quis justo.Fusce semper ante id mi porta porta.Pellentesque nec pretium purus.库拉比图尔lobortis tempus consectetur.Nam ullamcorper gravida 年代 sed suscipit.Morbi quis porttitor nunc.悬垂藻一份 turpis vitae laoreet.Aliquam pellentesque scelerisque urna.Cras vulputate nisi sed elit commodo cursus.埃涅阿河中间,erat at convallis dictum, urna enim tincidunt nisi, vitae tempor nisi nisi atellus.Aliquam volutpat dui eget gravida埃利芬德.Nullam pulvinar justo eget Tellus commodo, eget moestie dui convallis.Curabitur 在fermentum lorem.保护者porttitor sem ut enim efficitur bibendum et vel metus.</p><p *ngIf="i != 3">测试</p></mat-card-content><mat-card-actions fxFlexAlign="end" align="end"><按钮垫图标按钮><mat-icon>mode_edit</mat-icon><按钮垫图标按钮><mat-icon>删除</mat-icon></mat-card-actions></mat-card>

解决方案

自从我回答了你之前的

EDIT:调整了答案/stackblitz,使一行包含 5 个元素.

说明

为了使图像始终保持相同的高度,我在 <img> 标签中添加了image"类(您当然可以将 css 应用于 img-tag 也可以直接使用 .product img{...}).

并应用以下 CSS:

.image{高度:150px;/* 根据需要调整 */适合对象:包含;}

使用 object-fit: contains,您的图像将始终正确缩放并在可用区域内完全可见.

请记住,object-fit 目前仅 完全以下浏览器支持.

为了在每行中获得 5 个元素,您必须使用 fxFlex 属性调整 fxLayoutGap 和每个元素的宽度计算.请按如下方式更改您的代码..

<!-- 在循环外添加 addProduct-button --><mat-card fxFlex="0 1 calc(20% - 20px)" (click)="addProduct()" class="product">...</mat-card><!-- 遍历产品 --><mat-card fxFlex="0 1 calc(20% - 20px)" *ngFor="let product of products; let i = index" class="product">...</mat-card>

.. 并将 fxLayoutGap 上设置的 20px 和 fxFlex 的计算更改为您想要的值.

现在设置这些值后,您必须应用最小宽度值,否则所有元素的宽度都会变小并且行不会换行:

.product{最小宽度:180px;/* 根据需要调整 */最小高度:250px;底边距:20px;/* 与 fxLayoutGap 相同,用于均匀分布 */}

编辑 2

要使第一个元素与其他元素具有相同的高度,您必须将 .product CSS 类的 (min-)height 调整为等于最高产品的高度.

编辑 3(回答问题的编辑 2)

由于您还没有标记您的问题已回答,我已经修改了您在编辑 #2 中提供的代码以完成您想要的设计:stackblitz

我更改了以下内容:

关于左侧的边框.. 我认为您的容器离浏览器窗口左侧太近了.我也更改了我的 stackblitz 中的容器 css.

I'm working with mat-card in a list and I have a problem with the alignment.

Here is what I have:

Here is what I want:

The code :

<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>

I didn't understand I can I centre the image (resize it if necessary).

EDIT: Thank coreuter's answer, I'm close to getting what I want.

the first block is not at the same height as the others. And I've got some blank space at the end of each row (I would like 5 elements per row).

The updated code:

<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>

EDIT 2:

I think it's almost perfect. I tried with a big content inside the mat-card-content div and I don't know if it's good. Here is a screenshot of what I have:

Do you think it possible to get the buttons at the same height as the big mat-card (the last one)? Another thing, I can't see the border-left of the first element of each row.

Here the updated code :

<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>

Thank you again for your help, I really appreciate it!

EDIT 3: This version works! Thank you very much coreuter! See it on StackBlitz.

The mat-card-content is not fixed by the "fxFlex" property. The content goes outside the mat-card. (It's working on the last StackBlitz but not for me).

.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;
}

<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>

解决方案

Since I answered your previous SO question, I'll build my answer to this question upon my previous answer. Please refer to this updated Stackblitz with images of different width and height.

EDIT: Adjusted the answer/stackblitz to make a row containing 5 elements.

Explanation

In order to keep the image always the same height I've added the class "image" to the <img>-tag (you can of course apply the css to the img-tag directly with .product img{...} as well).

<img class="image" mat-card-image src="{{product.picture.url}}" alt="photo">

and applied the following CSS:

.image{
  height: 150px; /* adjust as needed */
  object-fit: contain;
}

With object-fit: contain your image will always properly scaled and fully visible within the available area.

Keep in mind that object-fit is currently only fully supported by the following browsers.

EDIT:

In order to get 5 Elements within each row you have to adjust the fxLayoutGap and the calculation of the width for each element using the fxFlex attribute. Please change your code as follows..

<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>

.. and change the 20px set on the fxLayoutGap and the within the calculation of fxFlex to your desired value.

With those values now set you have to apply a min-width value, otherwise all elements will just get smaller in width and the row won't wrap:

.product{
  min-width: 180px; /* adjust as desired */
  min-height: 250px;
  margin-bottom: 20px; /* same as fxLayoutGap for even distribution */
}

EDIT 2

To make the first element the same height as the others you have to adjust to (min-)height of the .product CSS-class to be equal to the height of the highest product.

EDIT 3 (to answer edit 2 of the question)

Since you didn't mark your question answered yet, I've modified the code you provided in your edit #2 to accomplish your desired design: stackblitz

I've changed the following:

Regarding the border on the left side.. I assume your container is too close to the browser windows left side. I've change the container css in my stackblitz as well.

这篇关于对齐垫卡内容(图像、文本和按钮)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
前端开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆