I'd like to specify the width of a div element to be the width of a child img element contained within. However, this width is implicit depending on the size of the source image.
The first flex item shows the effect I'm trying to achieve, however, I've had to manually set the value of the width to 150px. How can this be achieved dynamically (preferably without javascript)?
<!DOCTYPE html>
<html>
<head>
<style>
.flex-item {
border: 1px solid #30b0b0;
color: #303030;
text-align: center;
padding: 5px;
}
.flex-container {
border: 1px solid #30b0b0;
display: flex;
justify-content: space-around;
list-style: outside none none;
padding: 5px;
}
</style>
</head>
<body>
<ul class="flex-container">
<li class="flex-item">
<div style="width: 150px; margin: auto;">
<img height="100px" src="http://upload.wikimedia.org/wikipedia/commons/thumb/a/aa/Metrostation_Mladost_1.JPG/150px-Metrostation_Mladost_1.JPG">
<p>This text stays within the image</p>
</div>
</li>
<li class="flex-item">
<div>
<img height="100px" src="http://upload.wikimedia.org/wikipedia/commons/thumb/a/aa/Metrostation_Mladost_1.JPG/150px-Metrostation_Mladost_1.JPG">
<p>This text sticks out further than the image</p>
</div>
</li>
</ul>
</body>
</html>