Hold a compass near a wire with no current running through it. Nothing happens. Now run current through that wire. The needle swings, even though the wire never touched it and looks exactly the same as before.
The needle doesn't swing toward the wire, the way it would near a bar magnet. It swings sideways. Why sideways?
That sideways swing is the whole story. A moving charge doesn't pull on a compass needle. It spins the space around itself. Once you see that spin clearly, solenoids, electromagnets, and motors all fall out of one simple rule.
A current doesn't attract. It spins the space around it.
Picture looking down the length of a wire, straight at its cross section, with current flowing toward you. The magnetic field around that wire doesn't point at the wire or away from it. It circles around it, like water spinning around a drain.
Point your right thumb in the direction the current flows. Your fingers curl in the direction the field circles. That's the whole rule. No equations yet, just a shape: current in a straight line, field wrapped around it in circles.
Drag the probe around the wire. The field arrow always points tangent to the circle, never toward or away from the wire. Flip the current to flip which way it spins.
Drag the probe anywhere on that circle and watch the arrow. It never points toward the wire. It's always tangent to the circle, running perpendicular to the radius. Flip the current direction and the whole circulation flips with it, clockwise becomes counterclockwise. That's the right-hand rule made visible: the direction of the current sets the direction of the spin, nothing else does.
How strong is the spin? Just count the distance.
Close to the wire, the field is strong. Far away, it's weak. We write the relationship as:
Here is the current, is the distance from the wire, and is a fixed constant of nature called the permeability of free space, roughly in SI units. It just tells you how strongly current sources a magnetic field, the same way tells you how strongly mass sources gravity.
Notice the field falls off as , not like gravity or the electric field from a point charge. That's because the source here isn't a point, it's a line, current running the whole length of the wire. Every point along that line contributes, and the geometry of an infinite line spreads the field differently than a point does.
Any loop works. The shape doesn't matter.
Here's the part that seems almost too convenient. Draw any closed loop around the wire, any shape at all, a circle, a square, a lopsided blob. Walk around that loop and add up the field's push along your path at every step. That sum is called the line integral of the field, and we write it as:
This is Ampere's Law. The left side is the total circulation around your loop. The right side is just the current passing through it, multiplied by that same constant . The loop's size and shape never show up in the formula. They cancel out completely.
Drag the loop bigger or smaller. The field weakens and the circumference grows, but multiplied together they never change.
Watch what happens as you shrink and grow the loop. The field gets weaker as the loop grows, sure, but the circumference grows too, and by exactly the right amount. Multiply them together and the answer never budges. That's not a coincidence, it's the reason the falloff exists in the first place: it's precisely tuned so a circle's circumference growth cancels the field's decay.
This is what makes Ampere's Law genuinely useful. For any current arrangement with enough symmetry, you can pick whatever loop shape is easiest to work with, a circle around a wire, a rectangle around a slab, and the messy geometry problem turns into simple algebra.
Only the current inside the fence counts.
Think of the loop like a fence around a farm. Ampere's Law only cares about livestock actually standing inside the fence. It doesn't care how big the farm is, what shape the fence takes, or how many animals are wandering around just outside it.
Currents outside your loop can be enormous. It doesn't matter. Only what threads through the loop contributes to , and even then, direction matters: a current flowing one way adds, the opposite way subtracts.
Drag wires across the fence, click a wire to flip its direction. Only what's strictly inside the loop ever counts, and sign matters.
Drag the wires in and out of the loop. Click one to flip its direction. Watch update. A wire sitting just outside the boundary might carry more current than everything inside combined, and it still contributes exactly zero. That's the fence rule in action: presence inside the loop is everything, magnitude outside the loop is nothing.
Stack the loops and you get something new: a solenoid.
A single loop of wire makes a modest, lopsided field. But what happens if we take many loops and stack them side by side, like a spring, and run the same current through all of them?
Each loop contributes its own little circulating field. Between two neighboring loops, the outer edges of their fields point in opposite directions and cancel. But down the central axis, running through the middle of every loop, all their fields point the exact same way. Stack enough loops and that central contribution reinforces itself into something strong and uniform. This coiled structure is called a solenoid.
Slide the turn count up. The interior field lines thicken and straighten, the leakage outside the coil fades toward nothing.
Slide the turn count up and hit play. Watch the interior field lines thicken into straight, evenly spaced lines, while the faint loops of stray field outside the coil fade toward nothing. More turns packed into the same length means a stronger, cleaner interior field and less leakage outside.
Why the field is strong inside and nearly zero outside
We can get the exact formula with the same fence trick from before. Draw a rectangular Amperian loop that runs partly through the inside of the solenoid and partly through the outside, closing up on both ends.
Outside the solenoid, the field is essentially zero, so that side of the rectangle contributes nothing. The two short ends, running perpendicular to the coil, also contribute nothing, because the field runs parallel to the axis, not across it. Only the side running through the interior contributes anything at all.
If the solenoid has turns per unit length and carries current , then a loop of length encloses worth of current, once for every turn it crosses. Ampere's Law then gives:
The length cancels out entirely, leaving a field that depends only on how densely you pack the turns and how much current you push through them. No dependence on where inside the solenoid you measure it. That's why the interior field is uniform: pack the loops tight enough and the field barely varies from the axis to just off the edge of the winding.
This is also the working principle behind every electromagnet you've ever seen: coil wire tightly, push current through it, and you get a strong, controllable, uniform magnetic field. Turn the current off and the field disappears with it. No permanent magnet needed.
The short version
A current in a wire doesn't pull, it circulates, and the right-hand rule tells you which way. Ampere's Law says the total circulation around any loop depends only on the current actually enclosed by that loop, never on the loop's size or shape, and never on current sitting outside it. Stack many current loops into a solenoid and their fields cancel outside while reinforcing inside, producing a strong, uniform field given by . Every electromagnet, motor, and MRI machine is this same idea, scaled up.
All visualizations are interactive React components running entirely in your browser. Drag interactions use raw pointer coordinates, the animation in the solenoid uses requestAnimationFrame with elapsed time. No libraries beyond React.