The son of a provincial governor in northern Iceland, Hafstein studied law in Copenhagen, propagated the new literary ideals of Henrik Ibsen, Bjørnstjerne Bjørnson, and Georg Brandes, and campaigned for Iceland’s political independence. In 1886 he went back to Iceland, became a member of the Althing (parliament), and in 1904 was appointed Icelandic minister of state to the Danish crown, the first Icelander to hold the post. He was a tough and dynamic fighter for Iceland’s political freedom and backed it with farsighted work for the improvement of social conditions and educational opportunities for the ordinary Icelander.

Most of his poetry was written when he was a young man; it is filled with the vigour and joy of life, his love of country, and his admiration for the heroic, as exemplified in Heimdallur (1884), a portrait of Brandes. He also wrote many delicate love lyrics and drinking songs.