The Republicans fought to abolish slavery during the Civil War and the Dems fought to keep it yet in the 1960s it was the Dems fighting for equal rights for blacks but the Republicans fillibustered against it.
What happened to cause the 2 parties to shift to their polar opposites and why?
This [previous answer] (https://www.reddit.com/r/AskHistorians/comments/g72o7x/originally_republicans_were_opposed_to_slavery/) helps explain some of the factors. Most simply it happened in the realignment of the Republicans into a party of business under McKinley and the rise of progressiveism the decade prior that split the republicans in the 1910s and was then ultimately embraced by FDR and attached to the dem party.
Happy to answer follow up questions you may have about it since that answer is a broad overview of a long political history in America (as it pertains to the flip).