html - 如何内联放置视频和图像,每个宽度为父级宽度的一半?

标签 html css

我想将视频和图像并排放置,均为父级的 50%。下面是我试过的代码。在我看来,每个包装 div 应该是内联的,50%;子元素填充宽度。

HTML:

<div>
   <div class="video-container">
    <video poster="Resources/css/img/ioposter.png" controls="true" muted loop>
        <source src="/Resources/img/vid.mp4"  type="video/mp4">
        Your browser does not support the video tag.
    </video>
   </div>
   <div style="width:50%;display:inline-block;">
     <img style="width:100%;" src="Resources/myimg.jpg">
   </div>
</div>

CSS:

.video-container {
   position: relative;
   padding-bottom: 56.25%;
   padding-top: 0px;
   height: 0;
   overflow: hidden;
   display: inline-block;
   width:50%;
   vertical-align: top;
}

.video-container video {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
} 

最佳答案

我创建了一个用于测试的快速 jsFiddle:https://jsfiddle.net/L5euvkzm/2/

.wrapper {
    font-size: 0;
}

我所做的就是向外部 div 添加一个类,然后应用 font-size: 0;发生这种情况的原因是因为两个容器之间添加了空白。有其他方法可以解决这个问题,但我更喜欢这种方式,因为它可以让元素很好地并排堆叠。

关于html - 如何内联放置视频和图像,每个宽度为父级宽度的一半?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49762490/

相关文章:

HTML + CSS z-index 不工作

javascript - 在 HTML 表 : How To Manage Tables Row Height? 中

css - 在 WordPress 帖子中将文本宽度设置为 75%,将图像宽度设置为 100%?

javascript - 未接收动态生成的表单数据

html - 如何为这个 HTML 编写 XPath 和 CSS?

javascript - 如何设置覆盖 div 加载图像

jquery - 将样式应用于包含某个类的 div 的 div

html - html中元素之间的间距

html - 表单文本字段不完全相同

html - 卡片翻转 IE 动画问题