A pet fence includes a first part having multiple first bars, and two second parts each having multiple second bars are slidably connected to the first part. Two positioning tubes are connected to the first part and each have first holes and second holes. Multiple adjustment units connect the first part to the second parts, and each adjustment unit is cooperated with a locking unit which extends through one of the first holes the positioning tubes. When the locking unit is loosened, the two second parts are movable relative to the first part to adjust the width of the pet fence. At least one clamp unit is connected to one of the second holes of the positioning tube to secure the at least one clamp unit to the headrest rod the seat of a vehicle.