Yes, card counting is indeed legal, however, it is frowned upon and casinos can invoke their Right Of Admission Reserved clause to ban players who they find doing it. Online it is pointless to try to count cards as the virtual decks are fully shuffled after each hand of Blackjack.