简体   繁体   English

Angular-全局样式CSS和组件CSS

[英]Angular - global styles css and component css

In an Angular project I want to have many tables with a defined style. 在Angular项目中,我希望有许多具有定义样式的表。 So for each table in the components I need to write the css for the table styling. 因此,对于组件中的每个表,我需要为表样式编写css。

Take this StackBlitz example: StackBlitz 以这个StackBlitz为例: StackBlitz

As you can see I have master table with black border, and an agent table with red border, and they are in two separate components. 如您所见,我有一个带黑色边框的主表和一个带红色边框的代理表,它们位于两个单独的组件中。 I want that my master table continue black, but the agent table, and all new table I will create latter on in other components had for example an green border. 我希望我的主表继续为黑色,但是代理表以及我将在其他组件中创建的所有新表都具有绿色边框。

So my question is, what I need to do if I want use a specific style for a table and use the "global" style for other tables? 所以我的问题是,如果我想为表使用特定样式,而对其他表使用“全局”样式,该怎么办?

Update: how this works also with buttons? 更新:这也可以与按钮一起使用吗?

For example when styling a button , if you have a general style you would like to use across the page or the site, use a class, rather than styling the HTML element. 例如,在对button进行样式设置时,如果您希望在页面或网站上使用一般样式,请使用类,而不是对HTML元素进行样式设置。 Else you'll have to override those styles any time you don't want to use them. 否则,您在不想使用任何样式时都必须覆盖它们。 This way you can easily extend your styles so that a button can inherit the styles and have changes. 这样,您可以轻松扩展样式,以便按钮可以继承样式并进行更改。 Notice how .button-extended inherits the :hover styles from .button . 注意.button-extended是如何从.button继承:hover样式的。 By styling the class rather than the element, you are free to use it on any HTML element, rather than being restricted to one. 通过设置类而不是元素的样式,您可以在任何HTML元素上自由使用它,而不必局限于一个元素。 I also recommend you look into learning SASS (SCSS). 我还建议您研究学习SASS(SCSS)。 This is an example of the HTML and CSS: 这是HTML和CSS的示例:

 .button { display: inline-block; width: auto; background: #000; color: #fff; border: solid #39f; border-radius: 5px; padding: 1em; text-decoration: none; text-align: center; margin-bottom: 0.5em; } .button:hover { background: #fff; color: #000; } .button-specific { display: inline-block; background: #fff; color: #000; border: solid; border-radius: 5px; padding: 1em; text-decoration: none; text-align: center; margin-bottom: 0.5em; } .button-specific:hover { color: #39f; background: #000; } .button-extended { display: block; border: solid #ff0000; text-transform: uppercase; padding: 2em; background: #ccc; } 
 <a href="#" class="button">.button</a> <br> <a href="#" class="button-specific">.button-specific</a> <br> <a href="#" class="button button-extended">.button, .button-extended</a> 

You can put any global styles that you want in the styles.css file in your src folder. 您可以将所需的任何全局样式放入src文件夹中的styles.css文件中。 These styles are global to every template in your application. 这些样式对于应用程序中的每个模板都是全局的。

Then put any component unique styles in a css file in the same folder as the component and reference it in the @Component decorator. 然后,将所有组件的唯一样式放入与该组件相同的文件夹中的css文件中,并在@Component装饰器中对其进行引用。

@Component({
  selector: 'pm-star',
  templateUrl: './star.component.html',
  styleUrls: ['./star.component.css']
}) 

Styles defined in the @Component decorator are encapsulate for use with the component. @Component装饰器中定义的样式被封装以与组件一起使用。

Let's say you have two css file, one for global and the other one for the specific component and you just want to style a button or a table etc.. 假设您有两个css文件,一个用于全局文件,另一个用于特定组件,而您只想设置按钮或表格的样式等。

global.css global.css

.styledbutton { background-color : Red }

component.css component.css

.styledbutton { background-color : green !important }

Link the global css file to all of your component and the specific css together in the component like 将全局css文件链接到您的所有组件以及组件中的特定css在一起,例如

@Component({
   selector: 'component',
   templateUrl: 'component.html',
   styleUrls: ['global.css', 'component.css']
})

If you delete all the css from the component.css the button will get the global.css and if not it will take overriden css 如果您从component.css中删除所有css,则该按钮将获得global.css;否则,它将获取覆盖的css

Hope this helps 希望这可以帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Angular CSS 中的组件 Styles - Angular Component Styles in CSS 为什么 CSS 样式不适用于带有 Vaadin 元素的 Angular 2 组件? - Why does CSS Styles not apply on Angular 2 component with Vaadin elements? 如何在Angular 5中从父组件继承子组件中的css样式 - How to inherit css styles in child component from parent in Angular 5 Angular:<ol> 由 document.execCommand 添加在 styles.css 中只看到 css 规则,但在组件的 ZC7A628CBA22E2A28EB66761 中没有看到</ol> - Angular: <ol> added by document.execCommand sees only css rules in styles.css but not in the component's css 如何将 CSS 样式添加到特定的 angular 插值 - How to add CSS styles to a particular interpolation in angular Angular不会加载styles.css - Angular won't load styles.css 在 Angular 组件 Z2C56C360580420D293172F42D85 中覆盖材料 CSS class - Overriding a material CSS class in Angular component CSS angualr 6 组件中的优化 css 选项,styles.css 中的常用 css 或每个组件的 css - Optimized css option in angualr 6 components, common css in styles.css or per component css 从全局CSS跨度样式内联中排除跨度 - Exclude Span From Global CSS Span Styles Inline 是应该在html元素还是body元素上设置全局css样式? - Should global css styles be set on the html element or the body element?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM