Beryllium and magnesium do not give colour to flame whereas other alkaline earth metals do so. Why?
When an alkaline earth metal is heated, the valence electrons get excited to a higher energy level. When this excited electron comes back to its lower energy level, it radiates energy, which belongs to the visible region. Hence, the colour is observed. In Be and Mg, the electrons are strongly bound. The energy required to excite these electrons is very high. Therefore, when the electron reverts back to its original position, the energy released does not fall in the visible region. Hence, no colour in the flame is seen.
Due to small size, the ionization enthalpies of Be and Mg are much higher than those of other alkaline earth metals. Therefore, a large amount of energy is needed to excite their valence electron, and that’s why they do not impart colour to the flame.