简体   繁体   English

在mat-card-title中左右对齐

[英]Align left and right in mat-card-title

Using the following code in Angular 5 : Angular 5使用以下代码:

  <mat-card>
      <mat-card-title> Test message </mat-card-title>
  </mat-card>

I get this: 我明白了: 一个文字

How can I get this (some text is left-aligned, some text is right-aligned)? 我怎样才能得到这个(一些文本是左对齐的,一些文本是右对齐的)? 在此输入图像描述

I tried creating div and put inside paragraphs with predefined css but it didnot work out. 我尝试创建div并使用预定义的css放入内部段落,但它没有成功。

Use flexbox: 使用flexbox:

 .container { display: flex; } .fill { flex: 1; } 
 <div class="container"> <div>Card</div> <div class="fill"></div> <div>Title</div> </div> 

You can also do it with fewer lines and adjust it better to the content by using a container and flex properties: 您也可以使用更少的行来完成它,并使用容器和flex属性更好地调整内容:

<mat-card class="card-container">
  <mat-card-title > Test message </mat-card-title>
  <mat-card-title> Test message </mat-card-title>
</mat-card>

and this CSS: 这个CSS:

.card-container {
  /*  not needed styles to reflect it */ 
  background: blue;
  padding: 10px;
  color: white;
  width: 500px;

  /* needed styles below */
  display: flex;
  justify-content: space-between;
}

You can check a working sample here: https://jsfiddle.net/VanessaRC/Lz9vz6bs/1/ 您可以在此处查看工作示例: https//jsfiddle.net/VanessaRC/Lz9vz6bs/1/

This would ensure, that if you adjust the width to the container you need, the style adjusts nicely to fit without breaking and keeps your HTML clear and easy to read. 这样可以确保,如果您将宽度调整到所需的容器,样式可以很好地调整以适应而不会破坏并保持HTML清晰易读。

I Can't see images, but here is how you can split text in a div using flex layout 我无法看到图像,但这里是如何使用flex布局在div中拆分文本

 .container { /* Style */ height: 64px; line-height: 64px; background: teal; color: white; font-family: Helvetica; padding: 12px; box-sizing: border-box; /* Actual logic */ display: flex; align-items: center; justify-content: center; } .container div:last-child { text-align: right; } .container div { flex: 1 1 auto; } 
 <div class="container"> <div>Card</div> <div>Title</div> </div> 

Although the previous posters address the question sufficiently, once you add card content, it gets more complicated. 虽然之前的海报充分解决了这个问题,但是一旦你添加了卡片内容,就会变得更加复杂。 I'd suggest to look at this answer for a more complete solution: How to align left and right text mat-card-header in angular 4? 我建议看一下这个答案,找到一个更完整的解决方案: 如何在角4中对齐左右文本mat-card-header?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM