【问题标题】:Maintain aspect ratio of video in a fix height and fixed width layout using CSS使用 CSS 在固定高度和固定宽度布局中保持视频的纵横比
【发布时间】:2013-11-11 06:40:13
【问题描述】:

我正在创建一个具有固定高度布局(不使用弹性框)的网络应用程序,并且我也受到宽度的限制。

我想将屏幕从中间向下切开,并以大约 50% 的高度和宽度穿过中心。

在屏幕的左象限我不想插入 HTML5 视频播放器,而在右象限我想插入一些文本和图像。

我可以通过使用此处的 flex-video http://foundation.zurb.com/docs/components/flex-video.html 中指定的技术来保持视频播放器的纵横比

但是,这会因视频溢出到屏幕的下半部分而中断。

有没有我可以单独使用 CSS 来解决这个问题?

这是我目前所拥有的

<!DOCTYPE html>
    <head>
       <style>

       *{
        box-sizing: border-box;
       }

       body{
        margin: 0;
        padding: 0;

        font-family:sans-serif;
        color:white;
       }

       video {
        height: 100%;

       }

       .header{
            height: 60px;
            background: #002442;
            position: relative;
       }

        .media-card-screen{
            background: linear-gradient(to bottom, #004680 0%, #00080f 99%);
            position: absolute;
            width: 100%; 
            top:60px;
            bottom: 0px;
        }

        .media-card{
            height: calc(100% / 1.75);
        }

        .video-container{
            width: 50%;
            display: inline-block;
            padding: 20px 20px 0px 20px;
            height: 100%;
        }

        .video{
            display: block;
            overflow: hidden;
            height: 0;
            padding-bottom: 56.25%;
        }

        .title{
            display: inline-block;
            position: relative;
            vertical-align: top;
            height: 50px;
        }

        .title h1{
            margin: 0;
            padding: 0;
            font-size: 26px;
        }

        .rating{
            /*display: inline-block;
            vertical-align: top;
            position: absolute;
            right: 0px;*/
        }

        .action-bar-container{
            padding: 0;
            margin-top: 20px;
            list-style: none;
        }

        .action{
            height: 50px;
            line-height: 50px;
            background: green;
            margin: 0;
            padding: 0;
        }

        .title-rating-container{
            display:inline-block;
            vertical-align: top; 
            width:49%;
            position:relative;
            overflow: auto;
            height: 100%;
            padding: 20px 20px 0px 0px;
        }

        .move-short-desc{
            font-size: 18px;
        }

        .move-long-desc{
            font-size: 18px;
        }

        .mc-action-bar{
           -webkit-box-shadow: 0 2px 2px 0 black;
            -ms-box-shadow: 0 2px 2px 0 black;
            -moz-box-shadow: 0 2px 2px 0 black;
            box-shadow: 0 2px 2px 0 black;
            background: #004680;
            height: 50px;
            line-height: 50px;
            text-align: left;
            overflow: hidden;
            white-space: nowrap;
            padding: 0px 0px 0px 20px;
            margin-top: 20px;
        }

        .more-like-this-bar{
            height: 50px;
            line-height: 50px;
            background-color: rgba(0, 70, 128, 0.4);
        }

        .more-like-this-bar h3{
            margin: 0px 0px 0px 20px;
            padding: 0;
        }

       </style>
    </head>
    <body>
        <div class="master-container">
            <div class="header">

            </div>
            <div class="media-card-screen">
                <div class="media-card">
                    <div class="video-container">
                        <div class="video">
                            <video id="video" controls="" preload="none">
                                <source id="mp4" src="http://media.w3.org/2010/05/sintel/trailer.mp4" type="video/mp4">
                                  <source id="webm" src="http://media.w3.org/2010/05/sintel/trailer.webm" type="video/webm">
                                  <source id="ogv" src="http://media.w3.org/2010/05/sintel/trailer.ogv" type="video/ogg">

                            </video>
                        </div>
                    </div>


                    <div class="title-rating-container">
                        <div class="title">
                            <h1>Captain Phillips</h1>
                        </div>

                        <img style="float:right; margin: 5px; width: 150px;" src="http://placehold.it/150x255">   

                        <div class="rating">
                            <img src="http://upload.wikimedia.org/wikipedia/commons/thumb/7/7f/Pg_cinema.png/626px-Pg_cinema.png" width="50" height="50">
                        </div>

                        <div class="expires">
                            <h3>Expires: 3hrs</h3>  
                        </div>

                        <div class="duration">
                            <h3>Duration: 128mins</h3>  
                        </div>

                        <div class="move-short-desc">

                            <p>The true story of Captain Richard Phillips and the 2009 hijacking by Somali pirates of the US-flagged MV Maersk Alabama, the first American cargo ship to be hijacked in two hundred years.</p>

                        </div>

                         <div class="move-long-desc">

                            <p>Two-time Academy Award winner Tom Hanks teams with Oscar-nominated director Paul Greengrass and screenwriter Billy Ray to tell the true story of Richard Phillips, a U.S. cargo-ship captain who surrendered himself to Somali pirates so that his crew would be freed. The MV Maersk Alabama was en route to Mombasa, Kenya, when it was surrounded and boarded by a gang of hardened bandits led by the determined Muse (Barkhad Abdi). As the crew of the Maersk Alabama rushed into a fortified "secure room" created for just such an incident, Phillips and Muse found themselves in a tense standoff that threatened to erupt into violence at any moment. Catherine Keener and Michael Chernus co-star. ~ Jason Buchanan, Rovi</p>

                        </div>

                    </div>
                </div>

                <div id="actionBar" class="mc-action-bar">
                    <div class="mc-action-button" data-call-to-action-button="buy">
                        <i class="icon-shopping-cart"></i><span>Buy</span>
                    </div>
                    <div class="mc-action-button" data-call-to-action-button="favourites">
                        <i class="icon-heart"></i><span>Remove From Favourites</span>
                    </div>
                </div>

                <div class="more-like-this">
                    <div class="more-like-this-bar">
                        <h3>MORE LIKE THIS</h3>
                    </div>

                    <div class="more-like-this-scroller-container">

                    </div>
                </div>
        </div>

    </body>
</html>

【问题讨论】:

  • 如果您的布局是固定宽度和高度的,为什么还需要响应式视频?你有什么理由不能只给视频一个固定的宽度和高度吗?

标签: css html video responsive-design


【解决方案1】:

您可以让video 对象自动调整大小,一旦加载视频(按下播放时),它将保持纵横比,就像img 标签所做的那样:

CSS:

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

html:

<video id="video" controls="" preload="none">
    <source id="mp4" src="http://media.w3.org/2010/05/sintel/trailer.mp4" type="video/mp4">
    <source id="webm" src="http://media.w3.org/2010/05/sintel/trailer.webm" type="video/webm">
    <source id="ogv" src="http://media.w3.org/2010/05/sintel/trailer.ogv" type="video/ogg">
</video>

测试: http://jsfiddle.net/3WB2q/1/

【讨论】:

    【解决方案2】:

    我最近解决了同样的问题(见http://jsfiddle.net/6PXhP/ 或以下):

    <div class="aspect-ratio aspect-ratio-16-9">
        <iframe width="560" height="315" src="//www.youtube-nocookie.com/embed/ZjsbbdsFOOU?rel=0" frameborder="0" allowfullscreen=""></iframe>
    </div>
    

    在 CSS 中:

    .aspect-ratio {
        position: relative;
        max-width: 100%;
        height: auto;
    }
    
    .aspect-ratio > * {
        position: absolute;
        top: 0;
        left: 0;
        width: 100%;
        height: 100%;
    }
    
    .aspect-ratio-16-9 {
        padding-top: 56.25% !important;
    }
    

    其中 56.25% 是 9/16 * 100%。

    【讨论】:

      【解决方案3】:

      我使用 max-height 和 min-height 属性使它工作。

      不幸的是,视频链接在测试时断开了,所以我无法验证容器内的视频是否渲染良好。

      另外,我将.video-container 更改为浮动块,并删除.video 样式。

      这是修改后的完整代码:

      <!DOCTYPE html>
          <head>
             <style>
      
             *{
              box-sizing: border-box;
             }
      
             body{
              margin: 0;
              padding: 0;
      
              font-family:sans-serif;
              color:white;
             }
      
             video {
              height: 100%;
               width: 100%;
               min-height: 100%;
               max-height: 100%;
             }
      
             .header{
                  height: 60px;
                  background: #002442;
                  position: relative;
             }
      
              .media-card-screen{
                  background: linear-gradient(to bottom, #004680 0%, #00080f 99%);
                  position: absolute;
                  width: 100%; 
                  top:60px;
                  bottom: 0px;
              }
      
              .media-card{
                  height: calc(100% / 1.75);
              }
      
              .video-container{
                  width: 50%;
              display: block;
              float: left;
                  padding: 20px 20px 0px 20px;
                  height: 100%;
      
              }
      
              /*.video{
                  display: block;
                  overflow: hidden;
                  height: 0;
                  padding-bottom: 56.25%;
              }*/
      
              .title{
                  display: inline-block;
                  position: relative;
                  vertical-align: top;
                  height: 50px;
              }
      
              .title h1{
                  margin: 0;
                  padding: 0;
                  font-size: 26px;
              }
      
              .rating{
                  /*display: inline-block;
                  vertical-align: top;
                  position: absolute;
                  right: 0px;*/
              }
      
              .action-bar-container{
                  padding: 0;
                  margin-top: 20px;
                  list-style: none;
              }
      
              .action{
                  height: 50px;
                  line-height: 50px;
                  background: green;
                  margin: 0;
                  padding: 0;
              }
      
              .title-rating-container{
                  display:inline-block;
                  vertical-align: top; 
                  width:49%;
                  position:relative;
                  overflow: auto;
                  height: 100%;
                  padding: 20px 20px 0px 0px;
              }
      
              .move-short-desc{
                  font-size: 18px;
              }
      
              .move-long-desc{
                  font-size: 18px;
              }
      
              .mc-action-bar{
                 -webkit-box-shadow: 0 2px 2px 0 black;
                  -ms-box-shadow: 0 2px 2px 0 black;
                  -moz-box-shadow: 0 2px 2px 0 black;
                  box-shadow: 0 2px 2px 0 black;
                  background: #004680;
                  height: 50px;
                  line-height: 50px;
                  text-align: left;
                  overflow: hidden;
                  white-space: nowrap;
                  padding: 0px 0px 0px 20px;
                  margin-top: 20px;
              }
      
              .more-like-this-bar{
                  height: 50px;
                  line-height: 50px;
                  background-color: rgba(0, 70, 128, 0.4);
              }
      
              .more-like-this-bar h3{
                  margin: 0px 0px 0px 20px;
                  padding: 0;
              }
      
             </style>
          </head>
          <body>
              <div class="master-container">
                  <div class="header">
      
                  </div>
                  <div class="media-card-screen">
                      <div class="media-card">
                          <div class="video-container">
                              <div class="video">
                                  <video id="video" controls="" preload="none">
                                      <source id="mp4" src="http://media.w3.org/2010/05/sintel/trailer.mp4" type="video/mp4">
                                        <source id="webm" src="http://media.w3.org/2010/05/sintel/trailer.webm" type="video/webm">
                                        <source id="ogv" src="http://media.w3.org/2010/05/sintel/trailer.ogv" type="video/ogg">
      
                                  </video>
                              </div>
                          </div>
      
      
                          <div class="title-rating-container">
                              <div class="title">
                                  <h1>Captain Phillips</h1>
                              </div>
      
                              <img style="float:right; margin: 5px; width: 150px;" src="http://placehold.it/150x255">   
      
                              <div class="rating">
                                  <img src="http://upload.wikimedia.org/wikipedia/commons/thumb/7/7f/Pg_cinema.png/626px-Pg_cinema.png" width="50" height="50">
                              </div>
      
                              <div class="expires">
                                  <h3>Expires: 3hrs</h3>  
                              </div>
      
                             <div class="duration">
                                 <h3>Duration: 128mins</h3>  
                             </div>
      
                             <div class="move-short-desc">
      
                                 <p>The true story of Captain Richard Phillips and the 2009 hijacking by Somali pirates of the US-flagged MV Maersk Alabama, the first American cargo ship to be hijacked in two hundred years.</p>
      
                             </div>
      
                              <div class="move-long-desc">
      
                                 <p>Two-time Academy Award winner Tom Hanks teams with Oscar-nominated director Paul Greengrass and screenwriter Billy Ray to tell the true story of Richard Phillips, a U.S. cargo-ship captain who surrendered himself to Somali pirates so that his crew would be freed. The MV Maersk Alabama was en route to Mombasa, Kenya, when it was surrounded and boarded by a gang of hardened bandits led by the determined Muse (Barkhad Abdi). As the crew of the Maersk Alabama rushed into a fortified "secure room" created for just such an incident, Phillips and Muse found themselves in a tense standoff that threatened to erupt into violence at any moment. Catherine Keener and Michael Chernus co-star. ~ Jason Buchanan, Rovi</p>
      
                             </div>
      
                         </div>
                    </div>
      
                    <div id="actionBar" class="mc-action-bar">
                        <div class="mc-action-button" data-call-to-action-button="buy">
                            <i class="icon-shopping-cart"></i><span>Buy</span>
                        </div>
                        <div class="mc-action-button" data-call-to-action-button="favourites">
                            <i class="icon-heart"></i><span>Remove From Favourites</span>
                        </div>
                    </div>
      
                    <div class="more-like-this">
                        <div class="more-like-this-bar">
                            <h3>MORE LIKE THIS</h3>
                        </div>
      
                        <div class="more-like-this-scroller-container">
      
                        </div>
                    </div>
            </div>
      
        </body>
      

      【讨论】:

        猜你喜欢
        • 2015-12-02
        • 1970-01-01
        • 2014-02-06
        • 2013-07-07
        • 2017-02-13
        • 1970-01-01
        • 2016-09-03
        • 1970-01-01
        • 2014-10-14
        相关资源
        最近更新 更多