Magnesium-doped nickel oxide (Mg x Ni 1−x O) thin films are transparent over a wide ultraviolet-visible spectral range and over a wide Mg content range. However, the influence of the Mg dopant on the structure and properties of NiO films is poorly understood. In this work, the lattice distortion and the electronic structure of Mg x Ni 1−x O (0 x 0.52) thin films deposited on ultrasmooth sapphire substrates were investigated using synchrotron x rays. Films with higher Mg content had lower values of Debye temperature and atomic order parameter. The nearest Ni-O distance and the in-plane nearest Ni-Ni distance both expanded with increasing Mg content. The Ni 2p core-level spectra and the valence band spectra of the Mg x Ni 1−x O thin films showed complex multiplet structures that were caused by the strong electron correlation in the Ni 3d states, where the spectral features are strongly dependent on both the distortion of the NiO 6 octahedra and the Mg content. We found that the electronic structures are mainly a result of hybridization of Ni 3d and O 2p in the NiO 6 octahedra and the reduction of the Zhang-Rice bound state following Mg doping. Finally, the flexibility of the band gap tuning in Mg x Ni 1−x O thin films is explained.