Why does period 3 of the periodic table contain 8 elements instead of 18?
You could say that the period number tells you about the largest value of principal quantum number in which a electron is present.

Electrons are filled according to $n+l$ rule. It states as $n+l$ increases, the energy of the orbital increases. If $n+l$ is the same, then the bigger $n$ has larger energy.

So $\mathrm{4s}$ would be filled before $\mathrm{3d}$. Therefore elements containing $\mathrm{3d}$ electrons would have their $\mathrm{4s}$ orbital filled, and therefore, come in 4th period. There 3rd period contains elements having $\mathrm{3s}$ and $\mathrm{3p}$ electron as their last electron which are equal to 8.