Lets go step for step. There are multiple different "techniques" we need to use.
First we need a container (div#myVideo
). The container we give the property and value: object-fit: contain;
. This will make the container maintain the videos aspect ratio (also works for images). So it will resizes the height in this case to maintain the videos aspect ratio while it's width adepts to the given width of the viewport.
Then we give the same container the property and value position: relative;
this will be necessary for the grid.
Lets move on to the video:
The video we give the property and value: width: 100%;
. This will make the video fit the viewport width. the height is adepting to maintain it's aspect ratio (see above).
The grid:
The grid needs a higher z-index
then the video
to be placed on top of it (layer-wise).
Also we give it the property and value: position: absolute; inset: 0;
(inset: 0;
is a syntax for top, right, bottom, left: 0;
however it is just not fully supported by all browsers yet). This will make the grid span the entire container width and height (which equals the videos width and height as explained above).
Finally a sidenote:
I changed a few of your lines to be more effectiv.
grid-template-columns: 33.33% 33.33% 33.33%;
=> grid-template-columns: repeat(3, 1fr);
repeat(3, 1fr);
will create 3 columns with an equal width of 1fr
means exactly 1/3 in this case. If you change the 3 to a 4, then this would equal 25%.
Then I removed your border: 1px solid white;
and changed it to box-shadow:0 0 0 1px white;
in combination with the parents grid-gap: 1px;
it will emulate a "border-collapse" and your border will be an actual 1px and not 2px (reference)
#myVideo {
object-fit: contain; /* sizes the container to maintain the videos aspect ratio */
position: relative;
}
#myVideo > video {
width: 100%; /* making sure that the video doesnt overflow the viewport (horizontally) */
z-index: 1;
}
.grid-container {
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
z-index: 2;
color: white;
display: grid;
grid-template-columns: repeat(3, 1fr);
grid-gap: 1px; /* emulates a bordercollapse */
}
.grid-item {
background-color: none;
box-shadow:0 0 0 1px white; /* emulates a border-collapse */
font-size: 30px;
color: white;
}
/* for styling purpose only */
body {
margin: 0;
}
<div id="myVideo">
<video src="https://www.tacoshy.de/stackoverflow/testvideo.mp4" autoplay></video>
<div class="grid-container">
<div class="grid-item"></div>
<div class="grid-item">1</div>
<div class="grid-item">2</div>
<div class="grid-item">3</div>
<div class="grid-item">4</div>
<div class="grid-item">5</div>
<div class="grid-item">6</div>
<div class="grid-item">Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua.</div>
<div class="grid-item">7</div>
</div>
</div>