Three major figures in German philosophy, G.W.F. Hegel and Ludwig Feuerbach, and Immanuel Kant, have all influenced Karl Marx's thoughts on religion to a varying extent.
Hegel's philosophy regarding religion revolved around his theory of the dialectic. He believed that throughout history there are series of reflections; thoughts follow through a process, there is the initial thought (the thesis), and a contrasting thought (the antithesis) which cause a strain between the two, this disappears with the final thought (the synthesis), which is a combination of the first two thoughts. For Hegel, this is the explanation of why we have religion in our society, and we worship a supreme being. Religion is the result of the dialectic process throughout history, and people believe in an absolute, final realisation/ synthesis, the Geist (a spirit). However Hegel maintains emphatically that this Geist is in reality nothing, it does not exist, it purely hypothetical, a product of the mind. Karl Marx's critique of religion is essentially a critique of Hegel's philosophy and he in part uses Feuerbach's ideas of alienation in his criticism.
Feuerbach discussed his theory of religion in his work "The Essence of Christianity". He held the belief that religion is not based around a supernatural Deity who created the universe and is the source of all goodness. Religion is merely a form of self awareness, we create the concept we call God, projecting our highest qualities upon him. Accordingly as we hold wisdom, goodness, love, justice to be the highest values in which to aspire to, our constructed, perfect, metaphysical being is the source of these attributes. Consequently we become alienated from ourselves, for everything which is good and commendable becomes attributed to God and the divine, and this means that we become the corrupt and sinful beings.
Feuerbach's beliefs are echoed in Marx's idea of religion being the opium of the people. However Marx criticised Hegel and Feuerbach for they only looked at their theory in reference to individuals and did not apply it to the whole of society. They also held no desire to change the society's views regarding religion. Marx in contrast to these two took a materialistic attitude towards Feuerbach's ideas, taking his idealistic theory forward to another level. Marx analysed religion within the wider context of society, believing that it was essential not to treat religion as an isolated institution, but to recognise the connection between religion and society and labour productions.
The aim of Feuerbach, and consequently Marx, was to attempt to reverse the existing theory that God was the centre of all goodness, so that instead, humans revolve around themselves, they being in the centre rather than this illusory being. This parallels with the work of another German philosopher, Immanuel Kant. In the preface of the second edition of his work "Critique of Pure Reason" he wrote about the famous "Copernican revolution". Kant reversed the existing perspective of looking at metaphysics, which in turn echoed Copernicus' work on the helio-centric model of the universe.
Also Marx's belief that we need to rid from society any rule or institution which causes people to believe that they are sinners, and are immoral, enslaved beings, has connections with Kant's work in his Critique of Practical reason concerning the basic principle of the categorical imperative. Kant had also used the metaphor of opium before when describing religion in his work "Religion within the limits of mere reason", from which Marx coined the phrase, "religion as the opium of the people".