Note: Even though this is the accepted answer, the answer below is more accurate and is currently supported in all browsers if you have the option of using a background image.
No, there is no CSS only way to do this in both directions. You could add
.fillwidth {
min-width: 100%;
height: auto;
}
To the an element to always have it 100% width and automatically scale the height to the aspect ratio, or the inverse:
.fillheight {
min-height: 100%;
width: auto;
}
to always scale to max height and relative width. To do both, you will need to determine if the aspect ratio is higher or lower than it's container, and CSS can't do this.
The reason is that CSS does not know what the page looks like. It sets rules beforehand, but only after that it is that the elements get rendered and you know exactly what sizes and ratios you're dealing with. The only way to detect that is with JavaScript.
Although you're not looking for a JS solution I'll add one anyway if someone might need it. The easiest way to handle this with JavaScript is to add a class based on the difference in ratio. If the width-to-height ratio of the box is greater than that of the image, add the class "fillwidth", else add the class "fillheight".
$('div').each(function() {_x000D_
var fillClass = ($(this).height() > $(this).width()) _x000D_
? 'fillheight'_x000D_
: 'fillwidth';_x000D_
$(this).find('img').addClass(fillClass);_x000D_
});
_x000D_
.fillwidth { _x000D_
width: 100%; _x000D_
height: auto; _x000D_
}_x000D_
.fillheight { _x000D_
height: 100%; _x000D_
width: auto; _x000D_
}_x000D_
_x000D_
div {_x000D_
border: 1px solid black;_x000D_
overflow: hidden;_x000D_
}_x000D_
_x000D_
.tower {_x000D_
width: 100px;_x000D_
height: 200px;_x000D_
}_x000D_
_x000D_
.trailer {_x000D_
width: 200px;_x000D_
height: 100px;_x000D_
}
_x000D_
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>_x000D_
<div class="tower">_x000D_
<img src="http://placekitten.com/150/150" />_x000D_
</div>_x000D_
<div class="trailer">_x000D_
<img src="http://placekitten.com/150/150" />_x000D_
</div>
_x000D_