The pet gates illustrated below will all fit effectively in a 31 inch opening.
If your space measure includes fractions of an inch (eg. 31 1/4"), then for measurements less than 1/2", use the pet gate width equal to the number of inches in your measurement (eg. for 31 1/4" use the 31 inch category).
For measurements that include fractions of 1/2" or greater (eg. 31 1/2" or 31 3/4"), then select the pet gate width that is one inch greater than the number of inches in your measure (eg. for 31 1/2" use the 32 inch category).