Due to advantages of low cost and intrinsic flame retardancy, urea−formaldehyde (UF) foams with electromagnetic shielding function present a promising application prospect in the fields of construction, transportation and electronics. In this study, by using carbon nanotubes (CNTs) as conductive filler and thermo‐expandable microspheres (EMs) as physical foaming agent, a series of UF/CNT composite foams with perfect and polygonal closed cellular structure were prepared for electromagnetic shielding. By adding 6 wt% EMs, the composite foam exhibited a uniform cell wall thickness, while the local accumulation of CNTs was inhibited and CNTs were distributed evenly within the cell walls. With increasing CNT content the dispersion of CNTs transformed from a scattered and ‘island‐like’ structure to an interconnected structure, and for samples with CNT content higher than 1.34 vol% a continuous conductive network was constructed within the cell walls, thus significantly improving the electrical conductivity and electromagnetic shielding effectiveness for the foams, which reached 34.5 dB and satisfied the requirement of commercial applications. Moreover, the contribution of absorption to the electromagnetic shielding effectiveness was much larger than that of reflection for composite foams, indicating that absorption was the dominant shielding mechanism. © 2023 Society of Industrial Chemistry.