如何在Angular中使用弹性盒布局(Flexbox)

huangapple go评论55阅读模式
英文:

How to use flexbox in Angular

问题

我正在学习Angular,当我尝试在body上使用flexbox时似乎不起作用,但在其他元素上使用它有效。例如,我尝试创建一个简单的计数器应用,并希望将所有内容居中,但似乎不起作用。

我搜索了一下,发现大部分人都在使用Angular Flex-Layout,那是我需要使用的吗,还是我遗漏了什么?

app.component.html

<div class="content">
    <h2 class="counter">{{num}}</h2>
    <button class="btn" (click)="increment($event)">+</button>
    <button class="btn" (click)="decrement($event)">-</button>
</div>

app.component.css

body {
    display: flex;
    align-content: center;
}

.content {
    display: flex;
}
英文:

I'm learning Angular and when I try to use flexbox on the body it doesn't seem to work, but using it on other elements works. For example what I'm trying to do is I made a simple counter app and I want to center everything but it doesn't seem to work.

I googled and all I could see was people using Angular Flex-Layout so is that what I need to use or am I missing something?

app.component.html

&lt;div class=&quot;content&quot;&gt;
    &lt;h2 class=&quot;counter&quot;&gt;{{num}}&lt;/h2&gt;
    &lt;button class=&quot;btn&quot; (click)=&quot;increment($event)&quot;&gt;+&lt;/button&gt;
    &lt;button class=&quot;btn&quot; (click)=&quot;decrement($event)&quot;&gt;-&lt;/button&gt;
&lt;/div&gt;

app.component.css

body {
    display: flex;
    align-content: center;
}

.content {
    display: flex;
}

答案1

得分: 1

你构建后的HTML如下:

&lt;body&gt;
  &lt;app-root&gt;
    &lt;!-- 你的组件的HTML代码 --&gt;
  &lt;/app-root&gt;
&lt;/body&gt;

所以你在这里漏掉了一个步骤。

在你的app.component.scss文件中:

:host {
  height: 100%;
  display: flex;
  align-content: center;
}

.content {
  display: flex;
}
英文:

Your HTML after build is

&lt;body&gt;
  &lt;app-root&gt;
    &lt;!-- HTML Code of your component --&gt;
  &lt;/app-root&gt;
&lt;/body&gt;

So you're missing a step here.

In your app.component.scss file :

:host {
  height: 100%;
  display: flex;
  align-content: center;
}

.content {
  display: flex;
}

答案2

得分: 0

组件的CSS文件是有作用范围的。 这意味着它们只适用于组件中的元素(在您的情况下是app.component.html)

如果您想在组件外部编写样式,可以使用全局样式表,例如 src/styles.css,在 angular.json 中定义。它们的工作方式与常规CSS相同。

外部和全局样式文件

英文:

The component CSS files are scoped. That means they only applies to elements in the component (app.component.html in your case)

https://angular.io/guide/component-styles

If you want to write styles outside components, you can use global stylesheets, e.g. src/styles.css, defined in angular.json. They work just like regular css

external-and-global-style-files

答案3

得分: -1

尝试添加 justify-contentalign-items 来将内容居中对齐。您还需要设置 htmlbody 元素的 heightmin-height

html {
  height: 100%;
}

body {
  min-height: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
}

文档链接在这里

align-items 属性将项目在交叉轴上对齐。

justify-content 将项目在主轴上对齐。

英文:

Try adding justify-content and align-items to align the content to center. You will also need to set height and min-height of html and body elements.

html {
  height: 100%;
}

body {
  min-height: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
}

Documentation is here.

align-items property aligns the item on the cross axis.

justify-content aligns the item on the main axis.

huangapple
  • 本文由 发表于 2023年4月6日 19:32:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/75949030.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定