D8 How do I add a node ID as class to the <body> tag?

3
down vote
accepted
In mytheme.theme file: add the below code.

function mytheme_preprocess_html(&$variables) {
// Add node id to the body class.
$node = \Drupal::routeMatch()->getParameter('node');
if($node) {
$variables['attributes']['class'][] = 'node-' . $node->id();
}
}
This will add "node-{{nid}}" class in body tag.