Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

html

css

reactjs

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:

enter image description here

like image 322
Shotiko Topchishvili Avatar asked Sep 07 '25 04:09

Shotiko Topchishvili


1 Answers

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>
like image 170
G-Cyrillus Avatar answered Sep 10 '25 02:09

G-Cyrillus