我想包含两个16:9在包装器垂直视频元素.我希望元素尊重包装器的边界,并在保持其长宽比的同时响应窗口调整大小.当我有多个元素时,它会溢出包装器.在TailwinCSS的版本3中,新的纵横比类可以很好地工作.我正在使用@tailwindcss/aspect-ratio@0.4.0tailwind 插件.

https://codesandbox.io/s/aspect-ratio-tailwind-error-slbobj?file=/pages/index.vue

  <div class="flex flex-col min-h-screen">
    <main class="flex-1 flex bg-gray-900 max-h-screen text-white">
      <div class="flex-1 flex flex-col min-h-0 max-h-full">
        <!-- header -->
        <div class="flex-shink-0 flex items-center justify-between p-6">
          top bar
        </div>

        <!-- content -->
        <div class="flex-1 w-full max-w-[1200px] min-h-0 max-h-full mx-auto p-6 bg-green-500">
          <!-- video 1 -->
          <div class="aspect-w-16 aspect-h-9">
            <div class="w-full h-full bg-yellow-500"></div>
          </div>
          <!-- video 2 -->
          <div class="aspect-w-16 aspect-h-9">
            <div class="w-full h-full bg-red-500"></div>
          </div>
        </div>

        <!-- footer -->
        <div class="flex-shink-0 flex items-center justify-between p-6">
          bottom bar
        </div>
      </div>
    </main>
  </div>

enter image description here

我想要的是:

enter image description here

package version
tailwindcss 2.2.15
@tailwindcss/aspect-ratio 0.4.0

推荐答案

因为内容容器中的绝对值为max-w-[1200px],所以会出现溢出.要解决这个问题,请使用带有相对值的calc()函数,诀窍是使用vh,因为我们需要停止容器中div的增长.

enter image description here

<script src="https://unpkg.com/tailwindcss-jit-cdn"></script>

<div class="flex flex-col min-h-screen">
  <main class="flex-1 flex bg-gray-900 max-h-screen text-white">
    <div class="flex-1 flex flex-col min-h-0 max-h-full">
      <!-- header -->
      <div class="flex-shink-0 flex items-center justify-between p-6">top bar</div>

      <!-- content -->
      <div class="flex-1 w-full max-w-[calc(100vh-300px)] min-h-0 max-h-full mx-auto p-6 bg-green-500">
        <!-- video 1 -->
        <div class="aspect-w-16 aspect-h-9">
          <div class="w-full h-full bg-yellow-500"></div>
        </div>
        <!-- video 2 -->
        <div class="aspect-w-16 aspect-h-9">
          <div class="w-full h-full bg-red-500"></div>
        </div>
      </div>

      <!-- footer -->
      <div class="flex-shink-0 flex items-center justify-between p-6">bottom bar</div>
    </div>
  </main>
</div>

Vue.js相关问答推荐

如何根据数据库中的条件在vue中创建类

在VueJS中卸载元素之前是否应该删除JavaScript事件收件箱?

虚拟DOM在Vue中姗姗来迟

Vue.js编译的render函数不起作用

Vue 3 / Vuetify 3:如何为按钮设置全局默认 colored颜色

处理多张卡片中的按钮

纵横比元素溢出容器

我如何使用 vuejs 在我的 url 中编码一个令牌

将媒体源/流绑定到视频元素 - Vue 3 组合 API

两个div元素之间的vue2过渡

Webpack 编译错误:TypeError: __WEBPACK_IMPORTED_MODULE_1__ … is not a function

使用 azure devops 发布管道部署 Vue.js 应用程序

Django Rest 框架、CSRF 和 Vue.js

Vue 和 TypeScript 所需的props

Vue 3 - 带有全局组件的无法解析组件

如何在 Vue 3 中使用 SSR

VueJS 使用 prop 作为数据属性值

如何在 VueJS 中访问 [__ob__: Observer] 的元素?

vue组件中导入和使用three.js库

如何在我的 vue js 中实现无限滚动