Most trilobites lived in fairly shallow water and were benthic; they walked on the bottom, and probably fed on detritus. A few, like the agnostids, may have been pelagic, floating in the water column. Cambrian and Ordovician trilobites generally lived in shallow water. After the Ordovician, when many trilobite groups declined or went extinct, the survivors tended to be restricted to deeper water.
Food particles were stirred up by the legs and passed forward to the mouth. Since the mouth had no large mandibles, we infer that trilobites were not usually predatory and were restricted to soft food. Some trilobites had long spines on the first leg segment, the gnathobase; these may have been able to tear up larger pieces of food, and probably scavenged for a living.
Fossil burrows and tracks have been found that match trilobite bodies very precisely; these show that trilobites could burrow into sediment to feed or to avoid predators. Many trilobites living after the Cambrian developed the ability to roll up, also probably as a defense against predators. This specimen of Pliomera fischeri, from the Ordovician of the St. Petersburg region of Russia, is doing just that. Notice the notches in the pygidium just where it contacts the glabella. In the living trilobite, these would have allowed water to enter and flow over the gills, so that the trilobite could breathe while enrolled.