Electrocatalysts with high efficiency and stability for water splitting over a broad pH range are highly desirable but difficult to synthesize. Herein, we synthesized ultrafine Co4N nanodots anchored to the surface of an N-doped carbon framework (Co4N@NC) via a new space-restricted strategy. The key to this method is the design of a task-specific deep eutectic solvent precursor, which ultimately forms ultrafine Co4N nanodots that are anchored to the surface of NC framework by hydrogen bonding interactions between the components. The formation of a new Co-C bond at the interface of Co4N@NC expedites the electron transfer and optimizes the adsorption/desorption energies. Co4N@NC demonstrates excellent electrocatalytic water splitting activity at all pH values, requiring only 1.47 (0.5 mol L−1 H2SO4), 1.48 (1.0 mol L−1 KOH), and 1.61 V (1.0 mol L−1 PBS) to reach 10 mA cm−2 current density. This study provides theoretical guidance for the systematic design of electrocatalysts with excellent stability in various electrolytes.