Home  >  Article  >  Web Front-end  >  HTML5实践-使用CSS实现弹性视频的代码分享

HTML5实践-使用CSS实现弹性视频的代码分享

黄舟
黄舟Original
2017-03-23 15:26:201525browse

   当我编码Elemin Theme(我最近设计的一个响应式的站点)的时候,我遇到的一个跳帧就是,如何能让嵌入式的视频在尺寸变化上变得更加灵活。使用max-width:100% 和height:auto可以让html5的video标签很好的工作,但是这个解决方案不适用于iframe 或者 object标签的内嵌代码。通过几小时的寻找资料和实验,我最终找到了解决办法。当你在进行响应式设计的时候,这一css技巧能派上用场。你可以访问最终demo地址,缩放你的浏览器查看效果。

  灵活的html5 video标签(demo)

  使用html5的video,可以通过设置max-width:100%让他变得灵活。前面的介绍中,已经提到他不适用于常用的iframe和object中的内嵌代码。

video {
    max-width: 100%;
    height: auto;
}

  灵活的 Object & Iframe 内嵌视频

  这个技巧相当简单,你需要为video添加一个e388a4556c0f65e1904146cc1a846bee容器,并且将p的padding-bottom属性值设置在50%到60%之间。然后设置子元素(ifame或者object)的width和height为100%,并且使用绝对定位。这样会迫使内嵌对象自动扩充到最大。

  CSS

.video-container {
    position: relative;
    padding-bottom: 56.25%;
    padding-top: 30px;
    height: 0;
    overflow: hidden;
}.video-container iframe,  
.video-container object,  
.video-container embed {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
}

  HTML

  在固定宽度下实现灵活性

  如果限制了视频的宽度,那么我们需要一个额外的e388a4556c0f65e1904146cc1a846bee容器包裹video,并为p设置固定宽度和max-width:100%。

  CSS

.video-wrapper {
    width: 600px;
    max-width: 100%;
}

  HTML

  兼容性

  这个技巧支持所有的浏览器,包括:Chrome, Safari, Firefox, Internet Explorer, Opera, iPhone 和 iPad。

The above is the detailed content of HTML5实践-使用CSS实现弹性视频的代码分享. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact [email protected]