Permeability variation in the vertical direction, a typical sandstone reservoir heterogeneity, can trap a large amount of oil in the lowpermeability layer. We performed water- alternating - gas (WAG) injection and CO2 foam flooding on a specially constructed millimetersized layered sandstone and investigated fluid distribution using highresolution X - ray microtomography. Based on the segmented images, CO2 capillary- trapping capacity, oil recovery, Euler number, shaper factor, capillary pressure, and fluid flow conductivity were calculated. Our results show that increasing the number of WAG cycles favored CO2 capillary trapping, and oil recovery was enhanced by the WAG strategy. However, there was still a significant amount of oil trapped in the lowpermeability layer. After the WAG injection, the connectivity of the residual oil clusters decreased, the capillary pressure of the oil clusters increased, and oil flow conductivity decreased. This was not conducive to further oil recovery. The subsequent injection of CO2 foam effectively recovered the oil in the lowpermeability layer. During the no- injection period, we observed a crossflow phenomenon caused by gravity segregation (the highpermeability layer was located below the lowpermeability layer), i.e., oil in the lowpermeability layer decreased while oil in the highpermeability layer increased, which is beneficial for subsequent oil production. Furthermore, CO2 moved upward driven by gravity, and although capillary barriers could prevent CO2 from escaping, subsequent water injection was essential to improve the stability of CO2 capillary trapping. This work accurately quantifies the distribution of oil and gas in highand lowpermeability layers, thus providing fundamental data for oil recovery and CO2 trapping in reservoirs with vertical heterogeneity. Although the sample used in the experiment was not natural reservoir rock, our results imply that when the permeability ratio between the two layers is greater than 2, sufficient attention must be paid to the fluid distribution differences caused by this layered heterogeneity. Different displacement strategies, such as WAG and CO2 foam flooding, or gravity differences between oil and gas can be used to enhance oil recovery.