question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add CSS class to Angular's generated custom elements

See original GitHub issue

Which @angular/* package(s) are relevant/releated to the feature request?

compiler

Description

Right now, component elements in Angular are display: inline by default. This however is almost never desirable and in most cases breaks layouts. It is also quite confusing to people new to Angular – I think the best example here are flex-layouts that do not work “out of the box”.

Proposed solution

To minimize breaking changes, one of the better solutions would be to add a CSS class (e.g. ng-host) to all custom elements generated by Angular. That way, all custom elements could be styled by a single rule in the global style.css / style.scss file in the project space (e.g. .ng-host { display: block; }).

Maybe a flag could be added to the Angular.json settings to disable this behavior (maybe even by default) to keep the project size small on existing projects and projects that do not require this feature.

Alternatives considered

Seeing as it’s quite confusing for people new to Angular, an alternative could also be to make all elements display: block by default. This however would be a huge change for existing, bigger projects.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:2
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
DeffiDefficommented, Feb 7, 2022

Thank you so much, this is all very helpful. I did not know --display-block is a thing, I agree there’s no need for more complexity then!

2reactions
JoostKcommented, Feb 7, 2022

You might be interested to learn that the CLI’s schematic to generate new components has an --display-block option, which you can optionally enable by default in angular.json. It inserts :host { display: block; } into each newly generated component’s styles. This isn’t quite the same as what you’re requesting, but it’s a rather simple approach that doesn’t introduce additional complexity to the runtime.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Add class to custom component angular 2 - Stack Overflow
The code in my answer just adds my-class to the host element of the MyComponent component. You can now style it from the...
Read more >
Angular elements overview
The browser maintains a CustomElementRegistry of defined custom elements, which maps an instantiable JavaScript class to an HTML tag. The @angular/elements ...
Read more >
Creating a Custom Element in Angular | by lavanya k - Medium
1. Install Angular CLI 6 and create a new project · 2. Add elements package · 3. Create a component · 4. Add...
Read more >
Add CSS Styles to a Host Element in Angular - Ultimate Courses
In Angular we create components, which render a template. Much like Web Components, Angular declares which components to render by using the ...
Read more >
[Tutorial] How to create Custom Angular Elements?
Angular Custom Elements are a recent addition to the Angular framework that allows you to create exportable components.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found