Red phosphorus has been well-recognized as promising anode materials for lithium-ion batteries (LIBs) and potassium-ion batteries (PIBs) due to its extremely high theoretical capacity and low cost. However, the huge volume change and poor electric conductivity severely limit its further practical application. Herein, the nanoscale ultrafine red phosphorus has been successfully confined in a three-dimensional pitch-based porous carbon skeleton composed of well-interconnected carbon nanosheets through the vaporization-condensation method. Except for the traditional requirement of high electric conductivity and stable mechanical stability, the micropores and small mesopores in the porous carbon matrix centered at 1 to 3 nm and the abundant amount of oxygen-containing functional groups are also beneficial for the high loading and dispersion of red phosphorus. As anode for LIBs, the composite exhibits high reversible discharge capacities of 968 mAh g−1, excellent rate capabilities of 593 mAh g−1 at 2 A g−1, and long cycle performance of 557 mAh g−1 at 2 A g−1. More impressively, as the anode for PIBs, the composite presents a high reversible capacity of 661 mAh g−1 and a stable capacity of 312 mAh g−1 at 0.5 A g−1 for 500 cycles with a capacity retention up to 84.3%. This work not only sheds light on the structure design of carbon hosts with specific pore structure but also open an avenue for high value-added utilization of coal tar pitch.