Responsive YouTube embedded in Flex
P粉111641966
P粉111641966 2024-03-21 23:20:29
0
1
249

I tried embedding responsive YouTube and some other content in a Flex container, but the container the YouTube embed is in doesn't grow and so either overlaps or is overlapped by other elements. I'm not sure if I explained it well, so I also made a code and box to demonstrate:

https://codesandbox.io/s/objective-mountain-m4dhhb?file=/src/App.js

import { Box, Paper } from "@mui/material";

export default function App() {
  return (
    <Box
      sx={{
        display: "flex",
        flexFlow: "column wrap",
        gap: 2,
        maxWidth: "960px"
      }}
    >
      <Box>
        <Paper
          variant="outlined"
          sx={{
            padding: 2
          }}
        >
          <h2>Some content</h2>
          <Box>
            <p>
              Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla
              maximus at enim in sollicitudin. Nam suscipit tellus sit amet
              porttitor hendrerit. Lorem ipsum dolor sit amet, consectetur
              adipiscing elit. Quisque vel dolor felis. Nulla sodales felis
              iaculis venenatis aliquam. Integer vehicula turpis a mauris
              maximus, eu sagittis ligula dignissim. In faucibus est lacus,
              auctor ornare lacus auctor ut. Morbi vitae ullamcorper nisi.
              Integer non sagittis erat.
            </p>
          </Box>
        </Paper>
      </Box>
      <Box>
        <Paper
          variant="outlined"
          sx={{
            padding: 2
          }}
        >
          <h2>YouTube player</h2>
          <Box sx={{ display: "flex" }}>
            <iframe
              src="https://www.youtube.com/embed/-opb8DdWBIM"
              title="YouTube video player"
              style={{
                margin: "0 auto",
                width: "100%",
                aspectRatio: "16 / 9"
              }}
            />
          </Box>
        </Paper>
      </Box>
      <Box>
        <Paper
          variant="outlined"
          sx={{
            padding: 2
          }}
        >
          <h2>Some other content</h2>
          <p>
            In vehicula a est non tempor. Maecenas ut leo ac arcu sollicitudin
            ullamcorper et vitae lectus. In venenatis lectus purus, non rhoncus
            ante pellentesque a. Sed sollicitudin leo sit amet odio ultricies
            pretium. Mauris eu nibh diam. Sed ante ipsum, efficitur ac leo eget,
            cursus ornare augue. Morbi aliquet porta rhoncus. Quisque ut tellus
            metus. Nam purus arcu, accumsan a dolor at, rhoncus iaculis velit.
            Class aptent taciti sociosqu ad litora torquent per conubia nostra,
            per inceptos himenaeos. Nulla augue metus, semper id varius sit
            amet, congue vitae nunc. Phasellus interdum arcu quis quam tristique
            porta. Curabitur sit amet imperdiet turpis, ut facilisis mauris.
            Aliquam sit amet commodo nisi. Quisque elementum nulla ac
            ullamcorper interdum. Aliquam interdum sed lorem a tempus.
          </p>
        </Paper>
      </Box>
    </Box>
  );
}

How can I change it so that the embedded container grows and shrinks with the size of the embedded video? thanks for your help

P粉111641966
P粉111641966

reply all(1)
P粉329425839

renew

This is a codesandbox solution.

  1. Wrap the iframe in a container with position: "relative" and set its
    Width: "100%" .
  2. Set the container's paddingBottom to the desired
    Aspect ratio value expressed as a percentage. For example, for a 16:9 aspect ratio, Use "56.25%" (9 / 16 * 100%).
  3. Set iframe to Position: "Absolute", Top: 0, Left: 0, Width: "100%", Height: "100%" .
  4. To ensure that the text under the video is not hidden, please set flexFlow: "column",

Here are examples of how to achieve this:

//Main container box

Some other code

YouTube player

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!