How do I get an image to stay centered when its fluid width (percentage based) container is too small to show the whole image?

How can I center the image inside it's container
This means it should show the middle of the image instead of the sides when container is too small.
When It Works
You might have a container that holds some content such as two
<div>s that are 50% wide each, sitting next to each other. For this example, we can illustrate just one child of the container:We'll name outer rectangle
.container, the inner rectangle.contentand the imageimg. This arrangement is perfectly fine, as long as.contentis always wider thanimg.When It Breaks
Since we're dealing with percentages and probably working with a responsive design, this may not always be the case. If
.contentis ever thinner thanimg, cropping will occur:The Problem
If the most interesting part of
imgis in the center, we need to get the browser to crop both edges evenly - leaving the best part of it visible, no matter what width of.contentis.The Solution
Fortunately, a solution is possible. Even better, no extra markup is required.