简体   繁体   English

当在 Angular 8 中有导入的样式表时,如何使用 SCSS 更改字体颜色?

[英]How do I change the font color using SCSS when it has an imported stylesheet in Angular 8?

I am not able to change any font/text colors in my div (using the standard methods), and I think the import of "...theme.scss" is blocking these changes (?), I have never used SCSS.我无法更改 div 中的任何字体/文本颜色(使用标准方法),并且我认为“...theme.scss”的导入阻止了这些更改(?),我从未使用过 SCSS。 Specifically I want to change just the phone numbers (all) text to red.具体来说,我只想将电话号码(所有)文本更改为红色。 I can make other changes, so I know the .scss is hooked up.我可以进行其他更改,因此我知道 .scss 已连接。 I would like to add the red text to an existing style if possible.如果可能,我想将红色文本添加到现有样式中。

html... html...

<div class="contact">
  <h1>Contact Us</h1>
  <div class="contact-footer">
    <p><strong>For any other questions, please contact...</strong></p>
    <div class="contact-footer-container">
      <div class="contact-subcontainer">
      <div class="contact-card"><p>Publications<br>Phone: <a href="tel:1-800-123-4567">1-800-123-4567</a><br> <a href="some@something.com">some@something.com</a></p></div>
      <div class="contact-card"><p>Product Support<br>Toll-Free: <a href="tel:1-800-123-4567">1-800-123-4567</a><br>
        Direct: <a href="tel:1-000-123-4567"> 1-000-123-4567</a><br> Fax:  1-000-123-4567
    </div>
    </div>
  </div>
</div>

component.scss...组件.scss...


@import '~@angular/material/theming';
@import '../../some-material-theme.scss';


p {
    font-family: $font-family;
    font-size: $text-medium;
}

.contact-footer {
    background-color: lightgray;
    padding: 8px;
    margin: 15px;
}
.contact-footer-container {
    display: flex;
    flex-flow: row;
}
.contact-subcontainer {
    display: flex;
    flex-flow: row;
    flex-grow: 1;
}
.contact-card {
    flex-grow: 1;
    padding: 15px;
    padding-top: 0;
}
.contact-card > p {
    line-height: 150%;
}
mat-icon {
    font-size: 18px;
}
a {    
    text-decoration: none;
}

This should work... in component.scss file这应该工作...在 component.scss 文件中

.red-text {
color: red;
}

in template在模板中

<a href="tel:1-800-123-4567" class="red-text">1-800-123-4567</a>

if that doesn't work then there's an issue somewhere else in your code, maybe the scss is not being loaded in @Component or something like that.如果这不起作用,那么您的代码中的其他地方存在问题,也许 scss 没有被加载到 @Component 或类似的东西中。

Check @Component decorator has stylesUrls检查@Component装饰器是否有stylesUrls

@Component({
  selector: 'main-comp',
  templateUrl: './maincomp.component.html',
  styleUrls: ['./maincomp.component.scss']
})

In order to get this working, you may need to put all imports and global stylings in styles.scss , inside /src/ folder which will be made available globally for your all components, additionally you may define custom classes and use as per requirements.为了让这个工作,您可能需要将所有导入和全局样式放在style.scss 中,在/src/文件夹中,该文件夹将为您的所有组件全局可用,此外,您可以定义自定义类并根据要求使用。 For eg in styles.scss :例如在style.scss 中

/* Global styles and imports */

@import '~@angular/material/theming';
@import '../../some-material-theme.scss';

/* General styles */

.color-red {
    color: red;
}

.color-orange {
    color: orange;
}

.color-green {
    color: green;
}

Check if styles.scss is configured properly in angular.json or you can add it for eg:检查style.scssangular.json 中是否正确配置,或者您可以添加它,例如:

"build": {
    "options": {
         ....,
         "index": "src/index.html",
         "styles": ["src/styles.scss"],
         ....
             }
         }

First I agree to suhas that you must import the styles globally ie in styles.scss .首先,我同意你必须全局导入样式,即在styles.scss On another note, importing theme styling might override you base css changes, specially bootstrap/material themes.另一方面,导入主题样式可能会覆盖您的基本 css 更改,特别是引导程序/材料主题。 If you are not getting any run time error in console, you can check the particular <a> element styles by inspecting the element.如果您在控制台中没有收到任何运行时错误,您可以通过检查元素来检查特定的<a>元素样式。

In case you apply any additional class to change the color (eg .color-red ), you must see it in style section of inspect element section and if it is striked-out, meaning your css change is overridden by material css.如果您应用任何其他类来更改颜色(例如.color-red ),您必须在检查元素部分的样式部分看到它,如果它被删除,这意味着您的 css 更改被材料 css 覆盖。 In such case, you might need to add !important at the end of css rule.在这种情况下,您可能需要在 css 规则的末尾添加!important

// HTML
<a class="color-red" href="tel:1-000-123-4567"> 1-000-123-4567</a>


// SCSS
.color-red {
  color: red !important;
}

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

相关问题 我如何使用 css 样式表更改已经生成的 angular 应用程序 - 使用 scss - How can i change already generated angular app with css stylesheet - to use scss 如何在 Angular scss 样式表中使用 ::before? - How can I use ::before in Angular scss stylesheet? 如何以角度更改特定垫表列条目的字体颜色? - How do I change the color of the font of a specific mat table column entries in angular? 如何在到期日更改字体颜色? - How do I change the font color upon the expiration date? 在 Angular 应用程序 (SCSS) 中导入的字体不会在其他设备上加载 - Font imported in an Angular app (SCSS) does not load on other devices angular:如何更改导入的组件的类以更改颜色? - angular: How to change an imported component's class to change color? 如何鼠标悬停在角度为 9 的情况下更改字体真棒 5 图标颜色的颜色? - How to mouseover change the color of a font-awesome 5 icon color with angular 9? angular 的 ngx-bootstrap - 日期选择器当似乎没有任何效果时如何更改主题颜色 - ngx-bootstrap for angular - datepicker how do i change the theme color when nothing seems to work 当我们点击 angular 中的特定图标时如何更改字体图标的颜色 - How to change the color of the font icons when we click on the particular icon in angular 如何更改角度材料输入中线条的颜色? - How do I change the color of the line in an angular material input?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM