The F and N co-doped porous carbon derived from ZIF-8 is used as a membrane in Li–S batteries with enhanced capacity and cycling stability.