如何将 mat-button 与 mat-input 对齐?

nsq*_*res 5 html css angular-material angular mat-input

我怎样才能让这两个项目正确对齐?我试图让它们完美对齐,但调整按钮高度只会使底部离搜索输入的底部更远。我认为这是因为它们的中心没有对齐,所以调整高度并不是真正的正确解决方案。 在此处输入图片说明

这是 HTML

<!-- Search Box and Button-->
    <p>Search by company name:</p>
    <mat-form-field appearance="outline"> 
        <mat-label>Search</mat-label>
        <input matInput placeholder="Search">
    </mat-form-field>

    <button mat-stroked-button color="primary">Search</button>
Run Code Online (Sandbox Code Playgroud)

这是 SCSS:

mat-form-field.mat-form-field {
    font-size: 12px;
    width: 300px;
  }

  button.mat-stroked-button {
    font-size: 14px;
    height: 42px;
    margin: 10px;
  }
Run Code Online (Sandbox Code Playgroud)

小智 5

要实现水平对齐,您需要对标记进行一些调整,并使用 flex 属性来实现完美对齐。我添加了一些 div 作为父类,但如果您愿意,您可以将 CSS 分配给代码的实际父类。我在下面添加了我的代码片段,相应地更新 HTML 和 CSS :

.searchcontainer {
          text-align:center;
          width: 100%;
          max-width: 600px;
          margin: 0 auto;
       }

       .searchcontainer .search-part {
          display: flex;
          flex-direction: row;
          justify-content: center;
          align-items: center;
       }

      mat-form-field.mat-form-field {
          font-size: 12px;
          width: 300px;
      }

      button.mat-stroked-button {
          font-size: 14px;
          height: 42px;
          margin: 10px;
      }
Run Code Online (Sandbox Code Playgroud)
<div class="searchcontainer">

      <p>Search by company name:</p>

      <div class="search-part">

        <mat-form-field appearance="outline"> 
            <mat-label>Search</mat-label>
            <input matInput placeholder="Search">
        </mat-form-field>

        <button mat-stroked-button color="primary">Search</button>
      </div>

    </div>
Run Code Online (Sandbox Code Playgroud)

  • 遗憾的是他们没有提供本地执行此操作的方法。另外,如果放大的话,它就不会那么漂亮了。 (3认同)