What is the best way to customize a video in Bootstrap 5 so that it remains at the bottom of the screen with centrally aligned text

When using Bootstrap 5, I am facing a challenge with a video on the index page that plays beneath the navigation. I want to maintain the 16x9 aspect ratio and have centered text overlayed on the video. However, I encounter an issue when attempting to set a maximum height.

HTML:

<section className="hero-video">
  <div className="ratio ratio-16x9">
    <div className="overlay-text">
      <h1>Foo Bar</h1>
    </div>
    <iframe
      src="https://youtube.com/embed/wxL8bVJhXCM?controls=0&amp;autoplay=1&amp;mute=1&amp;modestbranding=0&amp;showinfo=0"
      frameBorder="0"
      allow="autoplay; encrypted-media"
      title="video"
      style={{
        position: 'absolute',
        bottom: 0,
        width: '100%',
        height: '100%',
        margin: '0 auto',
      }}
    ></iframe>
  </div>
</section>

CSS:

.hero-video {
  position: relative;
  overflow: hidden;
  max-height: 600px;
}
.overlay-text {
  position: absolute;
  background-color: rgba(58, 57, 57, 0.5);
  z-index: 10;
  width: 100%;
  height: 100%;
  overflow: hidden;
}
.overlay-text h1 {
  color: #fff;
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
}

Full Component:

import React from 'react'
import { useStaticQuery, graphql } from 'gatsby'

const Hero = () => {
  const { site } = useStaticQuery(
    graphql`
      query {
        site {
          siteMetadata {
            description
            hero
          }
        }
      }
    `,
  )

  const description = site?.siteMetadata?.description
  const hero = site?.siteMetadata?.hero

  return (
    <section className="hero-video">
      <div className="ratio ratio-16x9">
        <div className="overlay-text">
          <h1>{description}</h1>
        </div>
        <iframe
          src={`https://youtube.com/embed/${hero}?controls=0&amp;autoplay=1&amp;mute=1&amp;modestbranding=0&amp;showinfo=0`}
          frameBorder="0"
          allow="autoplay; encrypted-media"
          title="video"
          style={{
            position: 'absolute',
            bottom: 0,
            width: '100%',
            height: '100%',
            margin: '0 auto',
          }}
        ></iframe>
      </div>
    </section>
  )
}

export default Hero

Research:

  • How do I set max-width for an image in Bootstrap carousel and keep aspect ratio?
  • Max height div not working in css and bootstrap 5
  • set size to responsive iframe in bootstrap
  • How can I apply a max-height to an iframe but keep it at 100% width?

Question:

Is there a way in Bootstrap 5 to set a maximum height for a video, maintain centered text overlay, and anchor the video to the bottom of the div?

Answer №1

When using Bootstrap 5, there is no need to apply CSS directly to the iframe element. Simply make sure to add the ratio class to the parent div. Additionally, ensure that the overlay-text element is positioned correctly by adjusting the order and adding some CSS styling:

<section className="hero-video">
  <div className="ratio ratio-16x9">
    <iframe
      src="https://youtube.com/embed/wxL8bVJhXCM?controls=0&amp;autoplay=1&amp;mute=1&amp;modestbranding=0&amp;showinfo=0"
      frameBorder="0"
      allow="autoplay; encrypted-media"
      title="video">
    </iframe>
  </div>
  <div className="overlay-text">
    <h1>Foo Bar</h1>
  </div>
</section>

Additionally, make sure to include top and left positioning in the CSS for the overlay-text class:

