By the late 600s AD, the Moors, people from North Africa who had converted to Islam, were regularly attacking Visigothic Spain. At first these raids were not very serious, and the Spanish were able to fight them off. But in 711 AD, the Moors took advantage of a civil war in Spain to make a more serious attack, and this time they won. The Moors took over ruling all of Spain, right up into the Basque country in the Pyrenees and into what is now southern France.
The Moors did not control the northernmost parts of this territory for very long, but they did hold on to southern Spain for almost 800 years.
At first, Spain was under the control of the Umayyad caliphs, far away in Damascus. In 750 AD, the only surviving member of the Umayyad family fled from the Abbasids to Spain, where he took over ruling from the local rulers. Naturally he paid no attention to the Abbasid rulers in Baghdad, but declared Spain to be an independent kingdom. He had his capital at Cordoba, where he built many beautiful buildings, including the famous mosque in the picture you see here.
The descendants of the Umayyads ruled Spain for a long time, but in the 1100s the Almohads took over and moved the capital to Seville.