A three-dimensional (3D) variant of scanning micro X-ray fluorescence (XRF) is described and evaluated at the ID18F instrument of the European Synchrotron Radiation Facility (ESRF). The method is based on confocal excitation/detection using a polycapillary half-lens in front of the energy-dispersive detector. The experimental arrangement represents a significant generalization of regular two-dimensional (2D) scanning micro-XRF and employs a detector half-lens whose focus coincides with that of the focused incoming beam. The detection volume defined by the intersection of the exciting beam and the energy-dependent acceptance of the polycapillary optics is 100-350 mum(3). Minimum detection limits are sub-ppm, and sensitivities are comparable with regular scanning XRF. Next to the reduction of in-sample single/multiple scattering, the setup provides the possibility of sample depth scans with an energy-dependent resolution of 10-35 mum in the energy range of 3-23 keV and the possibility of performing 3D-XRF analysis by simple XYZ linear scanning. This provides a suitable alternative to X-ray fluorescence tomography. The method is illustrated with results of the analysis of solid inclusions in diamond and fluid inclusions in quartz.