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.

Component css styles are not applied?

See original GitHub issue

It appears that any css that I add to my component.css is not applied inside the markdown directive.

<markdown [data]="mydata">
<!-- component css ain't working in here -->
</markdown>

You can verify this by e.g. adding h2 {color: red } in the styles part near the top of app.ts in your plunker, https://plnkr.co/edit/y5LPj7?p=preview

@Component({
  styles: [`
h2 { color: red; /* not working */
    }
  `],
})

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:5
  • Comments:15 (2 by maintainers)

github_iconTop GitHub Comments

13reactions
jfcerecommented, Oct 16, 2019

Hi @ben4d85, thanks for bringing that up!

Right now this problem is due to the ViewEncapsulation of the components that uses the default ViewEncapsulation.Emulated which means that the CSS that is inside the component opening/closing tags is scoped.

If you are not familiar with ViewEncapsulation you can read this post by Pascal Pretch that explains it very well: https://blog.thoughtram.io/angular/2015/06/29/shadow-dom-strategies-in-angular2.html

Workarounds

There is 2 workarounds you can use right now (that I tested in Plunker) …

Shadow-piercing

You can use the ::ng-deep shadow-piercer in your CSS to force styling to be applied inside the scope of the markdown component.

@Component({
  selector: ...,
  template: ...,
  styles: [`
+   markdown ::ng-deep h2 {
      color: red;
    }
  `],
})

Plunker example: https://plnkr.co/edit/UdmGqrar9LmSJoUh?p=preview

Removing encapsulation on your component

Another way would be to remove the encapsulation of your component by using ViewEncapsulation.None which would make the CSS of this component global instead of scoped.

import { ViewEncapsulation } from '@angular/core';

@Component({
  selector: ...,
  template: ...,
  styles: [`
    h2 {
      color: red;
    }
  `],
+ encapsulation: ViewEncapsulation.None,
})

Plunker example: https://plnkr.co/edit/Z5tsu5?p=preview

Long term solution

I will take a look in the next few days to see the impact of setting ViewEncapsulation.None on markdown component so we don’t have to use any workaround to apply styling inside the component.

I’ll keep the issue open until I investigate.

4reactions
JoshuaMullikencommented, Oct 4, 2019

::ng-deep was the only solution that worked for me.

markdown ::ng-deep img {
  width: 100%;
}

This was now properly applied within the markdown component.

Read more comments on GitHub >

github_iconTop Results From Across the Web

CSS is not working in my angular component - Stack Overflow
The CSS classes in my rating.component.ts file are not working. My project is compiling because I can add text to my rating component...
Read more >
CSS style not applying on dynamic injected html elements
I am using Angular 2.0.0-beta.12 & D3, i am injecting D3 barchart after viewInit, and inject css in component by adding attribute "styles", ......
Read more >
Component styles - Angular
Sometimes it's useful to apply styles to elements within a component's template based on some condition in an element that is an ancestor...
Read more >
ASP.NET Core Blazor CSS isolation - Microsoft Learn
Isolate CSS styles to individual pages, views, and components to reduce or avoid: Dependencies on global styles that can be challenging to ...
Read more >
Angular :host, :host-context, ::ng-deep - The Complete Guide
In this post, we will learn how the default Angular styling mechanism (Emulated Encapsulation) works under the hood, and we will also cover ......
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