.overlay-text {
  position: absolute;
  top: 0;
  left: 0;
  background-color: rgba(58, 57, 57, 0.5);
  z-index: 10;
  width: 100%;
  height: 100%;
  overflow: hidden;
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Arranging block-level elements in a horizontal format

In the book "CSS The definitive Guide", it is mentioned that "The values of these seven properties must add up to the width of the element’s containing block, which is usually the value of width for a block element’s parent". However, in the case provi ...

Breaking Free from Bootstrap Grids

I have a row with two Bootstrap columns - one wide and tall, the other short and narrow https://i.sstatic.net/u7WAf.png However, for mobile devices (targeted below lg), I want to change the column order to split into three sections Photo Consent Organis ...

How can I customize the color of the Title Component in Ant Design using Styled Components?

I am currently integrating Ant Design with styled components in my project. One of the components in my application is a NavBar that includes an H1 Component. H1.tsx import React from 'react'; import { Typography } from 'antd'; impor ...

Why isn't the HTML template opening in Outlook 7?

Does anyone know how to fix the code issue on Outlook 7? I've only included the header section of my code here, but please let me know if you need more information. I would appreciate any help with this problem. Thanks! ...

Creating responsive divs on my webpage

Although I have browsed through various posts with similar questions, none of the solutions seem to work for me. I will make sure to include all relevant information related to my issue and explain why I am unable to resolve it on my own. Currently, I am ...

The issue of duplicate CSS arising during the compilation of SASS into a single CSS file with G

Just getting started with Stack Overflow and Gulp (using version 3.9.1). My goal is to compile all of my scss files into a single css file for my website. Here's what I have in my gulpfile so far: var gulp = require('gulp'); var sass = requ ...

Utilize BootStrap framework to embed Twitch player, ensuring it fills the entire width of its parent element

I'm new to web development and struggling to make my embedded Twitch player use its parent's full width. The height is fine, but using the d-flex class from Bootstrap makes the player extremely thin. Please review my code here: https://jsfiddle. ...

Mobile Device Experiencing Constant Resizing Issues on Website

I have been working on my website for almost a year now, and I am facing an issue with its responsiveness on mobile devices. Despite using Bootstrap 4 and Ajax to load pages and modals, I can't seem to figure out what's causing the problem. Befor ...

Ways to conceal a grid item in Material UI framework

My goal is to hide a specific grid item for a product and smoothly slide the others in its place. Currently, I am using the display:none property but it hides the item instantly. I have already filtered the products and now I want to animate the hiding of ...

Pressing a button is a way to select a specific form

I'd like to create a clickable button that can direct users to the form section on the same page. <button type="button" class="btn btn-primary btn-sm"> Go to Form Section </button> ... <form id="form1"> <div class="form-g ...

The ideal caret position while utilizing flexbox for centering within a contenteditable element

After testing on OSX Chrome 45, it appears that align-items: center; works for content alignment, but there is an issue with caret positioning in an empty editable field until text is typed. Is there a solution to this problem that doesn't involve ad ...

Enhancing webpage styles with AngularJS

Just starting out with AngularJS and eager to get the best approach to tackle this challenge. Describing my dilemma: I have an anchor element that, when clicked, needs to toggle between classes "show-all" and "hide-all" while also updating the styling of ...

Enhancing Bootstrap Carousel with various effects

My exploration of the web revealed two distinct effects that can be applied to Bootstrap Carousel: Slide and Fade. I'm curious if there are any other unique effects, such as splitting the picture into small 3D boxes that rotate to change the image? ...

CSS and JavaScript dropdown malfunctioning due to a position swap in internal CSS

This example demonstrates functionality .c1 { background-color:red; position:relative; } .c2 { background-color:blue; position:absolute; height:50px; width:100px; display:none;} .show { display:block; } <body> <button ...

Stylish CSS Effects on Hover

I'm currently in the process of developing a website and I would like to enhance my buttons with a hover effect that involves loading another image or button with a slightly different color scheme. These new images have been created in Photoshop. How ...

What is the reason for adding CSS styles to a JavaScript file without importing them?

/Navbar.js/ import './navbar.scss'; import {FaBars, FaSearch} from "react-icons/fa"; import { useState } from 'react'; function Navbar(){ const [hide,sethide] = useState(true); const barIcon = document.querySelectorAl ...

drag and drop feature paired with additional textarea below

query 1: How can I make the bottom-left textarea move together with the top-left textarea when I drag it down? query 2: What is the solution to prevent the left-bottom textarea from moving when I drag down the top-right textarea? http://jsfiddle.net/4BX6 ...

Setting the size of a box using CSS in HTML pages

I am facing issues with resizing boxes. Below is the code snippet: <!DOCTYPE html> <html lang="en"> <head> <style> .wrapper { text-align: center; } #bubble { display: inline; -moz ...

Position the read more buttons in JavaScript at the bottom of the div

I designed three boxes in this section with content inside. To enhance the functionality, I added a feature that made the boxes smaller. Everything was functioning perfectly, but I encountered an issue with the alignment of the buttons at the bottom. Is th ...

Sliding in images with JQuery

I need help with animating the slide-in effect of 7 "card" images from the left to the center of the screen. I attempted to achieve this using the following code: function FetchCards() { $("#pack").css('margin-left', 0); $("#pack").css(& ...