【问题标题】:Flexbox layout, stumped on the header sectionFlexbox 布局,难倒标题部分
【发布时间】:2019-12-07 22:14:34
【问题描述】:

很好奇,如果您的 flexbox 专家有时间帮助这个布局(如果有必要也可以使用 Grid,但我认为这应该可以通过 flexbox 实现)。

这些年的自力更生已经使我的大脑适应了条件,我需要重新训练它。

这是我目前所拥有的,仍在搞砸。

我有什么: 已放置徽标并将统计信息部分拉到右侧,但我无法弄清楚如何将其正确放入列中。

想要的结果

<mat-toolbar>
  <div class="company-logo"></div>
  <div class="stats">
    <div class="stats-one">
      Info 1: XXX
      Info 2: XXX
    </div>
    <mat-divider [vertical]="true"></mat-divider>
    <div>
      Info 3: XXX
      Info 4: XXX
    </div>
  </div>
</mat-toolbar>
.mat-toolbar {
  display: flex;
  align-items: stretch;
  height: 5em;
  background-color: #4CAEB2;
  margin-bottom: 2em;
}

.company-logo {
  align-self: flex-end;
  background-image: url("../../../assets/images/logo.jpg");
  background-repeat: no-repeat;
  height: 4em;
  flex-basis: 20em;
}

.stats {
  display: flex;
  flex-direction: column;
  margin-left: auto;
  color: white;
}

【问题讨论】:

    标签: html css flexbox css-grid


    【解决方案1】:

    Flexbox 可以提供布局,但在我看来它需要过多的代码。

    更有效的解决方案是同时使用 flexbox 和网格。

    我在下面发布了一个示例。

    每一行代码都执行一个特定的功能。要查看每个规则的作用,请进入开发工具并切换每个规则的复选标记。

    mat-toolbar {
      display: flex;
      height: 5em;
      border: 1px solid black;
    }
    
    .company-logo {
      display: flex;
      align-items: center;
      justify-content: center;
      margin-left: 1em;
      margin-right: auto;
    }
    
    .stats {
      width: 30%;
      height: 100%;
      padding: 5px;
      margin-right: 1em;
    }
    
    .stats-one {
      display: grid;
      grid-template-columns: 1fr auto 1fr;
      grid-template-rows: 1fr 1fr;
      grid-auto-flow: column;
      align-items: center;
      justify-items: center;
      height: 100%;
    }
    
    .stats-one::before {
      grid-column: 2 / 3;
      grid-row: 1 / 3;
      border-right: 1px solid black;
      content: '';
      height: 80%;
      margin: 0 5px;
    }
    
    * {
      box-sizing: border-box;
    }
    <mat-toolbar>
      <div class="company-logo">
        <img src="http://i.imgur.com/60PVLis.png" width="65" height="65" alt="">
      </div>
      <div class="stats">
        <div class="stats-one">
          <div>Info 1: XXX</div>
          <div>Info 2: XXX</div>
          <div>Info 3: XXX</div>
          <div>Info 4: XXX</div>
        </div>
      </div>
    </mat-toolbar>

    jsFiddle demo

    【讨论】:

    • 谢谢。这让我走上了正轨。非常感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-23
    • 2021-03-19
    • 1970-01-01
    相关资源
    最近更新 更多