0

Restricting the height of the body to 100% of the screen height you will see background-repeat: no-repeat; not working.

How is, or would this be fixed in the code? https://jsfiddle.net/z3gq0y5x/

Why is the background still repeating?

How would that be fixed in the code?

I am using background-repeat: no-repeat; yet that is not working in the code.

html,
body {
  height: 100%;
  margin: 0;
  padding: 0;
}

body::before {
  content: "";
  background-repeat: no-repeat;
  position: fixed;
  z-index: -1;
  left: 0;
  right: 0;
  top: 0;
  bottom: 0;
}

.bg1 {
  background-image: linear-gradient(45deg, #102eff, #d2379b);
}

.bg2 {
  background-image: linear-gradient(45deg, #102eff, #d2379b);
}

.bg3 {
  background-image: linear-gradient(45deg, #102eff, #d2379b);
}

enter image description here

It can also be seen in the snippet I provided.

(function randomBackground() {
  const classNames = [
    "bg1",
    "bg2",
    "bg3"
  ];

  const random = Math.floor(Math.random() * classNames.length);
  document.querySelector("body").classList.add(classNames[random]);
}());



const videoPlayer = (function makeVideoPlayer() {
  const config = {};
  let player = null;

  const tag = document.createElement("script");
  tag.src = "https://www.youtube.com/iframe_api";
  const firstScriptTag = document.getElementsByTagName("script")[0];
  firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);

  function onYouTubeIframeAPIReady() {
    const frameContainer = document.querySelector(".video");
    videoPlayer.addPlayer(frameContainer, config.playlist);
  }

  function shufflePlaylist(player) {
    player.setShuffle(true);
    player.playVideoAt(0);
    player.stopVideo();
  }

  function onPlayerReady(event) {
    player = event.target;
    player.setVolume(100); // percent
    shufflePlaylist(player);
  }

  function addPlayer(video, playlist) {

    const config = {
      height: 360,
      host: "https://www.youtube-nocookie.com",
      width: 640
    };
    config.playerVars = {
      autoplay: 0,
      cc_load_policy: 0,
      controls: 1,
      disablekb: 1,
      fs: 0,
      iv_load_policy: 3,
      loop: 1,
      playlist,
      rel: 0
    };
    config.events = {
      "onReady": onPlayerReady
    };
    player = new YT.Player(video, config);

  }

  function init(videos) {
    config.playlist = videos.join();
    window.onYouTubeIframeAPIReady = onYouTubeIframeAPIReady;
  }

  return {
    addPlayer,
    init
  };
}());

videoPlayer.init([
  "CHahce95B1g",
  "CHahce95B1g",
  "CHahce95B1g",
  "CHahce95B1g"
]);
html,
body {
  height: 100%;
  margin: 0;
  padding: 0;
}

body::before {
  content: "";
  background-repeat: no-repeat;
  position: fixed;
  z-index: -1;
  left: 0;
  right: 0;
  top: 0;
  bottom: 0;
}

.bg1 {
  background-image: linear-gradient(45deg, #102eff, #d2379b);
}

.bg2 {
  background-image: linear-gradient(45deg, #102eff, #d2379b);
}

.bg3 {
  background-image: linear-gradient(45deg, #102eff, #d2379b);
}

.outer {
  display: table;
  height: 100%;
  margin: 0 auto;
  width: 100%;
}

.tcell {
  display: table-cell;
  vertical-align: middle;
  padding: 8px 8px;
}

.video-wrapper {
  position: relative;
  margin: auto;
  max-width: 640px;
}

.ratio-keeper {
  position: relative;
  height: 0;
  padding-top: 56.25%;
  margin: auto;
}

.video-frame {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  animation: fade 10s ease-in 0s forwards;
}

@keyframes fade {
  0% {
    opacity: 0;
  }

  100% {
    opacity: 1;
  }
}

iframe {
  user-select: none;
}

.hide {
  display: none;
}
<div class="outer">
  <div class="tcell">
    <div class="video-wrapper">
      <div class="ratio-keeper">
        <div class="video video-frame"></div>
      </div>
    </div>
  </div>
</div>

enter image description here

The answer below gives me this:

enter image description here

2nd updated answer provided:

The updated answer causes the video to no-longer be centered in the middle.

The video was originally centered in the middle in the question I posted.

The answer that was provided messed that up.

Seen Here: enter image description here

jps
  • 20,041
  • 15
  • 75
  • 79
Eve Ninnall
  • 1
  • 3
  • 20

1 Answers1

2

You should put the background-repeat: no-repeat; to the body, and not to the body::before. Also add min-height: 100%to your html

html{
  min-height: 100%;
}

html,
body {
  margin: 0;
  padding: 0;
}

body{
    height: 100%;
    background-repeat: no-repeat;
}

body::before {
  content: "";
  position: fixed;
  z-index: -1;
  left: 0;
  right: 0;
  top: 0;
  bottom: 0;
}

.container{
  display:flex;
  align-items:center;
  height: 100%;
}

The purpose of the .container is to center the video vertically.

Here is also the html code:

<div class="container">
  <div class="outer">
    <div class="tcell">
      <div class="video-wrapper">
        <div class="ratio-keeper">
          <div class="video video-frame"></div>
        </div>
      </div>
    </div>
  </div>
</div>
Ere Männistö
  • 518
  • 2
  • 20
  • This is almost perfect but remove height may cause incorrect background on other screen size. – vee Apr 09 '22 at 14:57
  • Yeah I just noticed, problem solved. Body keeps the height of 100%, but is removed from html. Thank you for pointing it out =) – Ere Männistö Apr 09 '22 at 15:04
  • That does not work. Look: https://i.imgur.com/HZqZ1Cc.png Code: https://jsfiddle.net/1kyhmapb/ – Eve Ninnall Apr 09 '22 at 15:20
  • @EricaLevine @erecodes Remove `height: 100%;` from body. Set `min-height: 100%;` to `html` only. – vee Apr 09 '22 at 15:33
  • Update your answer. – Eve Ninnall Apr 09 '22 at 15:34
  • @vee Can you show me it working in the code because now the video is no-longer centered in the middle. Seen Here: https://i.imgur.com/RJJZbyd.png – Eve Ninnall Apr 09 '22 at 15:37
  • The updated answer causes the video to no-longer be centered in the middle. Seen Here: https://i.imgur.com/RJJZbyd.png – Eve Ninnall Apr 09 '22 at 15:42
  • Hey Erica, previous result for me wasn't centered either. Centering elements vertically is a completely different topic and you should check it out from here: https://stackoverflow.com/questions/79461/how-can-i-vertically-align-elements-in-a-div – Ere Männistö Apr 09 '22 at 15:46
  • The video was originally centered in the middle in my Question I posted. https://jsfiddle.net/z3gq0y5x/ The answer that was provided messed that up. – Eve Ninnall Apr 09 '22 at 15:47
  • 1
    @EricaLevine https://jsfiddle.net/v1sm3eL5/ Change the way of `min-height` in `html` and `body`. Add display `flex` to vertical center contents. – vee Apr 09 '22 at 15:50
  • 1
    Erica, I made an update with a quick fix for vertical alignment. Wrap the player inside the container div. – Ere Männistö Apr 09 '22 at 15:55
  • 1
    @EricaLevine I edited a bit the code, knowing you need to align it vertically changes a bit the code. Your image which looks like the video is centered is actually not, if you look it trough the inspector. It is centered with the background, but not actually with the page. So when we fixed the original issue, which was the background repeating, it also made the issue with vertical alignment visible, since the page was not just the size of your content, but actually the whole page. – Ere Männistö Apr 09 '22 at 16:08