The greater the number of people in a given area the worse it will be for that environment.
Honestly, I even think detached houses beat most apartments. Apartments often depend on electric baseboard radiators and window AC units for heating and cooling. They often have multiple massive hot water heaters where the people on higher floors must leave their water running for 10-15 minutes before they start getting hot water. They also tend to have outdated appliances that are less energy efficient like electric ranges, and old refrigerators and ancient clothes washers/dryers. In most of the apartments I've lived in, it was firmly against the rules to hang your clothes on a line to be dried by wind and sun.
The appliances you get are often not as well maintained either. Tenants aren't pulling out the fridge to clean the coils and landlords rarely do either. Renters don't have the ability to make the kinds of improvements that would reduce their energy bills and landlords are incentivized to cut corners on construction and maintenance since it saves them money and the expensive utility payments are the renter's problem (a problem they can't even know about until after they've signed a lease and gotten their electric bill).
Public transportation is better than driving cars, but most people don't use public transpiration anyway and the number of cars on the road in dense urban settings is much higher than on rural streets. We can see that reflected in the air quality, which is abysmal in highly populated urban centers compared to rural areas.
Urban environments have fewer trees, fewer unpaved expanses of land, more pollution (air, water, light, noise), less wildlife (greater habitat destruction, more poisons and other dangers to animals) and more trash/litter.
Urban settings are by far less climate friendly than rural settings, but certainly we'd have new problems if everyone lived 2 miles from their nearest neighbor.
There's a middle ground that I don't think we've really reached yet but for now it's probably a good thing that we have masses of people concentrated into small tightly packed highly polluted pockets of filth and disease and also wide open spaces where it's clean and quiet, where animals can live, and the air is cleaner. We'd be smart to try to increase the efficiency of the spaces we move into while still avoiding going over the number of humans a given area can sustainably support, but I have a feeling that there will always be people who want to live farther way from anything else than is practical and people who want to live in areas that are too densely populated to be good for the environment.