AbstractUsing polystyrene two‐dimensional photonic crystal as template, gatifloxacin (GTFX) as imprinting molecule, methanol as solvent, acrylic acid as functional monomer, ethylene glycol dimethyl acrylate as crosslinking agent and 2,2‐diethyloxyacetophenone as initiator, after UV initiation polymerization the GTFX molecularly imprinted two‐dimensional photonic crystal hydrogel (GTFX‐MIPCH) sensor was prepared. The response performance of the hydrogel was investigated by measuring the diameter change of the Debye ring. The experimental results showed that the prepared GTFX‐MIPCH has a sensitive response to GTFX. When the concentration of GTFX increases from 0 to 1 × 10−4 mol L−1, the diameter of the Debye ring decreases by 8.50 mm, the corresponding particle spacing increases by 29.1 nm, and the color of the sensor changes from purple to orange. Also there was a linear relationship between the change of particle spacing (Δd) and the logarithm of GTFX concentration (lg c) in the range 10−12–10−6 mol L−1. It is noteworthy that the limit of detection of GTFX‐MIPCH is as low as 10−14 mol L−1. In addition, in a solution of GTFX analogues enrofloxacin, levofloxacin, ciprofloxacin and norfloxacin, the change of particle spacing of the GTFX‐MIPCH sensor is 13.3, 11.6, 11.1 and 10.3 nm respectively, indicating that GTFX‐MIPCH has good specific recognition ability for GTFX. Moreover, GTFX‐MIPCH shows good reusability and can also be used for the detection of GTFX in water samples. This GTFX‐MIPCH can achieve a visual detection effect on GTFX through the Debye ring and color change. © 2023 Society of Industrial Chemistry.