Solar is thought of as more environmentally because it generates about 1/15th to 1/25th the lifecycle emissions that fossil-fuel-produced electricity does:
That's based on a meta-analysis of about 400 studies comparing the lifecycle emissions of various power generation sources.
Yes, there are other environmental costs from producing solar panels (e.g. mining) and if they are improperly disposed of then there are risks of contamination. But the same applies to basically any energy source. Fossil fuel extraction is energy intensive and has negative environmental impacts beyond emissions; nuclear plants require vast amounts of material to construct, and obviously the waste needs to be dealt with extremely carefully; hydro dams also use a lot of material to build and they can have negative impacts on the surrounding environment. But ultimately, climate change is the biggest existential threat we face, so reducing carbon is generally going to be prioritized over other environmental concerns. And of course, as solar panels become more ubiquitous, recycling them will become easier and cheaper (and likely built into the cost of purchasing them like with many other things).