Although many studies have focused on the effects of the environment and area on local patterns of species richness, few studies have demonstrated how to reconcile the availability of more niches with smaller habitat areas in heterogeneous localities. Here, the environmental range that a species prefers was defined as a niche; a space was defined as an available space if the environmental range of the space matches a niche; and the metric ‘environmental range per unit space (ERUS)' was presented to describe the heterogeneity of localities. Because the spaces with stressful environmental ranges, outside the niche, increased with increasing heterogeneity, available spaces did not continue to indefinitely increase, but the proportion of available spaces in spaces was low at high heterogeneous localities. Consequently, the probability of species occurring in their respective available spaces was unimodal. Due to the presence of large spaces in homogenous localities and more spaces in heterogeneous localities, the interval distances between nonadjacent spaces were large in these localities. Together, the changes in the number and proportion of available spaces and distance between spaces determined the unimodal probability of species dispersing into their respective available spaces. Thus, the probability of species coexisting was unimodal because it is important for coexisting species to grow in their respective available spaces. The probability of population extinction increased with increasing heterogeneity because the available spaces became narrower. In this way, the ERUS strongly influence species richness: a unimodal richness along the heterogeneity gradient occurred in suitable, suboptimal and stressful environments and a unimodal algae richness occurred in a lake and river. These results challenge the viewpoint that richness increases with heterogeneity, providing information about the conservation of richness in very homogeneous and heterogeneous regions, and highlighting the importance of balancing the roles of environment and space in understanding and predicting richness.