Vanity comes from corrupt soul because the human being naturally is inclined towards love of the soul which is the origin of human being’s mistakes and immorality.
And due to this case, a small (religious) act/deed seems great to the same person and because of that sees himself as a special servant of God deserving praise. Rather, sometimes his bad deeds look good to him, if he sees better deeds from other people, does not care, and the vain person always considers others deeds as bad and his own inappropriate deeds as good [1].
[1] Sharhe Chehel Hadith, Imam Khomeini (RA), Page 70.
Source: en.shafaqna.com