在SVG容器中将单个弹性盒子项目(视频)底部左对齐。

huangapple go评论57阅读模式
英文:

Align single flexbox item (video) at bottom left within SVG container

问题

以下是翻译好的部分:

我有一个SVG容器,它位于垂直/水平中心,具有预定义的纵横比(即1.77)。(这是来自这里的

我需要将视频元素放在SVG画布内的固定位置。之前我在所有浏览器中都能正常工作,但Safari(请参考此处:https://stackoverflow.com/questions/61895287/wrong-position-of-video-element-inside-svg-foreignobject-on-safari)除外。

现在我又回到尝试弄明白flexbox,以及如何将视频定位在底部左侧角(就像其他矩形一样)。

什么神奇的CSS可以解决这个问题?

当用户调整浏览器大小时,视频应该与左下角的矩形类似比例缩放。

Codepen: https://codepen.io/Kalaschnik/pen/OJBVOKL

<!-- 开始片段: js 隐藏: false 控制台: true Babel: false -->

<!-- 语言: lang-css -->

* {
    overflow: hidden;
}

body {
    display: flex;
    justify-content: center;
    align-items: center;
    font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto,
        Oxygen, Ubuntu, Cantarell, "Open Sans", "Helvetica Neue", sans-serif;
}

video {
    position: absolute;
    height: 100px;
    /* 如何将视频放在SVG的左下角 */
    /* left: ??? */
    /* bottom: ??? */
}

svg {
    --aspectRatio: 1.7777777;
    width: min(100vw, (100vh * var(--aspectRatio)));
    height: min(100vh, (100vw / var(--aspectRatio)));
}

<!-- 语言: lang-html -->

<video autoplay loop muted playsinline>
    <source src="https://ccp-odc.eva.mpg.de/matt/output-hevc-safari.mp4" type='video/mp4; codecs="hvc1"'>
    <source src="https://ccp-odc.eva.mpg.de/matt/output-vp9-chrome.webm" type="video/webm">
</video>

<svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" viewBox="0 0 1920 1080" style="enable-background:new 0 0 1920 1080;" xml:space="preserve">
    <rect fill="#00A99D" width="1920" height="1080" />
    <rect y="780" fill="#208096"  width="200" height="300" />
    <rect x="860" y="390" fill="#208096" width="200" height="300" />
</svg>

<!-- 结束片段 -->
英文:

I’ve got an SVG container that sits in a vertical/horizontal center with a predefined aspect ratio (i.e., 1.77). (This was coming from here.

I need to place a video element at a fixed position inside the SVG canvas. Prior I was using foreignObject, which was working great in all browsers except Safari (see here: https://stackoverflow.com/questions/61895287/wrong-position-of-video-element-inside-svg-foreignobject-on-safari).

No I am back to trying to get my head around flexbox, and how to position the video at the bottom left corner (where the other rectangle is).

What magic CSS will do the trick?

Of course, the video should be scale similar to the bottom left box when the users resized the browser.

Codepen: https://codepen.io/Kalaschnik/pen/OJBVOKL

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

* {
	overflow: hidden;
}

body {
	display: flex;
	justify-content: center;
	align-items: center;
	font-family: system-ui, -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Roboto,
		Oxygen, Ubuntu, Cantarell, &quot;Open Sans&quot;, &quot;Helvetica Neue&quot;, sans-serif;
}

video {
	position: absolute;
	height: 100px;
	/* How to place video at the bottom left of the SVG */
	/* left: ??? */
	/* bottom: ??? */
}

svg {
	--aspectRatio: 1.7777777;
	width: min(100vw, (100vh * var(--aspectRatio)));
	height: min(100vh, (100vw / var(--aspectRatio)));
}

<!-- language: lang-html -->

&lt;video autoplay loop muted playsinline&gt;
	&lt;source src=&quot;https://ccp-odc.eva.mpg.de/matt/output-hevc-safari.mp4&quot; type=&#39;video/mp4; codecs=&quot;hvc1&quot;&#39;&gt;
	&lt;source src=&quot;https://ccp-odc.eva.mpg.de/matt/output-vp9-chrome.webm&quot; type=&quot;video/webm&quot;&gt;
&lt;/video&gt;

&lt;svg version=&quot;1.1&quot; id=&quot;Layer_1&quot; xmlns=&quot;http://www.w3.org/2000/svg&quot; xmlns:xlink=&quot;http://www.w3.org/1999/xlink&quot; x=&quot;0px&quot; y=&quot;0px&quot; viewBox=&quot;0 0 1920 1080&quot; style=&quot;enable-background:new 0 0 1920 1080;&quot; xml:space=&quot;preserve&quot;&gt;
  &lt;rect fill=&quot;#00A99D&quot; width=&quot;1920&quot; height=&quot;1080&quot; /&gt;
	&lt;rect y=&quot;780&quot; fill=&quot;#208096&quot;  width=&quot;200&quot; height=&quot;300&quot; /&gt;
	&lt;rect x=&quot;860&quot; y=&quot;390&quot; fill=&quot;#208096&quot; width=&quot;200&quot; height=&quot;300&quot; /&gt;
&lt;/svg&gt;

<!-- end snippet -->

答案1

得分: 1

以下是翻译好的部分:

如果您将视频和SVG包装在一个<div>中,并在<div>上设置宽度和高度,而不是在SVG上设置,您只需将视频定位为绝对定位,位于左下角。只要SVG具有相同的纵横比,它将占据整个空间。<div>需要相对定位,但这对外部元素(body)的弹性没有影响。

* {
  overflow: hidden;
}

body {
  display: flex;
  justify-content: center;
  align-items: center;
}

video {
  position: absolute;
  height: 100px;
  left: 0;
  bottom: 0;
}

div {
  position: relative;
  --aspectRatio: 1.7777777;
  width: min(100vw, (100vh * var(--aspectRatio)));
  height: min(100vh, (100vw / var(--aspectRatio)));
}
<div>
  <video autoplay loop muted playsinline>
    <source src="https://ccp-odc.eva.mpg.de/matt/output-hevc-safari.mp4" type='video/mp4; codecs="hvc1"' />
    <source src="https://ccp-odc.eva.mpg.de/matt/output-vp9-chrome.webm" type="video/webm" />
  </video>

  <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1920 1080">
    <rect fill="#00A99D" width="1920" height="1080" />
    <rect y="780" fill="#208096"  width="200" height="300" />
    <rect x="860" y="390" fill="#208096" width="200" height="300" />
  </svg>
</div>
英文:

If you wrap the video and the SVG in a &lt;div&gt; and set the width and height on the &lt;div&gt; instead of the SVG you can just position the video absolute, bottom left corner. The SVG will take up the entire space as long as it is the same aspect ratio. The &lt;div&gt; needs to be positioned relative, but that makes no difference to the flex to the outer element (the body).

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

* {
  overflow: hidden;
}

body {
  display: flex;
  justify-content: center;
  align-items: center;
}

video {
  position: absolute;
  height: 100px;
  left: 0;
  bottom: 0;
}

div {
  position: relative;
  --aspectRatio: 1.7777777;
  width: min(100vw, (100vh * var(--aspectRatio)));
  height: min(100vh, (100vw / var(--aspectRatio)));
}

<!-- language: lang-html -->

&lt;div&gt;
  &lt;video autoplay loop muted playsinline&gt;
    &lt;source src=&quot;https://ccp-odc.eva.mpg.de/matt/output-hevc-safari.mp4&quot; type=&#39;video/mp4; codecs=&quot;hvc1&quot;&#39;/&gt;
    &lt;source src=&quot;https://ccp-odc.eva.mpg.de/matt/output-vp9-chrome.webm&quot; type=&quot;video/webm&quot;/&gt;
  &lt;/video&gt;

  &lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 1920 1080&quot;&gt;
    &lt;rect fill=&quot;#00A99D&quot; width=&quot;1920&quot; height=&quot;1080&quot; /&gt;
    &lt;rect y=&quot;780&quot; fill=&quot;#208096&quot;  width=&quot;200&quot; height=&quot;300&quot; /&gt;
    &lt;rect x=&quot;860&quot; y=&quot;390&quot; fill=&quot;#208096&quot; width=&quot;200&quot; height=&quot;300&quot; /&gt;
  &lt;/svg&gt;
&lt;/div&gt;

<!-- end snippet -->

答案2

得分: 0

以下是您要翻译的内容:

Got a similar fix without using flexbox

Just for the sake of completeness:

* {
    overflow: hidden;
}

.container {
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
}

svg {
    --aspectRatio: 1.7777777;
    width: min(100vw, (100vh * var(--aspectRatio)));
    height: min(100vh, (100vw / var(--aspectRatio)));
}

video {
    position: absolute;
    height: 30%;
    left: 0;
    bottom: 0;
}
<div class="container">
  <video autoplay loop muted playsinline>
    <source src="https://ccp-odc.eva.mpg.de/matt/output-hevc-safari.mp4" type='video/mp4; codecs="hvc1"'>
    <source src="https://ccp-odc.eva.mpg.de/matt/output-vp9-chrome.webm" type="video/webm">
  </video>

  <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1920 1080">
    <rect fill="#00A99D" width="1920" height="1080" />
    <rect y="780" fill="#208096"  width="200" height="300" />
    <rect x="860" y="390" fill="#208096" width="200" height="300" />
  </svg>
</div>
英文:

Got a similar fix without using flexbox

Just for the sake of completeness:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

* {
	overflow: hidden;
}

.container {
	position: absolute;
	left: 50%;
	top: 50%;
	transform: translate(-50%, -50%);
}

svg {
	--aspectRatio: 1.7777777;
	width: min(100vw, (100vh * var(--aspectRatio)));
	height: min(100vh, (100vw / var(--aspectRatio)));
}

video {
	position: absolute;
	height: 30%;
    left: 0;
	bottom: 0;
}

<!-- language: lang-html -->

&lt;div class=&quot;container&quot;&gt;
  &lt;video autoplay loop muted playsinline&gt;
    &lt;source src=&quot;https://ccp-odc.eva.mpg.de/matt/output-hevc-safari.mp4&quot; type=&#39;video/mp4; codecs=&quot;hvc1&quot;&#39;/&gt;
    &lt;source src=&quot;https://ccp-odc.eva.mpg.de/matt/output-vp9-chrome.webm&quot; type=&quot;video/webm&quot;/&gt;
  &lt;/video&gt;

  &lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 1920 1080&quot;&gt;
    &lt;rect fill=&quot;#00A99D&quot; width=&quot;1920&quot; height=&quot;1080&quot; /&gt;
    &lt;rect y=&quot;780&quot; fill=&quot;#208096&quot;  width=&quot;200&quot; height=&quot;300&quot; /&gt;
    &lt;rect x=&quot;860&quot; y=&quot;390&quot; fill=&quot;#208096&quot; width=&quot;200&quot; height=&quot;300&quot; /&gt;
  &lt;/svg&gt;
&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年4月13日 22:55:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/76006919.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定