Due to the small size, the ionisation enthalpies of Be and Mg are much higher than those of other alkaline earth metals. This means that the valence electrons in beryllium and magnesium are more tightly held by the nucleus. Therefore, they need large amount of energy for excitation of electrons to higher energy levels. Since such a large amount of energy is not available in bunsen flame, therefore, these metals do not impart any colour to the flame.