If you're going to use jeepin's solution, I recommend the following changes
<style>
div.variable-line{
position:relative;
border-bottom:1px solid #CCCCCC;
text-align:left;
height: 1em;
margin: 0;
padding: 0;
}
div.variable-line-text{
position:relative;
top: -1em;
margin-top: 1px;
background: white;
display:inline-block;
padding-right:15px;
}
</style>
<div class="variable-line">
</div>
<div class="variable-line-text">Test 1</div>
<br/>
<div class="variable-line">
</div>
<div class="variable-line-text">Test 2 Test 2 Test 2 Test 2</div>
In short, keep the div creating the line at full height, its border at the bottom and the div containing the text outside and following the line element. That way, you can move the text up one full em (and move it down by one px to make it look better) without pushing the text above the block containing the line. It will be fully inside it, so that you do not screw up positioning against other elements.
Derokorian;11010275 wrote:
However, when I change the text the hr stays the same size (obviously) and I can't figure out how to make it always say 25px away from the text. Any thoughts?
As for your particular question, this is not possible to solve with CSS as far as I understand things. When you float something, you break it outside of the flow of content around it, which means that it no longer recognizes widths of the stuff around it so that it can fill out remaining space, or if you invert what things you float, the content around it does not recognize its width so that it can fill out the rest.
You can however solve this in javascript, taking the width of the containing element, space used by the text container and calculate the remaining space needed for the line.
<style>
div.text-line-container {
width: 400px;
line-height: 1.4em;
}
span.variable-line-text {
font-size: 1em;
padding-right: 1.25em;
/* If you add even 1px margin, the line will no longer fit
unless you also calculate width of the margins for this element. */
margin: 0;
}
div.variable-line {
border-bottom: 1px solid #CCCCCC;
height: 1em;
float: right;
}
.clear-right {
clear: right;
}
</style>
<div class="text-line-container">
<div class="variable-line"></div>
<span class="variable-line-text">
Text
</span><br class="clear-right">
<div class="variable-line"></div>
<span class="variable-line-text">
Longer Text
</span><br class="clear-right">
</div>
<script>
var w = $('div.text-line-container').width();
$('span.variable-line-text').each(function(i, e) {
/* Take care when computing remaining space. outerWidth does include
padding and border, but not margin.
*/
var remaining = w - $(e).outerWidth() - 1;
$(e).prev('div.variable-line').css('width', remaining);
});
</script>