Solution 1 :

Using ngClass directive to set an elements classes, ngStyle directive to set an elements style.

Solution 2 :

For this nothing is required in template, you can simple have in css file of component.

tr:hover {
  background-color:#f5f5f5;
}

But lets say you only wanted this to apply when your tr have a specific class which is based on a flag in your component.ts. Lets say that className was .custom. You can achieve it like :-
CSS :-

tr.custom:hover {
  background-color:#f5f5f5;
}

HTML :-

<table>
   <tr [ngClass]="{'custom': myValue}"></tr>
</table>

In TS :-

   @Input() data: number;
   public myValue = false;
   ngOnInit() {
      if(data>5) {
            myValue = true; 
      } else {
            myValue = false;
      }
   }

What the above will do, that whenever myValue flag will be true it will attach custom class to tr. and only then on hover background color will appear. So from ts you are deciding whether to show the color on hover or not.

For th, td. There is no need of ngClass for Static properties. Ng class is generally used for dynamic binding of a css class and ngStyle is used for dynamic binding of css style(inline style). But still if you want to use it.

NgStyle

TS :-

 public padding= 15px;
 public align: left;

HTML :-

 <td [ngStyle]="{'padding': padding, 'text-align': align}"></tr>
 <th [ngStyle]="{'padding': padding, 'text-align': align}"></th>

NgClass

CSS:-

  th.custom, td.custom {
     padding: 15px;
     text-align: left;
  }

HTML :-

   <td [ngClass]="{'custom': true}"></tr>
   <th [ngStyle]="{'custom': true}"></th>

But you will be requiring ngStyle/ngClass with each tr and th unless its inside ngFor.

Problem :

    <table border-bottom = 2>
        <th, td >
        {
            padding: 15px;
            text-align: left;
        }

        <tr:hover {background-color:#f5f5f5;}></tr>
    </table>

Writing in the above fashion throws errors. What’s the syntax for Angular?

Please show an example of combining th, td kind of properties with ngClass/ngStyle or anything else that is applicable.

Comments

Comment posted by Aquarius_Girl

Thankful to you. I didn’t know that these properties also existed.

Comment posted by ptpaterson

This answer might be more helpful to other users if you provided a code example that shows how to use the directives.

Comment posted by Aquarius_Girl

I request you to add an example to show how to group these properties using ngclass and ngstyle. I googled but couldn’t find info on this.

Comment posted by Aakash Garg

padding and text-align are css properties, not html properties. Your presentation and styling should be separate. that’s why we keep html and css separate

Comment posted by softwareengineering.stackexchange.com/questions/271294/…

@Aquarius_Girl here you go :-

Comment posted by Aakash Garg

@Aquarius_Girl, did it answer your question?

Comment posted by Aakash Garg

this is inline styles, ngClass and ngStyle is about dynamic binding of style and class. your question wanted example of ngClass and ngStyle. The link you gave, you can use same syntax in angular. as they are known as inline css. But its not a suggested practice to use inline styles. your question wanted to know about ngStyle and ngClass. I gave all approaches of that in my answer. please refresh your page to see my complete answer.

Comment posted by Aakash Garg

@Aquarius_Girl, if you only want inline css. your question is not related to angular. its only related to html. please remove angular tag from it. Altough ngStyle and ngClass are very useful for learning to dynamically bind styles and css classes. Please go through them in my example. They will be helpful to you in future.

By