Look at the board after 1.e4 e5. Neither side can castle, and neither side will be able to for at least two more moves. That is the reason beginners meet first, and it has nothing to do with checks or attacked squares: the bishop is still on f1 and the knight is still on g1, and the rule says every square between the king and the rook must be empty.
8
7
2
a
b
c
d
e
f
g
h 1 Five reasons, then, and they are worth separating by how long they last.
The two you never get back
Your king has moved. That is the end of it, on both wings, for the rest of the game. It does not matter whether the king moved to escape a check, to grab a pawn, or by accident on move three; it does not matter that it walked back to e1 afterwards. Nothing restores the right.
That rook has moved. Same thing, but only on that side. Play Rh1-e1 and short castling is dead while long castling is still available with the untouched a1 rook.
A captured rook counts the same way. If Black’s bishop takes on h1, White has no short castle for the rest of the game, and it makes no difference that the king never budged.
Something in the way
The commonest reason of all, and the one in the diagram above. Pieces between the king and the rook stop it, whether they are yours or your opponent’s. A black knight that has landed on f1 blocks White’s short castle just as effectively as White’s own bishop would.
This is a queue, not a rule about attacks. Empty the squares and you can castle, even if half of them are covered by enemy pieces, as long as the king’s own three squares are safe.
A check costs a move, not the right
Here is the distinction that matters most.
8
7
2
a
e
h 1 White is in check and cannot castle out of it. But count the options: 1.c3 blocks on c3 and attacks the bishop at the same time, and after 1…Bd6 the king is still sitting on its original square with both rooks at home, so 2.O-O is legal. The other three legal moves are all king moves, and every one of them throws the right away forever.
That is the habit to build. When you are checked before you have castled, look for a block or a capture first, and only move the king when there is nothing else. There are separate pages on castling out of check and on castling through check, because those two get confused constantly.
The square you cross
The last one is the sneaky one: a bishop on b5 or a6, a knight on e3, a rook that just arrived on the f-file. If it attacks f1 or g1, short castling is off. If it attacks d1 or c1, long castling is off. And nobody announces it.
So before you castle, look once along the diagonals that end on f1 and d1. That check takes a second and it saves the embarrassment of reaching for two pieces and having to put them back. If you want to train the eye, the exposed king puzzles are full of positions where one side never got round to castling and paid for it.