How do I format a video display in GenerateBlocks

  • I’m working on a new site for a client. They have a couple videos that will be self-hosted on the website. If I just add a container and embed a video block in it the video doesn’t size to fit the container. How do I contain the video in a GenerateBlocks video? I can center it horizontally, but there doesn’t seem to be a control to center it vertically or control the sizing to fit the container.

  • Hello,

    Maybe this doc could be of help?

  • Thanks for the information. The https://docs.generatepress.com/article/adding-a-background-video/ covers adding a background video. The video is the only content in that block, and the only content in the container the video block is in. I set set the maximum height and maximum width for the container but the video isn’t contained.

    I’ll experiment with using a custom HTML block instead of the video block to contain and play the video.

    Any suggestions are appreciated.

  • After a bit of experimenting it turned out to be a simple fix. The structure is a grid with 2 columns. I added a container to the grid column the video resides in. I experimented with the aspect ratio setting of that container until I got the video size I needed. It’s taller than I need but it should work well.

    It would be great if GenerateBlocks provided scaling/sizing settings for the Video block.

  • Glad you got it working.

    To clarify for anyone finding this later: GenerateBlocks doesn’t have its own Video block. What you’re placing in the Container is the core WordPress Video block, which outputs a <figure class="wp-block-video"> wrapping a <video> element. That <video> carries fixed width/height attributes and an inline aspect-ratio style, and the <figure> has its own default margins — which is why it doesn’t conform to the Container’s max-height/max-width.

    Your approach of setting an Aspect Ratio on the Container is the right one. If you want finer control over how the video fills that ratio, add a class to the Container under Advanced, then target both the figure and the video with custom CSS:

    .your-container .wp-block-video {
        margin: 0;
        width: 100%;
    }
    
    .your-container .wp-block-video video {
        width: 100%;
        height: 100%;
        object-fit: cover; /* or "contain" to avoid cropping */
    }

    Use cover to fill the Container (crops edges) or contain to fit the whole video inside it (may letterbox).

    One thing to note: the core block adds an inline aspect-ratio style directly on the <video> element. If you want the video to follow your Container’s aspect ratio instead, you’ll need to override it with aspect-ratio: auto !important; in that second rule, since inline styles take priority over the stylesheet.

    A native Video block with built-in sizing controls is a reasonable feature request — I’d suggest adding it to feedback.generatepress.com so the team can gauge interest.

  • Thanks for the suggestions. This is exactly the information I needed!

  • No problem!

  • I’m still having problems getting the video to fill the block container without exceeding the max height. Setting the aspect ratio in the container with the video does control the height but it shrinks the width.

    I added the suggested code to style.css.

    /* Butterfly video  */
    
    .butterfly .wp-block-video {
        margin: 0;
        width: 100%;
    }
    
    .butterfly .wp-block-video {
       width: 100%;
       height: 100%;
       object-fit: contain !important;
       aspect-ratio: auto !important;
    }
  • Hi,

    Can I see a URL please?

  • Oops!

  • Hi,

    What exactly are you trying to achieve? Do you want the video’s height to be constrained up to the point where the text at the left ends?

  • Yes, I actually need it to have less height, as the text from “Designed to stop people in their tracks” on down was originally supposed to go between the block with the butterfly video and the turtles banner. I want the video to fill the block and be constrained by setting the max dimensions for the block.

    Thanks for your help!

  • Ok, to begin with, select your grid and align it at the start (align-items: start).

    The reason the height won’t change is a conflict between two aspect ratios. The core Video block adds an inline aspect-ratio style directly on the <video> element (in your case 544 / 960, since the source is portrait), and inline styles override anything set in the Customizer or a stylesheet. So whatever you set on the Container, the video keeps sizing to its own inline ratio.

    To fix it, target the video element, override that inline aspect ratio, and give it an explicit height. Add a class to the video’s Container under Advanced (or use its existing class), then add this CSS:

    .your-container video {
        width: 100%;
        height: 500px;            /* your target height */
        aspect-ratio: auto;        /* overrides the inline 544/960 */
        object-fit: cover;          /* fills the width, crops top/bottom */
        display: block;
        margin: 0;
    }
    
    .your-container .wp-block-video {
        margin: 0;
    }

    A few notes:

    1. I don’t see the .butterfly class you are referencing anywhere in the markup so your CSS will not work if you use this class!

    2. Since your video is portrait and you’re displaying it in a wider, shorter box, object-fit: cover fills the full width and crops the top and bottom. If you’d rather see the whole video without cropping, use object-fit: contain instead — but because the source is tall, that will letterbox it with space on the sides.

    3. Adjust the height value to taste. Replace .your-container with the actual class on the Container holding the video.

  • Thanks for more great information. For some reason the butterfly class didn’t save when I saved the page. I just added the CSS you sent. The height is now constrained to whatever I set it to. The block containing the video has a width of 573px, but the video displays at 448px in width.

    /* Butterfly video  */
    
    .gb-element-630f2841 video {
        width: 100%;
        height: 670px !important;            /* your target height */
        aspect-ratio: auto !important;        /* overrides the inline 544/960 */
        object-fit: cover !important;          /* fills the width, crops top/bottom */
        display: block !important;
        margin: 0;
    }
    
    .gb-element-630f2841 .wp-block-video {
        margin: 0;
    }
    
    .butterfly .wp-block-video {
        margin: 0;
        width: 100%;
    }
    
    .butterfly .wp-block-video {
       width: 100%;
       height: 100%;
       object-fit: contain !important;
       aspect-ratio: auto !important;
    }
  • Hi,

    1. The .butterfly class is still not there.

    2. Please, use the CSS I set you with no changes if possible, apart from the height.

  • Thanks for your patience and assistance. I have commented out the butterfly class. For some reason, the CSS class box doesn’t always display when the container with the video is selected.

    Here is what I have in style.css.

    .gb-element-630f2841 video {
        width: 100%;
        height: 670px !important;            /* your target height */
        aspect-ratio: auto !important;        /* overrides the inline 544/960 */
        object-fit: cover !important;          /* fills the width, crops top/bottom */
        display: block !important;
        margin: 0;
    }
    
    .gb-element-630f2841 .wp-block-video {
        margin: 0;
    }
Viewing 16 posts - 1 through 16 (of 24 total)
  • You must be logged in to reply to this topic.