The aims were to assess the prevalence of HIV infection among young adult heroin users, including injecting heroin users (IHUs) and non-injecting heroin users (NIHUs), and to explore the differences by gender and other factors. The design was a cross-sectional cohort study between April 2001 and December 2003, which included 961 current heroin users (HU), aged 18–30 years: 422 in Madrid, 351 in Barcelona and 188 in Seville; 621 were IHUs and 340 were NIHUs. All were street-recruited by chain referral methods. Face-to-face interviews were conducted using a structured questionnaire with computer-assisted personal interviewing (CAPI). Samples for HIV testing (dried blood spot) were collected and tested with ELISA and Western Blot. Bivariate, logistic regression, and classification and regression tree analyses were performed. The overall prevalence of HIV infection among IHUs was 25·8% (95% CI 22·3–29·3) [32·4% (95% CI 26·6–38·1) in Madrid, 20·5% (95% CI 15·6–25·4) in Barcelona, and 20·6% (95% CI 9·8–31·4) in Seville], whereas in NIHUs it was 4·0% (95% CI 2·1–6·7), with no differences among cities. The prevalence was significantly higher in women than in men in NIHUs (10·9%, 95% CI 4·3–17·5 vs. 1·7%, 95% CI 0·5–4·2) and was non-significantly higher in IHUs (30·4%, 95% CI 23·0–37·8 vs. 24·1%, 95% CI 20·1–28·1). HIV prevalence in short-term IHUs was 12·9% (CI 8·8–17·02), with no differences among cities. In the logistic analysis, the variables associated with infection in IHUs were ever having injected with used syringes (OR 3·4, 95% CI 2·2–5·3), ever having been in prison (OR 2·6, 95% CI 1·6–4·0), and heroin as the first drug injected at least weekly (OR 2·3, 95% CI 1·1–4·5). Factors positively associated with HIV infection in NIHUs were female sex (OR 8·7, 95% CI 2·6–29·2) and age >25 years (OR 3·1, 95% CI 0·9–11·1), while primary educational level was inversely associated (OR 0·26, 95% CI 0·1–0·9). Although there are important geographic differences, HIV prevalence in IHUs remains high, even in short-term IHUs, whereas it was almost six times lower in NIHUs. The prevalence in women is higher than in men, particularly among NIHUs. A wide range of preventive strategies should be developed, aimed primarily at empowering women to negotiate safe sex.