I am replacing a light fixture in the middle of a circuit. Power flows into the fixture from the circuit breaker and out of the fixture to other lights and whatnot. There's also a switch attached. I thought I hooked everything back together properly, but when I switched the circuit back on at the breaker, the light turned on even though the switch was off. When I turned the switch on, it tripped the circuit. I attempted to make a diagram (minus grounding wires which are also connected). What did I do wrong and how do I fix it?
You put the switch and the light fixture both between hot & neutral, effectively parallel to each other. That makes the light always on and the switch creates a short between hot & neutral - correctly tripping the breaker.
What you have is a switch loop. They are confusing at first but actually quite simple.
Light switches are normally wired in the "middle" of the hot wire between the power source and the light. The way you do that with a switch loop is as follows:
- Black from power (hot) connects (yes this will seem strange) to the white wire going to the switch. It bypasses the light fixture.
- White from power (neutral) connects to the silver screw on the light fixture.
- Black from the switch goes to the brass screw on the light fixture. This is called switched hot.
This uses the same wires you already have, just arranged slightly differently. Plus you need one more wire nut to connect the black power wire to the white switch wire.
As noted in a comment on the question, you may want to preemptively replace the switch as it may have been damaged by arcing from shorting. If you do that (or if you replace it sometime later for any other reason), keep in mind that a traditional switch loop does not have neutral. That rules out many, but not all, options for timers, lighted switches, dimmers, smart switches, etc. If you put in anything other than a simple switch, make sure that it does not require neutral.
Meet the old-style switch loop
Back in the bad old days when "smart switches" were not a thing, switches only required two wires, namely always-hot and switched-hot. So, to save money, electricians took advantage of that fact and ran /2 cable to connect a switch to a light fixture when power came into the light. This scheme is called a switch loop. While the style you see here is no longer Code as of 2011 as a requirement that neutral be brought down to switch locations was added in that edition, it is widespread enough that one needs to know how to identify it and deal with it correctly.
Fortunately, there's a dead tip-off when you run across the next one of these: if you see a white wire connected to a hot wire of some color in a light fixture or receptacle box, or a white wire landed on a single pole switch, you in all likelihood are looking at a switch loop configuration. Furthermore, the symptoms you see with the light being on with the switch off and the switch tripping the breaker when turned on are symptomatic of what happens when a switch loop is miswired by "matching the colors". Wiring such a switch loop, though, is not a challenge once you realize what's going on:
- The white wire going off to the switch connects to the always-hot wires in the box (the black wires in the incoming and outgoing power cables in your case)
- And the black wire coming back from the switch connects to the hot screw on the light fixture, making it the switched-hot connection.
Don't forget to use some black electrical tape (or a Sharpie if you'd rather) to mark that white wire in the switch loop cable as a hot wire! That way, it's less confusing for the next bloke who works in the box.