Cellulose nanofibrils are biocompatible nanomaterials derived from sustainable natural sources. We report hydrogelation of carboxylated cellulose nanofibrils with divalent or trivalent cations (Ca(2+), Zn(2+), Cu(2+), Al(3+), and Fe(3+)) and subsequent formation of interconnected porous nanofibril networks. The gels were investigated by dynamic viscoelastic measurements. The storage moduli of the gels are strongly related to valency of the metal cations and their binding strength with carboxylate groups on the nanofibrils. Hydrogel moduli may be tuned by appropriate choice of cation. Cation-carboxylate interactions are proposed to initiate gelation by screening of the repulsive charges on the nanofibrils and to dominate gel properties through ionic cross-linking. Binding energies of cations with carboxylate groups were calculated from molecular models developed for nanofibril surfaces to validate the correlation and provide further insight into the cross-linked structures. The cellulose nanofibril-based hydrogels may have a variety of biomedical and other applications, taking advantage of their biocompatibility, high porosity, high surface area, and durability in water and organic solvents.