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

标签 html css angular angular-material mat-input

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

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

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

  button.mat-stroked-button {
    font-size: 14px;
    height: 42px;
    margin: 10px;
  }

最佳答案

要实现水平对齐,您需要对标记进行一些调整并使用 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;
      }
<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>

关于html - 如何将 mat-button 与 mat-input 对齐?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60251487/

相关文章:

angular - Http 拦截器在 Angular 6 中不起作用

javascript - 如何使用 JavaScript 生成 HTML 表单?

html - 如何在 bootstrap 中保持跨 div 的边界

javascript - Javascript 修改 CSS 时的 CSS 特异性?

html - 图像 slider 的响应式包装器 div

javascript - 如何在 Angular 应用程序上收听外部 js 事件?

javascript - Angular2 boot.ts 意外错误

html - css - 停靠一个面板并滚动另一面板

html - Bootstrap 类未在 React 组件中获取

html - Django CSS 不工作