简体   繁体   English

如何用“ background-size:cover”修复文本?

[英]How to fix a text with a “background-size: cover”?

So, i used this code for my div background: 因此,我将此代码用于div背景:

.bg{
background: url(http://cl.ly/image/1n0I2G3K231f/bg.jpg) no-repeat center center fixed;
  -webkit-background-size: cover;
  -moz-background-size: cover;
  -o-background-size: cover;
  background-size: cover;
   height: 100%;
}

Thing is, I want to put a text in the page that stays in a certain area of the background image. 问题是,我想在页面中放置一个文本,该文本保留在背景图像的特定区域中。 The problem is that the background image adapts the users screen size (as it was supposed to), and this makes the text to stay in different positions in the image depending on the user's screen size. 问题在于,背景图像会适应用户的屏幕尺寸(如预期的那样),这会使文本根据用户的屏幕尺寸停留在图像中的不同位置。

http://ftestes.ueuo.com/help/test.html http://ftestes.ueuo.com/help/test.html

How can i accomplish this? 我怎样才能做到这一点?

I would try to give the actual container (the one with the background image): 我会尝试给出实际的容器(带有背景图片的容器):

position: relative;

And then give you text a class/id, and give it this: 然后给您文本一个类/ ID,并为此:

position: absolute;
top: 5%; /*these values depend on where you want the text*/
left: 5%;/*you can also use bottom or right if needed*/

Please keep in mind that the font won't re-size. 请记住,字体不会调整大小。 What you're trying to do is pretty tough to accomplish but this should help you get started. 您正在尝试做的事情很难完成,但这应该可以帮助您入门。

UPDATE: 更新:

Here is a function I wrote that will change font size based upon the size of your window. 这是我编写的一个函数,该函数将根据窗口的大小更改字体大小。 It doesn't change dynamically, but will change on page load. 它不会动态更改,但是会在页面加载时更改。

http://jsfiddle.net/plushyObject/n3kkg470/1/ http://jsfiddle.net/plushyObject/n3kkg470/1/

instead of $(window) in that function, you could give your div with the background an ID and drop it in there. 而不是该函数中的$(window),您可以给具有背景的div一个ID并将其放在其中。

$('#idYouGave').width();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM