I have a block of text that goes on top of a picture. Then below this there is a footer. But the text comes from a database, so sometimes the text is taller than the picture and sometimes shorter. I want the footer to come below the "whole thing" in either case.
Like this -- imagine the XXX's are the picture
Scenario 1:
XXXXXXXXXXXXXXXX
XXXX Short XXXX
XXXX text XXXX
XXXXXXXXXXXXXXXX
-- Footer --
Scenario 2:
XXXXXXXXXXXXXXXX
XXXX Long XXXX
XXXX text XXXX
XXXX runs XXXX
past
the
picture.
-- Footer --
It's easy enough to put the text on top of the picture with position: absolute for one or the other. But then the footer gets positioned without regard to the absolute element.
At the moment I've got two different versions of the screen, one where the picture is static and the text is absolute, for cases where I expect the text to be longer; and one where the text is static and the picture is absolute, for cases where the text is longer. This works, but only because I know what data is in the database today. I could have the program examine the text, but I have no way to know how tall it will lay out without knowing the size of the window the user sets for his browser, not to mention font sizes, etc.
Maybe position: absolute isn't the right way to do this?
Update *
Someone suggested I make a fiddle. I was about to, but I see Adam B Smith made one that illustrates my problem very well: http://jsfiddle.net/DIRTY_SMITH/EgLKV/6183/
That fiddle looks great if the text is taller than the image. Now delete a bunch of text so that the text is shorter than the image, and you see the footer overlaps the image.
OK this one will do it for you http://jsfiddle.net/DIRTY_SMITH/EgLKV/6185/
lol
#container{min-height: 400px;}
#image
{
position:absolute;
z-index:-9999;
left:0;
top:0;
}
#text
{
z-index:9999;
width: 200px;
color:red;
font-size:24px;
font-weight:bold;
}
.footer {
background:#ffab62;
width:100%;
height:100px;
z-index:9999;
bottom:0;
left:0;
}
If you know the size of the image, and set the container's size same as the image, it does work.
.container {
border: 1px solid red;
position: relative;
display: table;
width: 250px;
height: 193px;
}
.container img {
position: absolute;
z-index: -1;
}
.container span {
background: rgba(255,255,255,.7);
display: table;
width: 80%;
margin: auto;
margin-top: 25%;
margin-bottom: 5%;
}
.footer {
background: pink;
}
<div class="container">
<img src="http://albanyvisitors.com/WpContents/wp-content/uploads/2015/06/200px-Big-Lake-Big-Sky-Mt-Washington-by-Bill-Origer-2015-photo-contest.jpg" />
<span>Lorem ipsum dolor sit amet, consectetur adipiscing elit</span>
</div>
<div class="footer">foooooooter</div>
<br>
<div class="container">
<img src="http://albanyvisitors.com/WpContents/wp-content/uploads/2015/06/200px-Big-Lake-Big-Sky-Mt-Washington-by-Bill-Origer-2015-photo-contest.jpg" />
<span>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</span>
</div>
<div class="footer">foooooooter</div>
Related
For months this is the only problem I haven't been able to solve. I've hired multiple people on Freelancer to help and two gave up, and another said it was impossible.
I'm simply trying to get the text of my buttons to stay within the boundaries of the button. Right now it looks like this.
All I need to do is wrap the text if it's too long to fit the horizontal boundaries of the button, and shrink the text if it's too big to fit within the boundaries of the button. Like this:
I've tried:
Using Fitty, FitText, and other libraries which don't work at all. They'll sometimes make the text too big to fit within the boundaries of the button, and sometimes they'll make all of my text small unnecessarily.
Creating my own function by looking at clientWidth and clientHeight, and shrinking the font as necessary. When I do that, clientWidth of my elements stay the same regardless of the actual size, I've also used getComputedStyle which doesn't seem to calculate properly either.
Paying people. Again multiple people have given up, and I've spent months trying to solve this with no success.
I've created a codepen with a minimally reproducible example showing the problem.
https://codepen.io/TheNomadicAspie/pen/dyRLrej
And here is the code (I've removed all of the unnecessary code, but left in the parent divs of the buttons in case they are affecting whatever is keeping the libraries/my functions/other people from being able to do this).
<div id="screen" class="screen">
<div id="display" class="display">
<div id="bottom_bar" class="bottom-bar">
<div id="bottom_display" class="bottom-display">
<div id="answers_display" class="answers-display">
<div id="answer_container_1" class="answer-button-1">
<div id="answer_checkbox_1" class="checkbox">
</div>
<div id="answer_button_container_1" class="answer-button-container">
<button id="answer_button_1" class="button lower-button">
</button>
</div>
</div>
<div id="answer_container_2" class="answer-button-2">
<div id="answer_checkbox_2" class="checkbox">
</div>
<div id="answer_button_container_2" class="answer-button-container">
<button id="answer_button_2" class="button lower-button">
</button>
</div>
</div>
<div id="answer_container_3" class="answer-button-3">
<div id="answer_checkbox_3" class="checkbox">
</div>
<div id="answer_button_container_3" class="answer-button-container">
<button id="answer_button_3" class="button lower-button">
</button>
</div>
</div>
<div id="answer_container_4" class="answer-button-4">
<div id="answer_checkbox_4" class="checkbox">
</div>
<div id="answer_button_container_4" class="answer-button-container">
<button id="answer_button_4" class="button lower-button">
</button>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<link href="styles.css" rel="stylesheet">
* {
outline: none;
opacity: 1;
-webkit-box-sizing: border-box;
box-sizing: border-box;
}
html,
body {
position: fixed;
height: 100%;
background-color: #26004b;
font-size: 2vh;
margin: 0 auto;
font-family: open_sans;
}
.screen {
position: absolute;
height: 100%;
}
.title {
grid-column: 2/3;
position: relative;
color: #f5f5f5;
height: 100%;
width: 100%;
margin: 0 auto;
text-align: center;
justify-content: center;
align-items: center;
font-family: hack;
font-size: clamp(2vw, 8vw, 10vh);
display: flex;
top: 0%;
}
.display {
position: relative;
height: 86.286%;
width: 100vw;
}
.bottom-bar {
display: grid;
grid-template-columns: 38.2% 61.8%;
position: relative;
height: 38.2%;
width: 100vw;
bottom: 0%;
}
.character {
grid-column: 1/2;
position: relative;
background-size: contain;
background-repeat: no-repeat;
background-position-y: bottom;
background-position-x: center;
}
.bottom-display {
grid-column: 2/3;
position: relative;
height: 100%;
padding-right: 5vw;
padding-top: 1%;
padding-bottom: 3%;
}
.answers-display {
display: grid;
gap: 1%;
max-height: 99%;
grid-template-columns: 100%;
grid-template-rows: 25% 25% 25% 25%;
height: 100%;
}
.answer-button-1 {
position: relative;
grid-row: 1/2;
display: grid;
grid-template-columns: 20% 80%;
height: 98%;
}
.answer-button-2 {
position: relative;
grid-row: 2/3;
display: grid;
grid-template-columns: 20% 80%;
height: 98%;
}
.answer-button-3 {
position: relative;
grid-row: 3/4;
display: grid;
grid-template-columns: 20% 80%;
height: 99%;
}
.answer-button-4 {
position: relative;
grid-row: 4/5;
display: grid;
grid-template-columns: 20% 80%;
height: 99%;
width: 100%;
}
.checkbox {
grid-column: 1/2;
height: 100%;
width: 100%;
overflow: hidden;
background-size: contain;
background-repeat: no-repeat;
background-position: center center;
object-fit: contain;
}
.answer-button-container {
grid-column: 2/3;
padding-left: 2%;
height: 100%;
}
.answer-button-container button {
width: 100%;
padding-left: 1%;
padding-right: 1%;
padding-top: 2%;
padding-bottom: 2%;
}
.button {
display: block;
position: relative;
height: 100%;
width: 100%;
background-color: black; /*Button Color*/
color: #f5f5f5;
font-family: open_sans;
font-size: 1.5rem;
border-radius: 20px;
text-decoration: none;
box-shadow: 0.1em 0.2em black;
cursor: pointer;
}
.lower-button {
white-space: nowrap;
}
const answer_button_1 = document.getElementById("answer_button_1");
const answer_button_2 = document.getElementById("answer_button_2");
const answer_button_3 = document.getElementById("answer_button_3");
const answer_button_4 = document.getElementById("answer_button_4");
answer_button_1.innerText = "This is a really really really really really really really really really really really really really really really really really really really really really long test answer";
answer_button_2.innerText = "This is a pretty long test answer but not as long as the other one";
answer_button_3.innerText = "This is a fairly short test answer";
answer_button_4.innerText = "Really short answer";
Edit: To clarify, I need the button to not get larger to fit the text, I need the text to get smaller to fit the button.
This here does the job by wrapping the text and making the box larger, it does however mess up the spacing between the boxes. Took me a few minutes, hope it helps :)
#answer_button_1 {
height: auto;
max-width:30wv;
hyphens: auto;
white-space: normal;
}
The Spacing between the buttons can be fixed by removing the following lines from .answers-display:
grid-template-rows: 25% 25% 25% 25%;
height: 100%;
Hmmmm. Yes, this is a tricky one. Using this article as inspiration, I was able to come up with the following solution. The technique used is to start with a really small font size (I have set minSize to 8 for this example) and test whether the text overflows its container; if the text does not overflow, increase the font size by a small amount (I've set step to 0.5) and re-test; then if the text overflows, revert to the previous font size.
Note, however, that it uses regular divs rather than buttons. The solution relies on a set of nested elements, which the button element does not support. Buttons also seem to have some built-in padding or sizing which is difficult to control. I suspect that Fitty and FitText don't work on buttons. I did try swapping the innermost divs with buttons in this snippet, and while it still works fairly well, it's more complicated and doesn't look as good. The only reason to prefer a button over a div is purely a semantic one, so I'd recommend sticking to using divs. Just add your click handler and off you go.
You can try different values for minSize, step and so on to see how that affects the result. Note that because I have used a minSize of 8, there comes a point where very long texts still overflow the button. Setting minSize to 0 avoids this -- the text fits on the button regardless of its length, but for some reason the text doesn't quite fill the button: the bottom padding appears larger. But your results may vary.
const isOverflown = ({ clientHeight, scrollHeight }) => scrollHeight > clientHeight
const resizeText = ({ element, elements, minSize = 10, maxSize = 512, step = 1, unit = 'px' }) => {
(elements || [element]).forEach(el => {
let i = minSize
let overflow = false
const parent = el.parentNode
while (!overflow && i < maxSize) {
el.style.fontSize = `${i}${unit}`
overflow = isOverflown(parent)
if (!overflow) i += step
}
// revert to last state where no overflow happened
el.style.fontSize = `${i - step}${unit}`
})
}
resizeText({
elements: document.querySelectorAll('.button>div>div'),
minSize: 8,
step: 0.5
})
body {
background: #33A;
font-family: sans-serif;
}
.button {
margin: 1em 0;
width: 300px;
height: 50px;
padding: 10px 10px 10px 15px;
color: #f5f5f5;
background-color: black;
border: 1px outset;
border-radius: 20px;
box-shadow: 0.1em 0.2em black;
cursor: pointer;
text-align: center;
}
.button>div {
width: 100%;
height: 100%;
}
<div class="button">
<div>
<div>
This text
</div>
</div>
</div>
<div class="button">
<div>
<div>
This Text is a bit longer
and should be wrapped correctly
</div>
</div>
</div>
<div class="button">
<div>
<div>
This text is the longest and should appear quite small.
This text is the longest and should appear quite small.
</div>
</div>
</div>
<div class="button">
<div>
<div>
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
</div>
</div>
</div>
<div class="button">
<div>
<div>
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</div>
</div>
</div>
<div class="button">
<div>
<div>
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</div>
</div>
</div>
It's a real pity that web standards do not currently provide a mechanism for automatically adjusting font size to fill a fixed size container. There are mechanisms to do it with images, so why not with text? I myself have had regular situations over the years where I have wished this were possible.
I am curious, though, as to why it is so important for you for the buttons to be a fixed size? Doesn't readability become a problem when there's a lot of text, and the font-size gets so small? Would it not be a better solution to simply allow the buttons to grow vertically to contain longer pieces of text? Or even truncate the text at a maximum number of characters or words, and add an ellipsis to indicate that truncation occurred?
So basically I'm trying to have a fixed box that is always the same size on screen but if something is wider than the box it would get trunicated to the next line. But instead it keeps making the box start scrolling horizontally which I absolutly do NOT want to ever happen.
Inside the scrollbox could be <div> <p> <h1-6> <hr> <img> or <a> tags and I want it to only scroll vertically and I want anything that goes off the right side to get moved to the next line for any sub element.
But right now it just scrolls horizontally and vertically.
the ${html} being any given string of html to be put in
<div class="col-md-10">
<div class="Holder">
<pre>
${html}
</pre>
</div>
</div>
p{
overflow-wrap:normal;
}
pre{
overflow-x: auto;
border:0;
background-color:transparent;
}
.Holder{
display: flex;
flex-direction: column;
height: calc(100vh - 140px);
}
Any way to get this thing to do as I intend it to?
Note: I'm currently using v3.4.1 of bootstrap.min.css with the above CSS being loaded after.
I want it to only scroll vertically and I want anything that goes off
the right side to get moved to the next line for any sub element.
If I understand this correctly, which means you want to wrap the elements into next line, you can do that with white-space
Try this
p{
overflow-wrap:normal;
}
pre{
overflow-x: auto;
border:0;
white-space: inherit;
background-color:transparent;
}
.Holder{
display: flex;
flex-direction: column;
height: calc(100vh - 140px);
}
.Holder {max-width: 500px; max-height: 150px; overflow-y: auto;}
<div class="col-md-10">
<div class="Holder">
<pre>
It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).
</pre>
</div>
</div>
NOTE Here I have set max-height and max-width for the .holder just for you to see the vertical scrolling, you can remove that later.
Try white-space property to pre tag.
pre {
overflow-x: auto;
border: 0;
background-color: transparent;
height: 100%;
white-space: break-spaces;
}
.Holder {
display: flex;
flex-direction: column;
width: 300px;
height: calc(100vh - 140px);
}
<div class="wrapper">
<div class="Holder">
<pre>
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</pre>
</div>
</div>
Check demo here →→ https://jsfiddle.net/Divya_Patel/hL5we9cm/4/
So I have a div, whose height I don't know initially. It has two children - an <img> on the left, and a <div> contains some content on the right. The image size is fixed, say 50 by 50. However, the content can be of any size. It may be even one line, in which case it becomes smaller in size compared to the photo. Either way, the parent div has to find out which one is bigger and grow vertically to accommodate both, while ensuring that both are properly centered (taking into account a vertical padding of 5 px).
Also imagine that the content won't wrap below the image, i.e it is a standalone element on the right side (as if floating right).
That is, if the content is say just one line, then the parent div should be of height 50+5*2 = 60px, the img on the left being centred at 5px from top, the content on the right (assume at a margin of 5 px from the left), which here is just one line, centred vertically.
Or say the content is so big that it is 100px high, then the parent div will be 100+5*2 = 110px high. The img will be on the left, at 30px from the top.
Can anyone help me fix the issue?
This is what I came up with: https://jsfiddle.net/fj77eobe/
.elem-option {
text-align: left;
white-space: nowrap;
width: 300px;
background: pink;
}
/* The ghost, nudged to maintain perfect centering */
.elem-option:before {
content: '';
display: inline-block;
height: 100%;
vertical-align: middle;
margin-right: -0.25em;
}
/* The element to be centered, can also be of any width and height */
.elem-photo {
display: inline-block;
vertical-align: middle;
width: 40px;
height: 40px;
margin-left: 10px;
}
.elem-content {
display: inline-block;
margin-left: 5px;
width: 100px;
word-wrap: break-word;
overflow: none;
}
<div class='elem-option'>
<img src="https://img.zmtcdn.com/data/reviews_photos/b22/28f633be81fd340785c3af7f7858cb22_1463913069.jpg" class="elem-photo" />
<div class='elem-content'>
Amader ekhane ekjon er heavy khar chhilo...take paedo bole khepano hoto..toh se ajkal ei defense deyAmader ekhane ekjon er heavy khar chhilo...take paedo bole khepano hoto..toh se ajkal ei defense deyAmader ekhane ekjon er heavy khar chhilo...take paedo
bole khepano hoto..toh se ajkal ei defense deyAmader ekhane ekjon er heavy khar chhilo...take paedo bole khepano hoto..toh se ajkal ei defense dey
</div>
</div>
Try setting the container to display: flex; + align-items: center;
.elem-option {
display: flex;
align-items: center;
padding: 5px;
background: pink;
}
.elem-photo {
margin-right: 5px;
flex: 0 0 50px;
height: 50px;
}
<div class='elem-option'>
<img src="//unsplash.it/200" class="elem-photo" />
<div class='elem-content'>
Lorem ipsum dolor sit amet.
</div>
</div>
<br>
<div class='elem-option'>
<img src="//unsplash.it/200" class="elem-photo" />
<div class='elem-content'>
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident.
</div>
</div>
I'm looking for a way in CSS to place a symbol into the margin of the document to highlight/indicate the position of some special phrase in the text body of the document. Think of the usual text-editors in programming IDEs that place little warning icons in the margin next to lines that contain errors.
This is easy to do if the document consists of non-wrapped single lines. Then I can just check if the line needs the symbol and place it manually.
But it gets tricky if I want to, for example, place an icon for spelling mistakes in a document where the browser automatically breaks the lines. Then I would have to have a way to figure out which line the spelling mistake ended up in. This is probably also possible with JS by checking the y-coordinate of some wrapper-span that marks the spelling mistake, but I'm looking for something more elegant.
Is there some trick with float-left or absolute positioning that allows me to, for example, put this marker symbol into the span that marks the error and have it be placed in the left margin of the document instead of inside the boundaries of the span?
Actually, the answer is exactly as you described. Have spans wrapping your text, and inside the span, include an icon element. Then float it left, and set a negative margin on it. Example:
CSS:
.icon {
display: inline-block;
width: 10px;
height: 10px;
background: blue;
float: left;
margin-left: -15px;
margin-top: 5px;
}
Markup:
<span class="selected"><span class="icon"></span>this is some text in a span. </span>
Working example: http://jsfiddle.net/FQCsn/
I think there's also an application for the position: absolute in the context of the :before pseudoelement. Try this and see if it gives you what you're looking for:
<html>
<head>
<title>Lorem Ipsum</title>
<style>
.allowLeftMargin
{
margin-left: 5em;
}
.highlightThis
{
background-color: yellow;
}
.highlightThis:before
{
background-color: yellow;
content: "Note";
padding-left: 0.25em;
padding-right: 0.25em;
position: absolute;
left: 1em;
}
</style>
</head>
<body>
<div class="allowLeftMargin">
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit,
sed do eiusmod tempor incididunt ut labore et dolore magna
aliqua. Ut enim ad minim veniam, quis nostrud exercitation
ullamco laboris nisi ut aliquip ex ea commodo consequat.
Duis aute irure dolor in reprehenderit in voluptate velit
esse cillum dolore eu fugiat nulla pariatur.
<span class="highlightThis">Excepteur sint occaecat</span>
cupidatat non proident, sunt in culpa qui officia deserunt
mollit anim id est laborum.</p>
</div>
</body>
</html>
You can quickly adjust the size of the browser window to confirm that the note moves with the highlighted span.
What you can do is put a strong around the spelling error, add another tag (a span for example) right after that spelling error, and set that span in position: absolute, but without the "top" property (because the top position is variable). Put that span in width: 100% in order to "select" the line, and add another tag inside that span (a i tag for convenience), and use it to put your icon.
p{ line-height:20px; margin:20px;}
strong{ color:red;}
span{ display:block; height:20px; left:0; position:absolute; width:100%;}
i{ background:red; display:block; height:12px; left:0; position:absolute;
top:-16px; width:12px;}
Example: http://jsfiddle.net/fwZqv/1/
Try to change the width of the "Result" window and see how it behaves.
It's not a perfect solution, and I would rather use JS for that matter.
As far as I can tell, it is not possible to place a CSS background image 1em from the right border of any block, neither is it possible to place a image 1em from the bottom.
The following code places the background image 1em from the left and 2em from the top.
<div class="foo" style="background: url('bar.png') no-repeat 1em 2em">
Some text here
</div>
Is there any way in CSS to specify that the background image should be "this far from the right edge" if the size of the box is dynamic and assuming that you cannot change the HTML?
(Percentages won't work, since the box can change size)
If this is not possible, what is the smallest amount of change you need to make to the HTML?
This is the workaround I came up with:
<style>
div.background
{
float: right;
background: url('bar.png') no-repeat top left;
margin-right: 1em;
width: 16px;
height: 16px;
}
</style>
<div class="foo">
<div class="background" style=""> </div>
Some text here
</div>
The CSS3 background-position spec allows you to change the anchor point from the top left to anything you want. For example, the following will set the lower bottom corner of the image 1em from the right and 2px from the bottom:
background-position: right 1em bottom 2px;
Confirmed to work in:
IE9/10, Firefox 13+, Chrome 26+, Opera 11+, Seamonkey 2.14+, Lunascape 6.8.0
As of April 2013, only IE6-8 and some fringe browsers lack support.
Here's a test page: http://jsbin.com/osojuz/1/edit
Elements with position: absolute; can be positioned by their right edge.
So, if you don't mind a minor change to the html, do this:
<div id="the-box">
<img id="the-box-bg" src="bar.png" />
Text text text text....
</div>
(...)
#the-box {
position: relative;
}
#the-box-bg {
position: absolute;
right: 1em;
z-index: -1;
}
You could of course also use absolute positioning of a second div, with a repeating background. But then you would have to set the size of the (inner) div in CSS.
You could try something like this:
<style type="text/css" media="screen">
#outer {
position: relative;
top: -1em;
left: -1em;
margin: 1em 0 0 1em;
outline: thin solid #F00;
background: url(http://i.stackoverflow.com/Content/Img/stackoverflow-logo-250.png) no-repeat 100% 100%;
}
#inner {
outline: thin solid #0F0;
position: relative;
top: 1em;
left: 1em;
}
</style>
<div id="outer">
<div id="inner">
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</div>
</div>
Edit: Looking forward to CSS 3 background-position.
After some research the actual x pixel length of the background position is always counted from the left side of the element. The only way to make this work (without using other elements) would be to use javascript, calculate the left length given the elements width:
var rightMargin = "10"; // in pixels
var imageWidth = "16";
var left = element.style.clientWidth - imageWidth - rightMargin;
element.style.backgroundPosition = "0px " + left + "px";