简体   繁体   中英

How to make video's width 100% or height 100%

I'm having the same issue as this, but i'm trying to do it on <video/> element. I want to make video element sometimes width: 100% , and sometimes height: 100% by aspect ratio of it.

Here's my css

.remoteVideo-container {
  position: fixed;
  left: 0;
  top: 0;
  z-index: 0;
  width: 100vw;
  height: 100vh;
  text-align: center;
  background-color: rgb(45, 48, 53);
}

.remoteVideo {
  object-fit: contain;
  transform: scale(-1, 1);
}

here's my jsx

      <div className="remoteVideo-container">
          <video
            className="remoteVideo"
            autoPlay
            ref={this.remoteVideo}
            muted
          ></video>
      </div>

Result:

在此处输入图片说明

I was able to do it by a small css hack. Set min-width and min-height to 100% and then height and width to auto to prevent from stretching or zooming in the video. And a bit more to center the video.

.remoteVideo {
  min-width: 100%; 
  min-height: 100%; 
  width: auto;
  height: auto;
  top: 50%;
  left: 50%;
  transform: translate(-50%,-50%);
}

You almost got it, you missed the sizing of the video tag. object-position could be also handy :

.remoteVideo {
  height:100%; /* or is max-height:100%; */
  width:100%;  /* or is max-width:100%;  */
  object-fit: contain;
  object-position:center;
  transform: scale(-1, 1);
}

Possible example to run in full page to test behavior on resize, or version based on max-height / max-width 100% https://codepen.io/gc-nomade/pen/bGNzzNj to shrink video if it becomes bigger than the screen .Up to you to choose value for height and width.

 body { margin: 0; } .remoteVideo-container { position: fixed; left: 0; top: 0; z-index: 0; width: 100vw; height: 100vh; text-align: center; background-color: rgb(45, 48, 53); } .remoteVideo { height: 100%; width: 100%; object-fit: contain; object-position: center; transform: scale(-1, 1); }
 <div class="remoteVideo-container"> <video class="remoteVideo" autoplay muted poster="https://s3-us-west-2.amazonaws.com/s.cdpn.io/4273/polina.jpg"> <source src="http://thenewcode.com/assets/videos/polina.webm" type="video/webm" /> <source src="http://thenewcode.com/assets/videos/polina.mp4" type="video/mp4" /> </video> </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