In order to improve the accuracy of vehicle target detection and the stability of ranging in driving environments, a vehicle target detection and ranging method based on deep learning is proposed. The YOLOX-S algorithm is used as the vehicle target detection framework for improvement: the CBAM attention module is introduced on the basis of the original algorithm to enhance the network feature expression ability, and the confidence loss function is replaced by Focal Loss to reduce the training weight of simple samples and improve the attention of positive samples. The vehicle ranging model is established according to the imaging principle and geometric relationship of the vehicle camera, and the ranging feature point coordinates and camera internal parameters are input to obtain the ranging results. The self-made Tlab dataset and BDD 100K dataset are used to train and evaluate the improved YOLOX-S algorithm, and a static ranging experimental scene is built to verify the vehicle ranging model. The experimental results show that the improved YOLOX-S algorithm has a detection speed of 70.14 frames per second on the experimental data set. Compared with the original algorithm, the precision, recall, F1 value, and mAP are improved respectively 0.86%、1.32%、1.09%、1.54% ; within the measurement range of 50 m in the longitudinal direction and 11.25 m in the lateral direction, the average ranging error is kept within 3.20% . It can be seen that the proposed method has good vehicle ranging accuracy and stability while meeting the real-time requirements of vehicle detection.