We present a real-space renormalization-group analysis for the U(1) spin model in two and three dimensions and for compact electrodynamics in three and four dimensions. The Hamiltonian formulation of the theory defined on a hypercubic spatial lattice is used. The nonperturbative renormalization-group transformations we derive give continuous phase transitions for the spin models, in two and three dimensions. In three dimensions the gap exponent agrees remarkably well with series-expansion results. For the gauge theory we find, in agreement with Polyakov, that compact electrodynamics in three dimensions confines for all values of the coupling constant. A calculation for the string tension shows that it vanishes with an essential singularity at zero coupling. In four dimensions for weak enough coupling confinement is lost and the theory shows a phase transition between a charge-confining and a free-photon phase.