Aims. We investigate the size-density relation in extragalactic H ii regions, with the aim of understanding the role of dust and different physical conditions in the ionized medium.Methods. First, we compiled several observational data sets for Galactic and extragalactic H ii regions and confirm that extragalactic H ii regions follow the same size (D)-density (n) relation as Galactic ones (n ∝ D −1 ), rather than a relation with constant luminosity (n ∝ D −1.5 ). Motivated by the inability of static models to explain this, we then modelled the evolution of the size-density relation of H ii regions by considering their star formation history, the effects of dust, and pressure-driven expansion. The results are compared with our sample data whose size and density span roughly six orders of magnitude. Results. The extragalactic samples cannot be understood as an evolutionary sequence with a single initial condition. Thus, the sizedensity relation does not result from an evolutionary sequence of H ii regions but rather reflects a sequence with different initial gas densities ("density hierarchy"). We also find that the size of many H ii regions is limited by dust absorption of ionizing photons, rather than consumption by ionizing neutral hydrogen. Dust extinction of ionizing photons is particularly severe over the entire lifetime of compact H ii regions with typical gas densities of > ∼ 10 3 cm −3 . Hence, as long as the number of ionizing photons is used to trace massive star formation, much star-formation activity could be missed. Such compact dense environments, the ones most profoundly obscured by dust, have properties similar to "maximum-intensity starbursts". This implies that submillimeter and infrared wavelengths may be necessary to accurately assess star formation in these extreme conditions both locally and at high redshift.