文本没有显示在背景图像上

text not showing up over background image

本文关键字:背景 图像 显示 文本      更新时间:2023-09-26

我正在制作一个登录页,这听起来可能是一个愚蠢的问题,但我的<h2><form>元素没有显示在我的页面上。我已经尝试了我能想到的一切,但仍然一无所获。我很困惑。

我在全屏背景图像中使用的照片是photoshop中的一张,中间有一个灰色的正方形(看起来就像有些人用z索引做的那样)。在背景中,它是一种复古设计的js骑行标志。

我不确定我是否做错了什么,或者我的css、html、js中是否有什么东西导致文本/表单不显示。

index.html

 <section id="bg">
        <img src="img/bg.jpg">
        <div class="container">
            <div class="row">
                <div class="col-lg-12" id="rotating-img-wrapper">
                    <img class="rotating-img" src="img/corona.png">
                    <img class="rotating-img" src="img/mc.png">
                    <img class="rotating-img" src="img/mtv.png">
                    <img class="rotating-img" src="img/op.png">
                    <img class="rotating-img" src="img/supercell.png">
                </div>
            </div> 
            <div class="text">
              <h2>To learn more about our services, drop us a line</h2>
               <div class="form-group">
                <label for="inputPassword3" class="col-sm-2 control-label">Email Address</label>
                <div class="col-sm-10">
                  <input type="password" class="form-control" id="inputPassword3" placeholder="Password">
                </div>
              </div>       
            </div>            
    </section> 

css

#bg {
  position: fixed; 
  top: -50%; 
  left: -50%; 
  width: 200%; 
  height: 200%;
}
#bg img {
  position: absolute; 
  top: 0; 
  left: 0; 
  right: 0; 
  bottom: 0; 
  margin: auto; 
  min-width: 50%;
  min-height: 50%;
}
#rotating-img-wrapper img {
  position: fixed;
  width: 250px;
  height: 750px;
}
.rotating-img {
  display: none;
  position: absolute;
  top: 0;
  left: 0;
}
h2 {
  color: #000000;
  position: absolute;
}

javascript

$(window).load(function() { 
    var InfiniteRotator =
    {
        init: function()
        {
            //initial fade-in time (in milliseconds)
            var initialFadeIn = 1000;
            //interval between items (in milliseconds)
            var itemInterval = 5000;
            //cross-fades time (in milliseconds)
            var fadeTime = 2500;
            //number of items
            var numberOfItems = $('.rotating-img').length;
            //current item
            var currentItem = 0;
            //show first item
            $('.rotating-img').eq(currentItem).fadeIn(initialFadeIn);
            //loop through the items
            var infiniteLoop = setInterval(function(){
                $('.rotating-img').eq(currentItem).fadeOut(fadeTime);
                if(currentItem == numberOfItems -1){
                    currentItem = 0;
                }else{
                    currentItem++;
                }
                $('.rotating-img').eq(currentItem).fadeIn(fadeTime);
            }, itemInterval);
        }
    };
    InfiniteRotator.init();
});

如果有人能在我的代码中看到我看不到的错误,我很想知道。谢谢你的帮助。

我对如何处理这种布局有一些见解。

我稍微简化了你的HTML(删除了表单,放了一行简单的文本),所以由于固定的和绝对的定位元件。

关键是,由于#bg元素是固定的,它为定位任何其他定位的子元素,无论是固定的还是绝对的。

在原始文章中,您将偏移设置为top: -50%left: -50%将块的原点放置在可见查看区域之外。

结果,h2位于#bg的左上角,因此不可见,并且p文本,它在常规的内容流中,也将从左上角开始容器块(#bg)。

首先,将偏移设置为top: 0left: 0,宽度和高度为100%,然后重新思考如何在图像旋转器和背景中调整图像的大小形象

既然你看到了要素所在,你就能够取得进展使用您的布局。

body {
  margin: 0; /* get rid of 10px border from default browser style sheet */
}
#bg {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
}
#bg img {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  margin: auto;
  min-width: 50%;
  min-height: 50%;
}
#rotating-img-wrapper img {
  position: fixed;
  width: 250px;
  height: auto;
}
.rotating-img {
  display: block;
  position: absolute;
  top: 0;
  left: 0;
}
h2 {
  color: #000000;
  position: absolute;
  top: 30%;
  left: 50%;
}
<section id="bg">
  <img src="http://placehold.it/500x500">
  <div class="container">
    <div class="row">
      <div class="col-lg-12" id="rotating-img-wrapper">
        <img class="rotating-img" src="http://placekitten.com/2000/4000">
      </div>
    </div>
    <div class="text">
      <h2>To learn more about our services, drop us a line</h2>
      <p>Some other words ...</p>
    </div>
</section>

您可以使用背景图像:url(img/bg.jpg);在#bg中,而不是直接添加图像并尝试在其上添加一些内容。

在图像CSS中,在CSS:中执行此操作

z-index:-1000;

z索引控制哪些元素与其他元素重叠。z索引越高,元素在前面的位置就越多。看看这是否会清除任何内容。否则,还有另一个问题。我也很好奇你为什么在所有这些元素上使用绝对定位。

在CSS中尝试以下代码:

#bg {
    background-image:url('img/bg.jpg');
}

然后从HTML页面中删除标记。我还将考虑简化您的代码,因为您似乎有大量的div都封装在一起。如果桌子适合你的需要,也许可以考虑使用它。