Habitat preference may promote adaptive divergence and speciation, yet the conditions under which this is likely are insufficiently explored. We use individual-based simulations to study the evolution and consequence of habitat preference during divergence with gene flow, considering four different underlying genetically based behavioural mechanisms: natal habitat imprinting, phenotype-dependent, competition-dependent and direct genetic habitat preference. We find that the evolution of habitat preference generally requires initially high dispersal, is facilitated by asymmetry in population sizes between habitats, and is hindered by an increasing number of underlying genetic loci. Moreover, the probability of habitat preference to emerge and promote divergence differs greatly among the underlying mechanisms. Natal habitat imprinting evolves most easily and can allow full divergence in parameter ranges where no divergence is possible in the absence of habitat preference. The reason is that imprinting represents a one-allele mechanism of assortative mating linking dispersal behaviour very effectively to local selection. At the other extreme, direct genetic habitat preference, a two-allele mechanism, evolves under restricted conditions only, and even then facilitates divergence weakly. Overall, our results indicate that habitat preference can be a strong reproductive barrier promoting divergence with gene flow, but that this is highly contingent on the underlying preference mechanism.