How to apply css to parent using child selector -- with css or angular 2

angular style parent from child
angular component css not working
angular ng-deep
angular global styles
ng-deep alternative
angular view encapsulation
change child component css from parent angular
/deep/ css

How to apply css properties using child selector ( mat-checkbox-checked ), Need to apply properties to its parent td and siblings... No need to apply for the second tr and its childs. Using only css or angular 2 or material or javascript ..! without using jQuery

        <!-- apply styles to this row -->
            <mat-checkbox class="mat-checkbox-checked"></mat-checkbox>
          <td>data 1</td>
          <td>data 2</td>
          <td>data 3</td>
          <td>data 4</td>
      <!-- No need to apply styles to this row -->
            <mat-checkbox class="mat-checkbox"></mat-checkbox>
          <td>data 1</td>
          <td>data 2</td>
          <td>data 3</td>
          <td>data 4</td>

if you want to select the first row of this table, you can do something like this:

tbody tr:first-child  {
    background: red;

Hope this would help.

Styles Between Components in Angular 2+ ←, With Angular 2+, component styles are protected and won't bleed unto You can use the special :host pseudo-class selector to apply styles in the host component's template, the one that parent to the In the following example span.​info with be style in the current component and it's children components:. Our community has been nominated for a Webby Award for Best Community Website - thank you! Show the love and vote here.

Assign some models to the checkboxes as follows

  <mat-checkbox class="mat-checkbox" [(ngModel)]="firstCb"></mat-checkbox>
  <mat-checkbox class="mat-checkbox" [(ngModel)]="secondCb"></mat-checkbox>

And use ngClass on trs as follows

<tr [ngClass]="{'selected' : firstCb}">

When user checks the first checkbox, then it’s parent tr will have selected as a class. Then you can apply any style you want like this

tr.selected { color: blue; }

Component styles, You can use the CSS class names and selectors that make the most sense in the context The host element is in a parent component's template. from the host element down through this component to all of its child elements in the DOM. an empty styles array when you create the component with the --inline-style flag. As we’ve covered, there is no such thing as a parent selector, so selecting a parent isn’t possible. We might be tempted to sidestep the problem, by completely reconsidering the design so that selecting the parent is avoided. But if that isn’t possible then here are some potential approaches. Using CSS :has() Selector

There is no parent selector in css. But if you using angular 2+, you can always inject ElementRef for DOM manupulation.

constructor(private elementRef: ElementRef){}


   // rootElement is native html element, therefore native methods and properties of html element and node is available.

   const rootElement = this.elementRef.nativeElement; 

   // get parent of checkbox with class of 'mat-checkbox-checked'
   const parentNode = rootElement.querySelector('.mat-checkbox-checked').parentNode;
   // and change its background color = '#f1f1f1';

Angular 2/4 Component Styles :host, :host-context, /deep/ Selector , Debugging Angular Styles; The :host modifier, when to use it and why; The There are a couple of reasons for that, and one key reason is CSS maintainability​. to any element with the blue-button class anywhere on the page, style 2 will The ::ng-deep pseudo-class selector also has a couple of aliases:  Sometimes although rare, there are occasions where we would like a component’s CSS to not be encapsulated and apply globally to our application. Angular offers an easy way to disable CSS encapsulation. So lets disable this on our SecondComponent.

Angular :host, :host-context, ::ng-deep, In addition, it is the only way to set the CSS class of the host element the host element it is necessary to use the :host -selector in your scss file. The :nth-child (n) selector matches every element that is the n th child, regardless of type, of its parent. n can be a number, a keyword, or a formula. Tip: Look at the :nth-of-type () selector to select the element that is the n th child, of a particular type, of its parent. Browser Support. The numbers in the table specifies the first

theCodeCampus → Angular 2, CSS Encapsulation allows for scoping one's styles to a specific 1 2 3 4 $ ng new encapsulation --styles=”scss” $ cd encapsulation $ ng g c first $ ng g c second. bash. This will generate an Angular app that uses SCSS and has the the child components are declared in parent determine style inheritance;  Angular applications are styled with standard CSS. That means you can apply everything you know about CSS stylesheets, selectors, rules, and media queries directly to Angular applications. Additionally, Angular can bundle component styles with components, enabling a more modular design than regular stylesheets.

CSS Encapsulation in Angular, Now, in "app-parent.component.css" I want to be able to do this: 2 points · 1 year ago edit: There are very valid use cases to perform this type of styling. in the parent, you may want to use the host-context selector in combination with other classes. A declarative library for handling hotkeys in Angular applications​. The child combinator ( >) is placed between two CSS selectors. It matches only those elements matched by the second selector that are the children of elements matched by the first. Elements matched by the second selector must be the immediate children of the elements matched by the first selector. This is stricter than the descendant combinator