Search code examples
htmlcssresponsive-designmedia-queries

issue replacing background video with image


Really frustrating problem I have encountered I have a full screen video playing in the background of my website however, I am designing the mobile layout and I am trying to replace the video with an image. However, it is not changing to the image on the landing page on the mobile- please see code below.

*/ CSS CODE */

@media(max-width:768px) {
.header {
    width: 100%;
    background-size: contain;
    background-position: center;
}
.title h1 {
    font-size: 40px;
    color: #fff;
    top: 150px;
}
p {
    font-size: 20px;
    font-weight: 600;
}
.middle {
    height: 1400px;
    background-color: #1f1f20;
}
.social {
    margin: 0px;
    padding: 10px;
    position: absolute;
    top: 380%;
    left: 30%;
    transform: translate(-50%, -50%);
    align-items: center;
}

.bottom {
    height: 50px;
    background-color: #fff;
    margin: auto;
    font-size: 20px;
    color: white;
    text-align: justify;
    text-align: center;
}
.Ishmayl-writeup {
    font-size: 20px;
    font-weight: 700;
    line-height: 30px;
    color: #1f1f20;
}
.title video {
    display: none;
}
.title video {
    background: url('/images/3.jpg');
    background-size: cover;
}

}

@media(min-aspect-ratio:16/9) {
.header {
    width: 100%;
    height: auto;
}
.header video {
    width: 100%;
    height: auto;
}
@media(max-aspect-ratio:16/9) {
    .header {
        width: auto;
        height: 100%;
    }
    .header video {
        width: auto;
        height: 100%;
    }
}
}

*/ HTML CODE */

<body>
<section class="top" id="Home">
    <header>

        <div class="title">

            <div class="video">
                <video src="Video/Mountain.mp4" autoplay="true" loop="true" muted type="video/mp4"></video>
                <div class="overlay"></div>

Solution

  • I would make two different classes for desktop and for mobile phone like this:

    Demo

    <div class="title">
      <div class="video onlydesktop">
        <video src="Video/Mountain.mp4" autoplay="true" loop="true" muted type="video/mp4"></video>
        <div class="overlay"></div>
      </div>
      <div class="photo onlymobile">
        <div class="overlay"></div>
      </div>
    </div>
    
    
    .photo  {
        background: url('https://images.unsplash.com/photo-1508138221679-760a23a2285b?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&w=1000&q=80');
        background-size: cover;
        width: 100%;
        height: 500px;
    }
    
    
    @media(min-width:769px) {
    
    .onlydesktop{
      display:inline;
    }
    .onlymobile{
      display:none;
    }
    
    @media(max-width:768px) {
    
    .onlydesktop{
      display:none;
    }
    .onlymobile{
      display:inline;
    }
    .header {
        width: 100%;
        background-size: contain;
        background-position: center;
    }
    .title h1 {
        font-size: 40px;
        color: #fff;
        top: 150px;
    }
    p {
        font-size: 20px;
        font-weight: 600;
    }
    .middle {
        height: 1400px;
        background-color: #1f1f20;
    }
    .social {
        margin: 0px;
        padding: 10px;
        position: absolute;
        top: 380%;
        left: 30%;
        transform: translate(-50%, -50%);
        align-items: center;
    }
    
    .bottom {
        height: 50px;
        background-color: #fff;
        margin: auto;
        font-size: 20px;
        color: white;
        text-align: justify;
        text-align: center;
    }
    .Ishmayl-writeup {
        font-size: 20px;
        font-weight: 700;
        line-height: 30px;
        color: #1f1f20;
    }
    .title video {
        display: none;
    }
    
    }