Tampering around with Firebug I found the property value -moz-fit-content
which exactly does what the OP wanted and could be used as follow:
width: -moz-fit-content;
Although it only works on Firefox, I couldn't find any equivalent for other browsers such as Chrome.