The group of fungi sometimes generally referred to as nail fungi are actually various fungal species that fall into two groups. The most common are the dermatophytes, species that are actually adapted to live on skin, hair, nails, and horn, and cause infections in humans and animals in those sites. The second group includes environmental species, fungal species that do sometimes infect nails but usually grow on other food sources in nature.
Published facts about fungi often suggest that fungal nail infections are passed directly from person to person. Fungi produce "conidia," spores that are resistant to environmental conditions and that can remain viable in clothing (like socks), shoes, towels, and places where there is constant moisture such as swimming pool decks and shower stalls. For the dermatophytes, spores in shared shoes and linens and in public places where people go barefoot are significant modes of transmission. For the others, exposure to soil or decaying organic matter is probably more significant. All nail fungi are opportunists: they exploit opportunities presented by exposure to many people, and by the susceptibility of individuals.
One of the interesting facts about fungi that infect hair and nails is that they do not live on living tissue. Hair, nails, and the outer layer of skin cells on our skin are not alive. These parts of our bodies have no nerve endings or blood supply. Nails are formed of skin-like cells piled densely together layer upon layer continuously and pushes out onto the tips of fingers and toes to form a protective layer. Nail fungi grow and spread between these cells, drawing nutrients from the non-living cells. This is also one of the things that make fungal nail infections difficult to treat: topical treatments often fail to penetrate the hard nail, and oral drugs delivered to the site in the blood do not reach the fungal growth.
Although infections caused by nail fungi often appear with little warning, there are things we can do to decrease the chances of acquiring them. First, do not share footwear or buy second hand shoes, and launder socks, towels and other linens frequently. Keep your shoes clean and dry and try not to wear the same pair day after day so that they have lots of time to dry out. Allow as much air circulation around fingers and toes as possible. Keep nails trimmed and clean, and attend to even minor injuries promptly: another of the well documented facts about fungi is that they can gain entry through breaks in the skin and nail injuries. Finally, wear some kind of footwear around swimming pools and in public showers and dressing rooms.
If signs of a fungal infection do appear, see a doctor promptly as these infections are much easier to clear up before they are too advanced.