简体   繁体   English

使用 CSS 将文本添加到背景图像

[英]Add text to background image using CSS

I am using a background image on a section which covers the whole screen while the section is in view.我在一个部分上使用背景图像,该部分在该部分可见时覆盖整个屏幕。 Then it is covered up by subsequent sections as the page scrolls.然后随着页面滚动,它被后续部分覆盖。

#section1{
 background: URL(../SiteData/Images/SectionBackground.jpg) no-repeat center top fixed #f1ece2;

-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;

min-height:800px;
}

I want to add text about half way down this image.我想在这张图片的一半左右添加文字。 This I can do, but I want it to stay in position, as if it was actually part of the image.我可以这样做,但我希望它保持原位,就好像它实际上是图像的一部分一样。 So when the screen scrolls up, the text gets hidden rather than scrolls to the top - so it stays in position on the image.因此,当屏幕向上滚动时,文本会被隐藏而不是滚动到顶部 - 因此它会保持在图像上的位置。

How would I do this?我该怎么做?

Thanks谢谢

Here is a demo answer for make them both image and text responsive Live On FIDDLE .这是一个演示答案,可让它们同时响应图像和文本的Live On FIDDLE Try this but not just copy paste.试试这个,但不仅仅是复制粘贴。 Try to understand the css how this text over the image.尝试了解 css 如何将文本覆盖在图像上。 If you have any questions ask me in comment.如果您有任何问题,请在评论中问我。 Best of luck.祝你好运。

  
 .img-reponsive { display: block; max-width: 100%; height: auto; margin: 0 auto; } .textoverimage { position: relative; } p.title { position: absolute; top: 75px; left: 0; text-align: center; font-size: 38px; width: 100%; color: #fff } @media only screen and (max-width: 767px) { p.title { font-size: 1.5em; line-height: 1.5em; } } @media only screen and (max-width: 479px) { p.title { font-size: 1.1em; line-height: 1.1em; } }
 <div class="textoverimage"> <img class="img-reponsive" src="http://cdn.shopify.com/s/files/1/0258/1101/t/2/assets/slideshow_3.jpg?5808719635421587686" alt="demo_img"> <p class="title"> TEXT </p> </div>

bootstrap 4引导程序 4



    <section class="vh-100" style="
     background: url(public/images/aimage.png) no-repeat center center fixed; 
     background-size: 100%;
 ">
        <div class="container h-100 d-flex ">

            <div class="row my-auto ">
                <div class="col-lg-3 col-sm-12">
                    <div class="jumbotron ">
                        <h1 class="display-3">Hello, world!</h1>
                        <p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Exercitationem fuga, molestiae illo pariatur praesentium nemo nam magnam molestias eius at! Cupiditate ratione natus veritatis eaque totam illo accusamus perferendis? Enim?</p>
                    </div>
                </div>
                <div class="col-lg-3 col-sm-12">
                    <div class="jumbotron ">
                        <h1 class="display-3">Hello, world!</h1>
                        <p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Exercitationem fuga, molestiae illo pariatur praesentium nemo nam magnam molestias eius at! Cupiditate ratione natus veritatis eaque totam illo accusamus perferendis? Enim?</p>
                    </div>
                </div>
                <div class="col-lg-3 col-sm-12">
                    <div class="jumbotron ">
                        <h1 class="display-3">Hello, world!</h1>
                        <p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Exercitationem fuga, molestiae illo pariatur praesentium nemo nam magnam molestias eius at! Cupiditate ratione natus veritatis eaque totam illo accusamus perferendis? Enim?</p>
                    </div>
                </div>
                <div class="col-lg-3 col-sm-12">
                    <div class="jumbotron ">
                        <h1 class="display-3">Hello, world!</h1>
                        <p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Exercitationem fuga, molestiae illo pariatur praesentium nemo nam magnam molestias eius at! Cupiditate ratione natus veritatis eaque totam illo accusamus perferendis? Enim?</p>
                    </div>
                </div>


            </div>
        </div>
    </section>

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

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