Understanding the intricate relationship between marine geography and microbial functions is crucial for marine conservation and management. In this study, we conducted a comprehensive analysis of bacterial composition and function in nearshore and offshore sediments of the Beibu Gulf using 16S rRNA high-throughput sequencing. The results showed that Proteobacteria (average relative abundance: 27.07 %) and Desulfobacterota (average relative abundance: 12.28 %) were the most dominant phyla across all stations, while Woeseiaceae (3.26 %-8.31 %) and Anaerolineaceae (0.61 %-7.43 %) could serve as potential indicator species for pollution. In our study area, the α-diversity of bacterial communities in sediment samples showed an initial increase from coastal to offshore regions, followed by a decrease with further distance from the coastlines. The composition of sediment bacterial communities was mainly influenced by total phosphorus (R