3

I have one big image as a background to my webpage. The image contains a box inside the image itself. How would I place text on that background image such that it should fit in the box, and shrink or resize accordingly (in other resolutions when the background resizes)?

Tetsujin no Oni
  • 7,300
  • 2
  • 29
  • 46
simple-coder
  • 33
  • 1
  • 5

3 Answers3

3

If you're looking to resize the "box" containing the text, you should be able to set the dimensions of the element to percentage-based width and height values with CSS.

If you want to resize the text inside the element, then you might want to consider using JavaScript (perhaps jQuery) to poll the size of the window at set intervals and adjust the text size based on the new window dimensions.

Edit: To clarify, you should be able to set the dimensions of the text box (probably a div) to be a percentage of the page. For example, the div containing the text could be 80% of the window width and 80% of its height. You can then set the margin to be "auto". This should cause the margin around the box and the dimensions to be proportional to the window width.

Example:

 <style type="text/css">
    div#box {
    height: 80%;
    width: 80%;
    margin: auto;
    }
</style>
<div id="box">Text goes here.</div>

This will cause the "box" div to be centered horizontally on the page, but vertical centering is a bit trickier. You'll probably want to look at this page to figure out how to center it vertically to stay within the box in the background.

As suggested by the other individual, you could also make the box background just the background of the text's container and not the entire page background. This might be a bit easier, but I think you will still need to use the percentage-based width and height attributes and auto margin to center it nicely.

Kyle
  • 2,822
  • 2
  • 19
  • 24
  • thanks kyle for ur answer.but i dont need to resize the bg image or the box.i have taken care of that bg image already.just i want to put text on some part of that bg image.(that part is a fancy box).so the text shouldnt go outside of the box.it should get resized.and that too its a static content – simple-coder Dec 29 '10 at 06:56
  • @simple-coder: Please see my edit. It might help you further. – Kyle Dec 30 '10 at 00:56
2

For starters, you can't resize a background image. Also, resizing text will need Javascript or a page refresh.

Try making an example at http://www.jsfiddle.net so people better see what you're describing.

UPDATE

Your question is still unclear and I strongly recommend jsfiddle. But if I've interpreted correctly...you're using FancyBox, which suggests you've got some Javascript running your page. Javascript can be used to find if your text is overflowing the container, and can resize it accordingly.

To do this, get your <div> (or container element) and check its .scrollHeight and .clientHeight properties. If the scroll is less than the client, the text doesn't need to be resized. If scroll is larger than the client, you can resize with the .style.fontSize property.

An untested example of what I'm describing is like this:

myDiv = $('containerElement'); // Get container object using its ID
size = 50;  // Start with 50px font size

while(myDiv.scrollHeight > myDiv.clientHeight) {
  // Decrement font size until scroll is less than client
  myDiv.style.fontSize = (size - 1) + 'px';
}

You'll have to do a little legwork on this to get it to work how you like. Things to note:

  • I used the dollar function to get an object, you can google it for more info
  • Your container must have defined dimensions for .clientHeight to find
  • You may need to try .offsetHeight instead of .clientHeight

If you're just looking to control overflow, you can use CSS:

  • overflow-x:hidden or scroll or auto, overflow-y is the same
  • white-space:nowrap will prevent auto text wrapping

But, once again, my answer is vague since it's not clear (with code) what you're asking.

Ben
  • 54,723
  • 49
  • 178
  • 224
  • thanks steve for ur answer.but i dont need to resize the bg image.i have taken care of that bg image already.just i want to put text on some part of that bg image.(that part is a fancy box).so the text shouldnt go outside of the box.it should get resized.and that too its a static content – simple-coder Dec 29 '10 at 06:54
  • hi steve sorry i confused u with the word "fancy box".there is no such js here.its a simple webpage with a background that contains a rectangular box in middle(the whole thing is done in photshop).now i gave width:100% to that so that it fits to screen.now i want to place some text that should fit inside the box.As an alternative i will extract that box from the bg image and place that as a div on main image.now how can i approach to scale the text in this new div?? – simple-coder Dec 29 '10 at 09:14
1

The problem with your solution is that it is very unscalable, not friendly to different browsers and will cause more problems as your website expands.

Try separating the box from the other bg image and use the box image as a background for the div you have the text in.

red-X
  • 5,108
  • 1
  • 25
  • 38
  • your idea seems to be good red-X.ok accroding to you i will split my current bg image to two.one the picture one and the other the box.so now i want to place this box on that bg picture as a div.so can u suggest me how to position this box along with text in it on that main bg image(resizable)?? – simple-coder Dec 29 '10 at 09:12