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.