More than three-quarters of the baryonic content of the Universe resides in a highly diffuse state that is difficult to detect, with only a small fraction directly observed in galaxies and galaxy clusters1,2. Censuses of the nearby Universe have used absorption line spectroscopy3,4 to observe the ‘invisible’ baryons, but these measurements rely on large and uncertain corrections and are insensitive to most of the Universe’s volume and probably most of its mass. In particular, quasar spectroscopy is sensitive either to the very small amounts of hydrogen that exist in the atomic state, or to highly ionized and enriched gas4,5,6 in denser regions near galaxies7. Other techniques to observe these invisible baryons also have limitations; Sunyaev–Zel’dovich analyses8,9 can provide evidence from gas within filamentary structures, and studies of X-ray emission are most sensitive to gas near galaxy clusters9,10. Here we report a measurement of the baryon content of the Universe using the dispersion of a sample of localized fast radio bursts; this technique determines the electron column density along each line of sight and accounts for every ionized baryon11,12,13. We augment the sample of reported arcsecond-localized14,15,16,17,18 fast radio bursts with four new localizations in host galaxies that have measured redshifts of 0.291, 0.118, 0.378 and 0.522. This completes a sample sufficiently large to account for dispersion variations along the lines of sight and in the host-galaxy environments11, and we derive a cosmic baryon density of \({\varOmega }_{{\rm{b}}}={0.051}_{-0.025}^{+0.021}{h}_{70}^{-1}\) (95 per cent confidence; h70 = H0/(70 km s−1 Mpc−1) and H0 is Hubble’s constant). This independent measurement is consistent with values derived from the cosmic microwave background and from Big Bang nucleosynthesis19,20.