given the following code
<!DOCTYPE html>
<html>
<head>
<title>show stats</title>
<style>
span.main {
position:relative;
display:inline-block;
}
div.stats {
position:absolute;
bottom:50px;
background:black;
color:white;
padding:0px 15px 0px 15px;
font-weight:500;
}
</style>
</head>
<body>
<span class="main">
<img src="http://i.imgur.com/o2udo.jpg" alt="test" title="testpic"/>
<div class="stats">
<p>this is a caption</p>
</div>
</span>
<span class="main">
<img src="http://i.imgur.com/o2udo.jpg" alt="test" title="testpic"/>
<div class="stats">
<p>this is a caption</p>
</div>
</span>
<span class="main">
<img src="http://i.imgur.com/o2udo.jpg" alt="test" title="testpic"/>
<div class="stats">
<p>this is a caption</p>
</div>
</span>
</body>
</html>
I want to make div.stats the width of the containing span.main. Setting the width to 100% doesn’t work, because the resulting width exceeds the width of the container by the amount of the padding (30px).
I can fix it by setting the padding of div.stats to 0 and then padding the paragraph inside this div. This seems to work, but I suspect the way I’m doing it is dumb. Is there a cleaner way to position this div across the width of its container, while maintaining its vertical position?
Add
left:0andright: 0todiv.stats:JSFiddle: http://jsfiddle.net/sT9vA/1/