Consider an Angular component with both inline styles defined in the @Component decorator and external styles linked via styleUrls. What is the behavior of Angular when rendering these styles?
@Component({
selector: 'app-sample',
template: `<p class='text'>Hello</p>`,
styles: [`.text { color: red; }`],
styleUrls: ['./sample.component.css']
})
export class SampleComponent {}Think about how CSS specificity and Angular style encapsulation work together.
Angular combines both inline styles and external styles when rendering a component. Inline styles defined in the styles array are added as style tags scoped to the component, and external styles from styleUrls are also included. If there is a conflict, inline styles generally have higher precedence due to their order and specificity.
Which of the following Angular component decorators correctly applies inline styles?
Remember the property name for inline styles is plural and expects an array.
The styles property in the @Component decorator expects an array of strings, each string containing CSS rules. The styleUrls property expects an array of file paths. Using style or a string instead of an array causes errors.
Given this Angular component, the external CSS file app.component.css contains p { font-weight: bold; } but the paragraph text is not bold. What is the likely cause?
@Component({
selector: 'app-root',
template: `<p>Important text</p>`,
styleUrls: ['./app.component.scss']
})
export class AppComponent {}Check the file path and file existence carefully.
If the path in styleUrls is incorrect or the file is missing, Angular silently fails to load the styles, so no styles apply. Angular supports .scss files if configured properly, so the extension alone is not the issue.
Consider this Angular component with conflicting inline and external styles. What color will the paragraph text be?
@Component({
selector: 'app-color',
template: `<p class='text'>Color Test</p>`,
styles: [`.text { color: green !important; }`],
styleUrls: ['./color.component.css']
})
export class ColorComponent {}Recall how CSS !important affects style precedence.
The inline styles defined in the styles array are applied with higher specificity and the !important rule forces the color green to override any external style color like blue or red.
In large Angular projects, why is it generally better to use external stylesheets (styleUrls) instead of inline styles (styles)?
Think about how code organization and browser caching affect large apps.
External stylesheets allow better separation of concerns, easier updates, and browser caching benefits. Inline styles increase the component bundle size and make reuse harder. Angular supports both, but external styles are preferred for scalability.