html - 在 index.html 中垂直对齐 Angular 组件 - 使用显示 :grid

标签 html css angular

有没有一种简单的方法可以垂直对齐显示的 Angular 组件?

我正在使用 display: grid 作为主容器。

stackblitz

时钟组件.html

<div class="container">
  {{time | date: 'HH:mm:ss'}}
</div>

时钟组件.css

:host {
  margin: auto;
}

app.component.html

<div class="container">
  <app-clock></app-clock>
</div>

app.component.css

.container {
  display: grid;
  height: 100%;
}

最佳答案

不确定您是否更喜欢 flexbox 解决方案,但这是我最近在这种情况下开始使用的解决方案:

.container {
    display: flex;
    align-items: center;
    height: 100%; /* make sure .containers's parents also have fixed height */
}

:host {
    display: inline-block;
    flex: 1;
}

此外,还有一种定位方法,在您的情况下很常见:

.container {
    position: relative;
    height: 100%;
}

:host {
    display: inline-block;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
}

关于html - 在 index.html 中垂直对齐 Angular 组件 - 使用显示 :grid,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52017498/

相关文章:

javascript - 制作跨平台应用程序的最佳方式?

html - H2-H3 标签行为怪异

html - CSS 变换属性问题 - 无法调整背景颜色

javascript - 基本网络音频 API 不播放声音

javascript - 创建迪士尼尘埃风格的光标轨迹

node.js - 未安装特定的 Angular Cli 版本

javascript - typescript map : key and value swapped on iteration

html - CSS:关于我的响应式标题设计的问题

c# - 如何将标签对齐到表格 asp.net C# 中行的顶部

Angular 4,在没有实际路由的情况下获取路由数据