1

I need to display video elements in grid, where some grid will be without video element and some grid will be with video element, all the time every grid should have equal width and height.

But with below example the second and third grid have different width rest all have equal width. If I put every grid with video element then every grid have equal size, and if remove every video element grid are with equal dimension. The problem is when some grid have video element in it. How can I resolve it.

<!DOCTYPE html>
<html>

<head>
  <style>
    body,
    html {
      width: 100%;
      height: 100%;
      margin: 0;
      padding: 0
    }
    
    .grid-container {
      display: grid;
      grid-template-columns: auto auto auto auto;
      grid-template-rows: auto auto;
      grid-gap: 8px;
      background-color: #fff;
      padding: 1px;
    }
    
    .grid-item {
      background-color: #fafafa;
      text-align: center;
      padding: 0px 0;
      font-size: 30px;
      border: 0px solid #d2d2d7;
      border-radius: 5px;
    }
  </style>

</head>

<body style='background: #fff;'>

  <div id='mainBg' class='grid-container' style='grid-auto-flow: row;margin-top:40px;height:calc(100% - 90px)'>

    <div class="grid-item">
      <div style="height: 100%; width: 100%;"> </div>
    </div>
    <div class="grid-item">
      <div style="height: 100%; width: 100%;"> </div>
    </div>
    <div class="grid-item">
      <div style=" height: 100%; width: 100%;"> </div>
    </div>
    <div class="grid-item">
      <div style=" height: 100%; width: 100%;">
        <video width="100%" height="100%" controls>
            </video>
      </div>
    </div>
    <div class="grid-item">
      <div style=" height: 100%; width: 100%;">
        <video width="100%" height="100%" controls>
             
            </video>
      </div>
    </div>
  </div>
</body>
</html>

Fiddle https://jsfiddle.net/8oqp5kys/

1 Answer 1

2

Update the following line inside your .grid-container:

grid-template-columns: 1fr 1fr 1fr 1fr;

This will set 4 columns of equal width. Some further reading on "fractional units" or "flexible length" units. So your full code may look like this:

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

.grid-container {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr 1fr;
  grid-template-rows: auto auto;
  grid-gap: 8px;
  background-color: #fff;
  padding: 1px;
}

.grid-item {
  background-color: #fafafa;
  text-align: center;
  padding: 0px 0;
  font-size: 30px;
  border: 0px solid #d2d2d7;
  border-radius: 5px;
}
<body style='background: #fff;'>

<div id='mainBg' class='grid-container' style='grid-auto-flow: row;margin-top:40px;height:calc(100% - 90px)'>

  <div class="grid-item">
    <div style="height: 100%; width: 100%;"> </div>
  </div>
  <div class="grid-item">
    <div style="height: 100%; width: 100%;"> </div>
  </div>
  <div class="grid-item">
    <div style=" height: 100%; width: 100%;"> </div>
  </div>
  <div class="grid-item">
    <div style=" height: 100%; width: 100%;">
      <video width="100%" height="100%" controls>
          </video>
    </div>
  </div>
  <div class="grid-item">
    <div style=" height: 100%; width: 100%;">
      <video width="100%" height="100%" controls>

          </video>
    </div>
  </div>
</div>
</body>

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.