In your THEMENAME.theme file, add the following code:
Implements hook_preprocess_menu()
1234567891011121314151617
/** * Implements hook_preprocess_menu(). */functionTHEMENAME_preprocess_menu(&$variables,$hook){if($hook=='menu__main'){// We're doing that for main menu.// Get the current path.$current_path=\Drupal::request()->getRequestUri();$items=$variables['items'];foreach($itemsas$key=>$item){// If path is current_path, set active to li.if($item['url']->toString()==$current_path){// Add active link.$variables['items'][$key]['attributes']['class']='active';}}}}