html - angular2 - 使用 flexbox 设置组件的高度

标签 html angular css flexbox

假设我的“主”app.component.html 里面只有三行代码:

<app-header-component></app-header-component>
<app-content-component></app-content-component>
<app-footer-component></app-footer-component>

我希望页眉组件占据大约 10% 的屏幕,内容占 70%,页脚占 20%,使用 flexbox 布局,并且这个尺寸不应该改变各自的内容量成分。 我尝试自己添加 flexbox 但比例发生了变化,特别是对于 <app-content-component> .我认为问题在于 <app-content-component>内部有多个嵌套组件,这就是为什么我需要的比率不会保持不变的原因。

最佳答案

设置类似的东西的最简单方法是使用 flex-grow/flex-shrink,它们得到 n 的一部分可用空间,此处设置为全视口(viewport)高度。

flex-basis 设置为 0,因此内容不会影响元素之间的空间分配方式。

它会保持元素的 10%/70%/20% 比例,但是 header/footer 仍然可以根据需要调整高度,哪种of 是使用 Flexbox 的目的。

堆栈片段

body {
  margin: 0;
}
my-container {
  display: flex;
  flex-direction: column;
  height: 100vh;
}

app-header-component {
  flex: 1 1 0;                   /*  1 part(s) of 10  */
  background: lightblue;
}
app-content-component {
  flex: 7 7 0;                   /*  7 part(s) of 10  */
  background: lightgreen;
  overflow: auto;
}
app-footer-component {
  flex: 2 2 0;                   /*  2 part(s) of 10  */
  background: lightblue
}
<my-container>
  <app-header-component>
    Header
  </app-header-component>
  <app-content-component>
    Content with many lines<br>
    Content with many lines<br>
    Content with many lines<br>
    Content with many lines<br>
    Content with many lines<br>
    Content with many lines<br>
    Content with many lines<br>
    Content with many lines<br>
    Content with many lines<br>
    Content with many lines<br>
    Content with many lines<br>
    Content with many lines<br>
    Content with many lines<br>
    Content with many lines<br>
    Content with many lines<br>
    Content with many lines<br>
    Content with many lines<br>
    Content with many lines<br>
    Content with many lines<br>
  </app-content-component>
  <app-footer-component>
    Footer
  </app-footer-component>
</my-container>


请注意,10/* 7 part(s) of 10 */ 是每组 flex-grow 的总和/flex-shrink.

关于html - angular2 - 使用 flexbox 设置组件的高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46880064/

相关文章:

javascript - jQuery 将类添加到图像链接而不会在链接传递变量时弄乱

Angular 4 Reactive Forms 切换隐藏表单元素的验证

angular - 如何创建一个 Angular 库来拦截我的应用程序发出的 http 请求

javascript - Angular 2 构造函数 ngOnInit 未定义属性 TypeScript

html - 每页都有页眉的打印友好页面?

javascript - React CSS 模块中的级联样式

javascript - 在 Javascript 中可靠地将标签添加到 <head>

javascript - 切换div动画

php - 如何使用 CSS 去除空格

html - anchor 定位浏览器