The African continent was colonized and divided between the French, British, Germans, Dutch, and even the Portuguese and Belgians. Why was a colonial giant like Spain limited to small parts of Morocco and Western Sahara (if I am not mistaken)?
This is a very interesting question and like many historical questions there is no one answer. In my answer I would discuss the main component that kept Spain out of Africa. This factor would be the The Treaty of Tordesillas. The Treaty of Tordesillas was signed at Tordesillas, Spain, on June 7, 1494, and then it was subsequently authenticated in Setúbal, Portugal. The main goal of this treaty was to settle land disputes between the Castilian (which would later become Spain) and Portuguese crowns related to the Age of Discovery. Pope Alexander VI, who was Spanish born (Aragon at the time), issued several papal bulls (Æterni regis, Inter caetera, Dudum siquidem) setting up a line of demarcation from pole to pole 100 leagues (about 320 miles) west of the Cape Verde Islands. Spain were guaranteed exclusive rights to all land west of that line and Portugal were to keep East of that line (a quick google search of a map of this line will give you a better indication of scope and scale). For the most part the Spanish and Portuguese empires expanded following this demarcation line.
There were some exceptions to this rule. Melilla and Cazaza (which at the time was under Portuguese rule) were put under the sphere of then Castile. Also, the Canary Islands, which had been conquered by the Castilians some decades earlier, were left under Castilian rule. Eventually Ceuta, which was conquered by the Portuguese in 1415, become part of the Portuguese and Spanish crowns during the Iberian Union in 1578; a time when Portugal's king died without an heir so the two countries merged. After Portugal regained its independence in 1668 the land was officially handed over to the King of Spain. Similarly, Equatorial Guinea was conquered by the Portuguese between 1472-1507. In 1778 Queen Maria I of Portugal ceded this land to the Spanish Crown.
Interestingly the Treaty of Tordesillas split the globe in half so there were still other areas where the Kingdom of Spain established authority over domains that were technically supposed to be left for Portuguese claims. The most notable of these were the Philippines. The Philippines were first "discovered" in 1521 by Magellan who was a Portuguese discoverer who sailed under the Spanish. He would then later be killed during the Battle of Mactan in April of 1521. These were the lands that were within the Portuguese sphere of the treaty but were still under Spanish rule.
The search for riches, land, and conversions of indigenous peoples, were paramount in Spain investing heavily in all things in the America. Similarly, the Portuguese crown were also too busy flooding resources in trying to get to India via the Cape of Good Hope. The Portuguese mainly set up coastal forts and trade posts in areas they were interested in like Malacca, Straight of Hormuz, etc. Their main driver were both trade and conversion of populations to Christianity. While in Brazil they did have an expansion into the interior beyond the treaty their ultimate objective were to not spread too far beyond their own abilities (even though they ultimately did so). Portugal spent a huge amount of resources trying to find a mythical ruler named Prester John who was described as a legendary Christian ruler of the East. He was popularized in medieval chronicles and traditions and the Portuguese really wanted to meet this fellow. They even mistook a kingdom of Hindus as Christians, which in turn gave them hope that Prester John could be found. Given the resources of Spain they were able to hold large swaths of the Americas and they had the population and industry to do so. This push to the west, and the continuous findings of new lands, peoples, and riches, led to the Spanish crown being disinterested in Africa. Portugal, their longtime rival, mainly left their territory alone and as such Spain did the same.
So in sum, The Treaty of Tordesillas, and Spanish discoveries to the west, laid the groundwork for Spain decide not to invest vast resources in the colonization of Africa.
Further Reading:
Rivers of Gold: The Rise of the Spanish Empire, from Columbus to Magellan
Conquest: Cortes, Montezuma, and the Fall of Old Mexico
The Golden Empire: Spain, Charles V, and the Creation of America
Conquerors: How Portugal Forged The First Global Empire
Edited: July 22 at 15:55