简体   繁体   中英

How to add content over multiple images?

I have a web page in which I have used css cross-fade animation to fade between two background images. Now, I need to add same content (logo and buttons) above both the images. However, this content is visible only for the first image and not the second. This is my code:

index.html

<div id="bgimg">
    <div class="bgimg-container">
      <div id="hero">
        <div class="bgimg-container">

            My content here


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

style.css

    #bgimg{
      display: table;
      background: url(../img/2.jpg);
      width:100%;
      height:100vh;
      background-position: top center;
      background-size: cover;
      background-repeat: no-repeat;
    }

    #hero {
    display: table;
    width: 100%;
    height: 100vh;
    background: url(../img/1.jpg);
    background-position: top center;
    background-size: cover;
    background-repeat: no-repeat;


    animation-name: cf3FadeInOut;
    animation-timing-function: ease-in-out;
    animation-iteration-count: infinite;
    animation-duration: 7s;
    animation-direction: alternate;
    }

    @keyframes cf3FadeInOut {
    0% {opacity:1;}
    45% {opacity:1;}
    55% {opacity:0;}
    100% {opacity:0;}
    }

    @media (min-width: 1024px) {
      #hero, #bgimg {
        background-attachment: fixed;
      }
    }

    #hero .hero-logo {
      margin: 20px;
    }

    #hero .hero-logo img {
      max-width: 100%;
    }


    #hero .hero-container {
      background: rgba(0, 0, 0, 0.8);
      display: table-cell;
      margin: 0;
      padding: 0;
      text-align: center;
      vertical-align: middle;
    }
    #bgimg .bgimg-container {
      background: rgba(0, 0, 0, 0.8);
      display: table-cell;
      margin: 0;
      padding: 0;
      text-align: center;
      vertical-align: middle;
    }

I've exhausted myself trying to figure it out. New here, please help.

here you go:

 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> <style> .img1 { background: #e12; position: absolute; top: 0; left: 0; } .img2:hover { opacity: 0; transition: .5s; } .img2 { background: #312; position: absolute; top: 0; left: 0; transition: .5s; } .img1, .img2 { /* other - for demo */ color: white; width: 100px; height: 100px; width: 100px; height: 100px; } </style> </head> <body> <div class="img1"></div> <div class="img2">Hello World</div> </body> </html> 

change the backgrounds to your images, or if you want to use an img tag, put each img tag inside a div

Here you have a working example, maybe can use this to modify your code;

 @keyframes cf3FadeInOut { 0% { opacity: 1; } 45% { opacity: 1; } 55% { opacity: 0; } 100% { opacity: 0; } } .img-container, .bg-img-1, .bg-img-2, .content { height: 100vh; width: 100vw; } .img-container { position: relative; } .bg-img-1 { background-image: url('https://images.pexels.com/photos/1509428/pexels-photo-1509428.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=650&w=940'); background-size: cover; position: absolute; } .bg-img-2 { background-image: url('https://images.pexels.com/photos/886465/pexels-photo-886465.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=650&w=940'); position: absolute; background-size: cover; animation-name: cf3FadeInOut; animation-timing-function: ease-in-out; animation-iteration-count: infinite; animation-duration: 3s; animation-direction: alternate; } .content { background-color: rgba(0,0,0,0.5); color: #fff; position: absolute; display: flex; justify-content: center; align-items: center; } 
 <div class="img-container"> <div class="bg-img-1"></div> <div class="bg-img-2"></div> <div class="content"> <div> hello <button>world</button> </div> </div> <div> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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