Dear student,
Sunlight reaches Earth's atmosphere and is scattered in all directions by all the gases and particles in the air. Blue light is scattered more than the other colours.
In case of space,there is not any atmosphere so that scattering of light can take place that is why it is dark even there are many stars in the space.
Regards