文本适合我网页中的框内

发布于 2024-10-09 12:43:51 字数 86 浏览 4 评论 0原文

我有一张大图像作为我的网页的背景。该图像本身包含一个框。我如何将文本放置在该背景图像上,使其适合框中,并相应地缩小或调整大小(当背景调整大小时以其他分辨率)?

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)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

自此以后,行同陌路 2024-10-16 12:43:51

如果您想要调整包含文本的“框”的大小,您应该能够使用 CSS 将元素的尺寸设置为基于百分比的宽度和高度值。

如果您想调整元素内文本的大小,那么您可能需要考虑使用 JavaScript(可能是 jQuery)以设定的时间间隔轮询窗口的大小,并根据新的窗口尺寸调整文本大小。

编辑:为了澄清,您应该能够将文本框(可能是 div)的尺寸设置为页面的百分比。例如,包含文本的 div 可以是窗口宽度的 80% 和高度的 80%。然后您可以将边距设置为“自动”。这应该会导致框周围的边距和尺寸与窗口宽度成比例。

示例:

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

这将导致“box”div 在页面上水平居中,但垂直居中有点棘手。您可能需要查看此页面来弄清楚如何将其垂直居中以保持在背景中的框内。

正如其他人所建议的,您还可以将框背景设置为文本容器的背景,而不是整个页面背景。这可能会更容易一些,但我认为您仍然需要使用基于百分比的宽度和高度属性以及自动边距来很好地居中。

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.

身边 2024-10-16 12:43:51

首先,您无法调整背景图像的大小。此外,调整文本大小将需要 JavaScript 或页面刷新。

尝试在 http://www.jsfiddle.net 上举一个例子,以便人们更好地了解您所描述的内容。

更新

你的问题仍然不清楚,我强烈推荐jsfiddle。但如果我的解释正确的话...您正在使用 FancyBox,这表明您已经有一些 Javascript 正在运行您的页面。 Javascript 可用于查找文本是否溢出容器,并可以相应地调整其大小。

为此,请获取

(或容器元素)并检查其 .scrollHeight.clientHeight 属性。如果滚动小于客户端,则不需要调整文本大小。如果滚动大于客户端,您可以使用 .style.fontSize 属性调整大小。

我所描述的一个未经测试的例子是这样的:

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';
}

你必须为此做一些跑腿工作才能让它按照你喜欢的方式工作。需要注意的是:

  • 我使用了dollar函数来获取一个对象,你可以google它来获取更多信息
  • 你的容器必须为.clientHeight定义尺寸才能找到
  • 你可能需要尝试.offsetHeight< /code> 而不是 .clientHeight

如果您只是想控制溢出,可以使用 CSS:

  • overflow-x:hidden 或滚动或自动,overflow -ywhite-space:nowrap 相同
  • ,将阻止自动文本换行

但是,我的答案再次含糊不清,因为不清楚(用代码)您要问什么。

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.

风透绣罗衣 2024-10-16 12:43:51

您的解决方案的问题在于它非常不可扩展,对不同的浏览器不友好,并且随着您的网站扩展会导致更多问题。

尝试将框与其他背景图像分开,并使用框图像作为包含文本的 div 的背景。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文